Skip to content

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

Open
Kangaroux wants to merge 4 commits into
typeddjango:masterfrom
Kangaroux:allow-none-as-field-value
Open

Update Field stubs to include None as an input/output type#511
Kangaroux wants to merge 4 commits into
typeddjango:masterfrom
Kangaroux:allow-none-as-field-value

Conversation

@Kangaroux

@Kangaroux Kangaroux commented Nov 17, 2023

Copy link
Copy Markdown

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
NULL_VALUES: set[str | None]

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

@Kangaroux Kangaroux Nov 17, 2023

Copy link
Copy Markdown
Author

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Separate PR please.

NULL_VALUES: set[str | None]

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Separate PR please.

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
Copy Markdown
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
Copy Markdown
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