-
-
Notifications
You must be signed in to change notification settings - Fork 496
Use field generic types for descriptors #2048
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
Changes from 2 commits
48266f2
f9529e2
8fa7a14
babf968
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
- case: test_custom_model_fields_with_generic_type | ||
main: | | ||
from myapp.models import User, CustomFieldValue | ||
user = User() | ||
reveal_type(user.id) # N: Revealed type is "builtins.int" | ||
reveal_type(user.my_custom_field1) # N: Revealed type is "myapp.models.CustomFieldValue" | ||
reveal_type(user.my_custom_field2) # N: Revealed type is "myapp.models.CustomFieldValue" | ||
reveal_type(user.my_custom_field3) # N: Revealed type is "builtins.bool" | ||
reveal_type(user.my_custom_field4) # N: Revealed type is "myapp.models.CustomFieldValue" | ||
reveal_type(user.my_custom_field5) # N: Revealed type is "Union[myapp.models.CustomFieldValue, None]" | ||
# user.my_custom_field6 is incorrectly typed as non-optional | ||
# reveal_type(user.my_custom_field6) ## N: Revealed type is "Union[myapp.models.CustomFieldValue, None]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect on master too and does not pick up on null=True. When debugging, the instance seemed correct and the args were optional so I think this issue is coming from another piece of code somewhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slightly off topic but still related: I think #1900 is involved and/or need to be considered regarding IMO we should finish explore that approach primarily, before moving on to a plugin approach. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would really like to see #1900 making it as well, although in its current form it doesn't really simplify creating custom field subclasses (and I believe addressing #1900 (comment) would help in this case but requires significant work on mypy) |
||
reveal_type(user.my_custom_field7) # N: Revealed type is "Union[builtins.bool, None]" | ||
reveal_type(user.my_custom_field8) # N: Revealed type is "Union[myapp.models.CustomFieldValue, None]" | ||
reveal_type(user.my_custom_field9) # N: Revealed type is "myapp.models.CustomFieldValue" | ||
reveal_type(user.my_custom_field10) # N: Revealed type is "Union[myapp.models.CustomFieldValue, None]" | ||
flaeppe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Fields that set _pyi_private_set_type or _pyi_private_get_type retain these types | ||
reveal_type(user.my_custom_field11) # N: Revealed type is "builtins.int" | ||
reveal_type(user.my_custom_field12) # N: Revealed type is "builtins.int" | ||
monkeypatch: true | ||
installed_apps: | ||
- myapp | ||
files: | ||
- path: myapp/__init__.py | ||
- path: myapp/models.py | ||
content: | | ||
from django.db import models | ||
from django.db.models import fields | ||
|
||
from typing import Any, TypeVar, Generic, Union | ||
|
||
_ST = TypeVar("_ST", contravariant=True) | ||
_GT = TypeVar("_GT", covariant=True) | ||
|
||
T = TypeVar("T") | ||
|
||
class CustomFieldValue: ... | ||
|
||
class GenericField(fields.Field[_ST, _GT]): ... | ||
|
||
class SingleTypeField(fields.Field[T, T]): ... | ||
|
||
class CustomValueField(fields.Field[Union[CustomFieldValue, int], CustomFieldValue]): ... | ||
|
||
class AdditionalTypeVarField(fields.Field[_ST, _GT], Generic[_ST, _GT, T]): ... | ||
|
||
class CustomSmallIntegerField(fields.SmallIntegerField[_ST, _GT]): ... | ||
|
||
class User(models.Model): | ||
id = models.AutoField(primary_key=True) | ||
my_custom_field1 = GenericField[Union[CustomFieldValue, int], CustomFieldValue]() | ||
my_custom_field2 = CustomValueField() | ||
my_custom_field3 = SingleTypeField[bool]() | ||
my_custom_field4 = AdditionalTypeVarField[Union[CustomFieldValue, int], CustomFieldValue, bool]() | ||
my_custom_field5 = GenericField[Union[CustomFieldValue, int], CustomFieldValue](null=True) | ||
my_custom_field6 = CustomValueField(null=True) | ||
my_custom_field7 = SingleTypeField[bool](null=True) | ||
my_custom_field8 = AdditionalTypeVarField[Union[CustomFieldValue, int], CustomFieldValue, bool](null=True) | ||
my_custom_field9 = fields.Field[Union[CustomFieldValue, int], CustomFieldValue]() | ||
my_custom_field10 = fields.Field[Union[CustomFieldValue, int], CustomFieldValue](null=True) | ||
my_custom_field11 = fields.SmallIntegerField[bool, bool]() | ||
my_custom_field12 = CustomSmallIntegerField[bool, bool]() |
Uh oh!
There was an error while loading. Please reload this page.