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

Update Field stubs to include None as an input/output type #511

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Kangaroux
Copy link

@Kangaroux Kangaroux commented Nov 17, 2023

I have made things!

This updates the field stubs to include None as valid input/output. The only projects that should be impacted by this change are those using serializer fields directly which I imagine are a very small minority. Since BaseSerializer is a subclass of Field but uses Any as the input/output types, the serializer stubs should remain unchanged.

Related issues

@Kangaroux Kangaroux changed the title Allow none as field value Update Field stubs to include None as an input/output type Nov 17, 2023
Any,
]
):
TRUE_VALUES: set[str | bool | int]
FALSE_VALUES: set[str | bool | int | float]
NULL_VALUES: set[str | None]

class CharField(Field[str, str, str, Any]):
class NullBooleanField(BooleanField): ...
Copy link
Author

@Kangaroux Kangaroux Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like NullBooleanField was deprecated in v3.13 of DRF (and removed in v3.14)

Do you want me to remove it in this PR or make a separate one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate PR please.

Any,
]
):
TRUE_VALUES: set[str | bool | int]
FALSE_VALUES: set[str | bool | int | float]
NULL_VALUES: set[str | None]

class CharField(Field[str, str, str, Any]):
class NullBooleanField(BooleanField): ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate PR please.

@@ -227,7 +217,7 @@ class SlugField(CharField):

class URLField(CharField): ...

class UUIDField(Field[uuid.UUID, uuid.UUID | str | int, str, Any]):
class UUIDField(Field[uuid.UUID | None, uuid.UUID | str | int | None, str | None, Any]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the best way to go about this.

I suspect not all methods using these typevars can deal with None.

And many places that use these TypeVars already explicitly specify e.g. _VT | None. Seems a lot safer to just update the signatures of the to_representation() method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, IntegerField.to_representation() actually does not allow None... Not sure what to make of this.

    def to_representation(self, value):
        return int(value)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Field.to_representation() should accept None
2 participants