You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Setting null=True on a Django TextField or CharField successfully tells mypy that the field's value can be None:
classMyModel(models.Model):
text=TextField(..., null=True)
foo=MyModel(text=None) # all good
But setting null=True this repo's custom MultiCollation(Text|Char)Field, which is a subclass of Django's (Text|Char)Field, does not lead mypy to the same conclusion:
classMyModel(models.Model):
text=MultiCollationTextField(..., null=True)
foo=MyModel(text=None) # error: Incompatible type for "text" of "Content" (got "None", expected "Union[str, Combinable]") [misc]
It's ugly, but we can work around this currently by explicitly annotating the type of text:
classMyModel(models.Model):
text: models.TextField[str|None, str|None] =MultiCollationTextField(..., null=True)
foo=MyModel(text=None) # all good
Side note: We could work around this now using # type: ignore wherever we set text=None, but it's not great, because .text should in fact have the type str|None, and thus it'd be valuable for mypy to yell at us if we try to use it as if it's type is just str.
I believe that the solution involves some wrangling of generic type arguments between MultiCollationTextField and TextField. We also might end up needing two separate sets of classes: MultiCollation(Text|Char)Field and NullableMultiCollation(Text|Char)Field.
The text was updated successfully, but these errors were encountered:
Context: #149 (comment)
Setting
null=True
on a Django TextField or CharField successfully tells mypy that the field's value can be None:But setting
null=True
this repo's custom MultiCollation(Text|Char)Field, which is a subclass of Django's (Text|Char)Field, does not lead mypy to the same conclusion:It's ugly, but we can work around this currently by explicitly annotating the type of text:
Side note: We could work around this now using
# type: ignore
wherever we settext=None
, but it's not great, because.text
should in fact have the typestr|None
, and thus it'd be valuable for mypy to yell at us if we try to use it as if it's type is juststr
.I believe that the solution involves some wrangling of generic type arguments between MultiCollationTextField and TextField. We also might end up needing two separate sets of classes: MultiCollation(Text|Char)Field and NullableMultiCollation(Text|Char)Field.
The text was updated successfully, but these errors were encountered: