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
The clean method of django.db.models.Field takes a model_instance argument, which django-stubs annotates as Optional[Model]. Django's implementation of clean calls self.validate(value, model_instance), directly using the value of its model_instance argument in the call to validate. But the model_instance argument to validateis annotated as Model in django-stubs, which Optional[Model] isn't compatible with.
I ran into this issue when working on a Field subclass's clean method, reusing most of the logic from Field's default clean implementation. Mypy complained that a value of type Optional[Model] can't be used for validate's model_instance argument, since that argument's type is specified to be Model.
This should be reproducible by simply subclassing Field and copying Django's default implementation of Field.clean into your subclass.
How is that should be
I'm not sure whether the annotation type for model_instance on clean or validate is incorrect; I don't know enough about the internals of Django fields to definitively say which one should change. It's also possible that there's a bug in Django here that these annotations have revealed.
System information
Per the source code links above, this issue appears to be present in the latest versions of Django and django-stubs, but the versions I'm using locally are:
OS: macOS 11.6
python version: 3.8.11
django version: 3.1.13
mypy version: 0.812
django-stubs version: 1.8.0
django-stubs-ext version: n/a
The text was updated successfully, but these errors were encountered:
Bug report
What's wrong
The
clean
method ofdjango.db.models.Field
takes amodel_instance
argument, which django-stubs annotates asOptional[Model]
. Django's implementation ofclean
callsself.validate(value, model_instance)
, directly using the value of itsmodel_instance
argument in the call tovalidate
. But themodel_instance
argument tovalidate
is annotated asModel
in django-stubs, whichOptional[Model]
isn't compatible with.I ran into this issue when working on a
Field
subclass'sclean
method, reusing most of the logic fromField
's defaultclean
implementation. Mypy complained that a value of typeOptional[Model]
can't be used forvalidate
'smodel_instance
argument, since that argument's type is specified to beModel
.This should be reproducible by simply subclassing
Field
and copying Django's default implementation ofField.clean
into your subclass.How is that should be
I'm not sure whether the annotation type for
model_instance
onclean
orvalidate
is incorrect; I don't know enough about the internals of Django fields to definitively say which one should change. It's also possible that there's a bug in Django here that these annotations have revealed.System information
Per the source code links above, this issue appears to be present in the latest versions of Django and django-stubs, but the versions I'm using locally are:
python
version: 3.8.11django
version: 3.1.13mypy
version: 0.812django-stubs
version: 1.8.0django-stubs-ext
version: n/aThe text was updated successfully, but these errors were encountered: