-
-
Notifications
You must be signed in to change notification settings - Fork 516
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
48266f2
Use field generic types for descriptors
md384 f9529e2
Fix type annotations for older python versions
md384 8fa7a14
Enforce get type is optional when field is nullable and don't implici…
md384 babf968
Fix imports and add field name to error message
md384 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| - 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 "myapp.models.CustomFieldValue" | ||
| reveal_type(user.my_custom_field6) # N: Revealed type is "myapp.models.CustomFieldValue" | ||
| reveal_type(user.my_custom_field7) # N: Revealed type is "builtins.bool" | ||
| reveal_type(user.my_custom_field8) # N: Revealed type is "myapp.models.CustomFieldValue" | ||
| reveal_type(user.my_custom_field9) # N: Revealed type is "myapp.models.CustomFieldValue" | ||
| reveal_type(user.my_custom_field10) # N: Revealed type is "builtins.bool" | ||
| reveal_type(user.my_custom_field11) # N: Revealed type is "builtins.bool" | ||
| reveal_type(user.my_custom_field12) # N: Revealed type is "Union[myapp.models.CustomFieldValue, None]" | ||
| reveal_type(user.my_custom_field13) # N: Revealed type is "Union[myapp.models.CustomFieldValue, None]" | ||
| reveal_type(user.my_custom_field14) # N: Revealed type is "Union[builtins.bool, None]" | ||
| reveal_type(user.my_custom_field15) # N: Revealed type is "None" | ||
| monkeypatch: true | ||
| out: | | ||
| myapp/models:31: error: Field is nullable but generic get type parameter is not optional [misc] | ||
| myapp/models:32: error: Field is nullable but generic get type parameter is not optional [misc] | ||
| myapp/models:33: error: Field is nullable but generic get type parameter is not optional [misc] | ||
| myapp/models:34: error: Field is nullable but generic get type parameter is not optional [misc] | ||
| myapp/models:35: error: Field is nullable but generic get type parameter is not optional [misc] | ||
flaeppe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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]() | ||
| # test null=True on fields with non-optional generic types throw error | ||
| 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](null=True) | ||
| # test overriding fields that set _pyi_private_set_type or _pyi_private_get_type | ||
| my_custom_field10 = fields.SmallIntegerField[bool, bool]() | ||
| my_custom_field11 = CustomSmallIntegerField[bool, bool]() | ||
| # test null=True on fields with non-optional generic types throw no errors | ||
| my_custom_field12 = fields.Field[Union[CustomFieldValue, int], Union[CustomFieldValue, None]](null=True) | ||
| my_custom_field13 = GenericField[Union[CustomFieldValue, int], Union[CustomFieldValue, None]](null=True) | ||
| my_custom_field14 = SingleTypeField[Union[bool, None]](null=True) | ||
| my_custom_field15 = fields.Field[None, None](null=True) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.