Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible argument types for django.db.models.Field.{clean,validate} #718

Open
rolandcrosby-check opened this issue Sep 22, 2021 · 0 comments
Labels
bug Something isn't working

Comments

@rolandcrosby-check
Copy link
Contributor

Bug report

What's wrong

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 validate is 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
@rolandcrosby-check rolandcrosby-check added the bug Something isn't working label Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

1 participant