Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type definition for Field.__set__ does not expect None to be passed even if the field is nullable #1889

Open
zwx00 opened this issue Feb 22, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@zwx00
Copy link

zwx00 commented Feb 22, 2025

Describe the bug
Trying to assign None to a field that has null=True gives a type error.

To Reproduce

import tortoise
from tortoise import fields

class Test(tortoise.Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255, null=True)

a = Test(name="test")
a.name = None

Resulting type checks:

git:(main) ✗ uv run mypy .      
test.py:12: error: Incompatible types in assignment (expression has type "None", variable has type "str")  [assignment]
Found 1 error in 1 file (checked 2 source files)
git:(main) ✗ uv run pyright .
/Users/zw/Development/tortoise-typebug/test.py
  /Users/zw/Development/tortoise-typebug/test.py:12:3 - error: Cannot assign to attribute "name" for class "Test"
    Argument of type "None" cannot be assigned to parameter "value" of type "str" in function "__set__"
      "None" is not assignable to "str" (reportAttributeAccessIssue)
1 error, 0 warnings, 0 informations 

Expected behavior
Should be allowed to set a field to None. It's a legal value and the type definition should reflect this.
Additional context
Happy to help out with a PR if needed. Would appreciate some pointers.

https://github.com/tortoise/tortoise-orm/blob/develop/tortoise/fields/base.py#L165

Here's the problematic code. This is causing an issue because CharField is defined as follows:

class CharField(Field[str]):
    ...

I'm also including my current workaround which works but requires lot of copypaste boilerplate.

class RequiredUUIDField(fields.UUIDField):
    def __set__(self, instance: "Model", value: uuid.UUID) -> None:
        super().__set__(instance, value)


class NullableUUIDField(fields.UUIDField):
    def __set__(self, instance: "Model", value: Optional[uuid.UUID]) -> None:
        super().__set__(instance, value)  # type: ignore


@overload
def UUIDField(null: Literal[False] = False, **kwargs) -> RequiredUUIDField: ...


@overload
def UUIDField(null: Literal[True], **kwargs) -> NullableUUIDField: ...


def UUIDField(null: bool = False, **kwargs):
    if null:
        return NullableUUIDField(null=True, **kwargs)
    return RequiredUUIDField(**kwargs)
@zwx00 zwx00 changed the title Fields' __set__ does not allow None to be passed even if the field is nullable Fields __set__ does not expect None to be passed even if the field is nullable Feb 22, 2025
@zwx00 zwx00 changed the title Fields __set__ does not expect None to be passed even if the field is nullable Types for fields __set__ does not expect None to be passed even if the field is nullable Feb 22, 2025
@zwx00 zwx00 changed the title Types for fields __set__ does not expect None to be passed even if the field is nullable Type definition for Field.__set__ does not expect None to be passed even if the field is nullable Feb 22, 2025
@henadzit henadzit added the enhancement New feature or request label Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants