You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fields on non-model classes are resolving to the return type as if they were on a model. Use case is binding the field with other data in order to access its field name and to_python methods while doing data collection.
from typing import Generic, Text, TypeVar
from django.db import models
A = TypeVar('A')
B = TypeVar('B')
def field_name(field: models.Field[A, B]) -> Text:
reveal_type(field) # Revealed type is 'django.db.models.fields.Field[A`-1, B`-2]
return field.name
class BoundWithField(Generic[A, B]):
field: models.Field[A, B]
def field_name(self) -> Text:
reveal_type(self.field) # Revealed type is 'B`2
return self.field.name # "B" has no attribute "name"
The text was updated successfully, but these errors were encountered:
At the moment, the stubs represent the most common scenario of fields being instantiated and assigned to attributes inside of model classes.
To make that work, Field in the stubs implements the descriptor protocol (e.g. has a __get__ method).
It's not exactly the truth of how it works in Django (after some poking through the code). Field in Django itself isn't a descriptor.
When a model is instantiated, Django's ModelBase metaclass actually adds the field to the class wrapped in a django.db.models.query_utils.DeferredAttribute, which is a descriptor.
We could choose to represent this in the stubs, and make them "less-usable" in the model field definition scenario if the stubs are used without the mypy django plugin (e.g. with another type-checker). In that case, the mypy django plugin could handle the job of wrapping the attribute in the descriptor on the model when it is defined.
Fields on non-model classes are resolving to the return type as if they were on a model. Use case is binding the field with other data in order to access its field name and to_python methods while doing data collection.
The text was updated successfully, but these errors were encountered: