allow to use fields as Field objects outside Model classes

This commit is contained in:
Maxim Kurnikov
2019-07-20 23:08:33 +03:00
parent df77299c2f
commit 6962b42cba
2 changed files with 14 additions and 3 deletions

View File

@@ -90,9 +90,12 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
# class access
@overload
def __get__(self: _T, instance: None, owner) -> _T: ...
# instance access
# Model instance access
@overload
def __get__(self, instance, owner) -> _GT: ...
def __get__(self, instance: Model, owner) -> _GT: ...
# non-Model instances
@overload
def __get__(self: _T, instance, owner) -> _T: ...
def deconstruct(self) -> Any: ...
def set_attributes_from_name(self, name: str) -> None: ...
def db_type(self, connection: Any) -> str: ...

View File

@@ -123,4 +123,12 @@
content: |
from django.db import models
class MyUser(models.Model):
name = models.CharField(max_length=100)
name = models.CharField(max_length=100)
- case: fields_on_non_model_classes_resolve_to_field_type
main: |
from django.db import models
class MyClass:
myfield: models.IntegerField[int, int]
reveal_type(MyClass.myfield) # N: Revealed type is 'django.db.models.fields.IntegerField[builtins.int, builtins.int]'
reveal_type(MyClass().myfield) # N: Revealed type is 'django.db.models.fields.IntegerField[builtins.int, builtins.int]'