Fix Field arguments variance (#573)

* Fix Field arguments variance

* fixup! Fix Field arguments variance

Test field can be used as bsse type
This commit is contained in:
Anton Agestam
2021-03-07 11:32:56 +01:00
committed by GitHub
parent 6a6fd47f28
commit 124d90794c
2 changed files with 17 additions and 3 deletions

View File

@@ -38,9 +38,9 @@ _ErrorMessagesToOverride = Dict[str, Any]
_T = TypeVar("_T", bound="Field")
# __set__ value type
_ST = TypeVar("_ST")
_ST = TypeVar("_ST", contravariant=True)
# __get__ return type
_GT = TypeVar("_GT")
_GT = TypeVar("_GT", covariant=True)
class Field(RegisterLookupMixin, Generic[_ST, _GT]):
_pyi_private_set_type: Any

View File

@@ -150,4 +150,18 @@
username = models.CharField(max_length=100)
class MyModel(AuthMixin, models.Model):
pass
pass
- case: can_narrow_field_type
main: |
from typing import cast, NewType
from django.db import models
Year = NewType("Year", int)
class Book(models.Model):
published = cast(models.Field[Year, Year], models.IntegerField())
book = Book()
reveal_type(book.published) # N: Revealed type is 'main.Year*'
book.published = 2006 # E: Incompatible types in assignment (expression has type "int", variable has type "Year")
book.published = Year(2006)
reveal_type(book.published) # N: Revealed type is 'main.Year*'
def accepts_int(arg: int) -> None: ...
accepts_int(book.published)