mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-16 00:37:11 +08:00
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:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user