|
| 1 | +- case: test_custom_model_fields_with_generic_type |
| 2 | + main: | |
| 3 | + from myapp.models import User, CustomFieldValue |
| 4 | + user = User() |
| 5 | + reveal_type(user.id) # N: Revealed type is "builtins.int" |
| 6 | + reveal_type(user.my_custom_field1) # N: Revealed type is "myapp.models.CustomFieldValue" |
| 7 | + reveal_type(user.my_custom_field2) # N: Revealed type is "myapp.models.CustomFieldValue" |
| 8 | + reveal_type(user.my_custom_field3) # N: Revealed type is "builtins.bool" |
| 9 | + reveal_type(user.my_custom_field4) # N: Revealed type is "myapp.models.CustomFieldValue" |
| 10 | + reveal_type(user.my_custom_field5) # N: Revealed type is "myapp.models.CustomFieldValue" |
| 11 | + reveal_type(user.my_custom_field6) # N: Revealed type is "myapp.models.CustomFieldValue" |
| 12 | + reveal_type(user.my_custom_field7) # N: Revealed type is "builtins.bool" |
| 13 | + reveal_type(user.my_custom_field8) # N: Revealed type is "myapp.models.CustomFieldValue" |
| 14 | + reveal_type(user.my_custom_field9) # N: Revealed type is "myapp.models.CustomFieldValue" |
| 15 | + reveal_type(user.my_custom_field10) # N: Revealed type is "builtins.bool" |
| 16 | + reveal_type(user.my_custom_field11) # N: Revealed type is "builtins.bool" |
| 17 | + reveal_type(user.my_custom_field12) # N: Revealed type is "Union[myapp.models.CustomFieldValue, None]" |
| 18 | + reveal_type(user.my_custom_field13) # N: Revealed type is "Union[myapp.models.CustomFieldValue, None]" |
| 19 | + reveal_type(user.my_custom_field14) # N: Revealed type is "Union[builtins.bool, None]" |
| 20 | + reveal_type(user.my_custom_field15) # N: Revealed type is "None" |
| 21 | + monkeypatch: true |
| 22 | + out: | |
| 23 | + myapp/models:31: error: GenericField is nullable but its generic get type parameter is not optional [misc] |
| 24 | + myapp/models:32: error: CustomValueField is nullable but its generic get type parameter is not optional [misc] |
| 25 | + myapp/models:33: error: SingleTypeField is nullable but its generic get type parameter is not optional [misc] |
| 26 | + myapp/models:34: error: AdditionalTypeVarField is nullable but its generic get type parameter is not optional [misc] |
| 27 | + myapp/models:35: error: Field is nullable but its generic get type parameter is not optional [misc] |
| 28 | + installed_apps: |
| 29 | + - myapp |
| 30 | + files: |
| 31 | + - path: myapp/__init__.py |
| 32 | + - path: myapp/models.py |
| 33 | + content: | |
| 34 | + from django.db import models |
| 35 | + from django.db.models import fields |
| 36 | +
|
| 37 | + from typing import Any, TypeVar, Generic, Union |
| 38 | +
|
| 39 | + _ST = TypeVar("_ST", contravariant=True) |
| 40 | + _GT = TypeVar("_GT", covariant=True) |
| 41 | +
|
| 42 | + T = TypeVar("T") |
| 43 | +
|
| 44 | + class CustomFieldValue: ... |
| 45 | +
|
| 46 | + class GenericField(fields.Field[_ST, _GT]): ... |
| 47 | +
|
| 48 | + class SingleTypeField(fields.Field[T, T]): ... |
| 49 | +
|
| 50 | + class CustomValueField(fields.Field[Union[CustomFieldValue, int], CustomFieldValue]): ... |
| 51 | +
|
| 52 | + class AdditionalTypeVarField(fields.Field[_ST, _GT], Generic[_ST, _GT, T]): ... |
| 53 | +
|
| 54 | + class CustomSmallIntegerField(fields.SmallIntegerField[_ST, _GT]): ... |
| 55 | +
|
| 56 | + class User(models.Model): |
| 57 | + id = models.AutoField(primary_key=True) |
| 58 | + my_custom_field1 = GenericField[Union[CustomFieldValue, int], CustomFieldValue]() |
| 59 | + my_custom_field2 = CustomValueField() |
| 60 | + my_custom_field3 = SingleTypeField[bool]() |
| 61 | + my_custom_field4 = AdditionalTypeVarField[Union[CustomFieldValue, int], CustomFieldValue, bool]() |
| 62 | +
|
| 63 | + # test null=True on fields with non-optional generic types throw error |
| 64 | + my_custom_field5 = GenericField[Union[CustomFieldValue, int], CustomFieldValue](null=True) |
| 65 | + my_custom_field6 = CustomValueField(null=True) |
| 66 | + my_custom_field7 = SingleTypeField[bool](null=True) |
| 67 | + my_custom_field8 = AdditionalTypeVarField[Union[CustomFieldValue, int], CustomFieldValue, bool](null=True) |
| 68 | + my_custom_field9 = fields.Field[Union[CustomFieldValue, int], CustomFieldValue](null=True) |
| 69 | +
|
| 70 | + # test overriding fields that set _pyi_private_set_type or _pyi_private_get_type |
| 71 | + my_custom_field10 = fields.SmallIntegerField[bool, bool]() |
| 72 | + my_custom_field11 = CustomSmallIntegerField[bool, bool]() |
| 73 | +
|
| 74 | + # test null=True on fields with non-optional generic types throw no errors |
| 75 | + my_custom_field12 = fields.Field[Union[CustomFieldValue, int], Union[CustomFieldValue, None]](null=True) |
| 76 | + my_custom_field13 = GenericField[Union[CustomFieldValue, int], Union[CustomFieldValue, None]](null=True) |
| 77 | + my_custom_field14 = SingleTypeField[Union[bool, None]](null=True) |
| 78 | + my_custom_field15 = fields.Field[None, None](null=True) |
0 commit comments