Skip to content

Commit ad44a40

Browse files
committed
allow to use fields as Field objects outside Model classes
1 parent 3a9e840 commit ad44a40

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

django-stubs/db/models/fields/__init__.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,12 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
9090
# class access
9191
@overload
9292
def __get__(self: _T, instance: None, owner) -> _T: ...
93-
# instance access
93+
# Model instance access
9494
@overload
95-
def __get__(self, instance, owner) -> _GT: ...
95+
def __get__(self, instance: Model, owner) -> _GT: ...
96+
# non-Model instances
97+
@overload
98+
def __get__(self: _T, instance, owner) -> _T: ...
9699
def deconstruct(self) -> Any: ...
97100
def set_attributes_from_name(self, name: str) -> None: ...
98101
def db_type(self, connection: Any) -> str: ...

test-data/typecheck/fields/test_base.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,12 @@
123123
content: |
124124
from django.db import models
125125
class MyUser(models.Model):
126-
name = models.CharField(max_length=100)
126+
name = models.CharField(max_length=100)
127+
128+
- case: fields_on_non_model_classes_resolve_to_field_type
129+
main: |
130+
from django.db import models
131+
class MyClass:
132+
myfield: models.IntegerField[int, int]
133+
reveal_type(MyClass.myfield) # N: Revealed type is 'django.db.models.fields.IntegerField[builtins.int, builtins.int]'
134+
reveal_type(MyClass().myfield) # N: Revealed type is 'django.db.models.fields.IntegerField[builtins.int, builtins.int]'

0 commit comments

Comments
 (0)