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
However, as of django-stubs 4.2.4 (apparently via #1663), django-stubs now claims that these generic base exception classes are not defined:
error: "type[Model]" has no attribute "DoesNotExist" [attr-defined]
error: "type[Model]" has no attribute "MultipleObjectsReturned" [attr-defined]
How is that should be
models.Model.DoesNotExist and models.Model.MultipleObjectsReturned are both real/defined exception classes that can be used and are needed/useful in generic code where the specific model class is not known.
Those attributes were apparently marked as "abstract" per comment here #1653 (comment) and are only available via django-stubs for non-abstract models, which is not technically correct and limits real-world usage of these base exception classes.
System information
OS: MacOS 13.5
python version: 3.10
django version: 3.2.21
mypy version: 1.5.0
django-stubs version: 4.2.4
django-stubs-ext version: 4.2.2
The text was updated successfully, but these errors were encountered:
Attributes models.Model.DoesNotExist and models.Model.MultipleObjectsReturned do not exist.
The metaclass ModelBase has a guard that exits early, found here for the type models.Model. Whereas you can see these attributes being added by the metaclass later on for concrete models here.
If you try to inspect those two attributes at runtime, you'll notice they don't exist
fromdjango.dbimportmodelsmodels.Model.DoesNotExist# AttributeError: type object 'Model' has no attribute 'DoesNotExist'models.Model.MultipleObjectsReturned# AttributeError: type object 'Model' has no attribute 'MultipleObjectsReturned'
However, what you should instead use as generic exceptions to catch are the base exception classes django.core.exceptions.ObjectDoesNotExist and django.core.exceptions.MultipleObjectsReturned.
Ah indeed, my bad. Thanks for the quick response and clarification. I guess the code in our project that referenced those exception classes was never executed in practice so I assumed incorrectly that they did indeed exist (with VSCode showing them as existing thanks to the stubs, despite them being "abstract"). 👍
Bug report
What's wrong
In writing generic code that needs to handle potentially different models, it's useful to be able to catch general exceptions with:
However, as of django-stubs 4.2.4 (apparently via #1663), django-stubs now claims that these generic base exception classes are not defined:
error: "type[Model]" has no attribute "DoesNotExist" [attr-defined]
error: "type[Model]" has no attribute "MultipleObjectsReturned" [attr-defined]
How is that should be
models.Model.DoesNotExist
andmodels.Model.MultipleObjectsReturned
are both real/defined exception classes that can be used and are needed/useful in generic code where the specific model class is not known.Those attributes were apparently marked as "abstract" per comment here #1653 (comment) and are only available via django-stubs for non-abstract models, which is not technically correct and limits real-world usage of these base exception classes.
System information
python
version: 3.10django
version: 3.2.21mypy
version: 1.5.0django-stubs
version: 4.2.4django-stubs-ext
version: 4.2.2The text was updated successfully, but these errors were encountered: