-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
AbstractUser lacks Meta-attribute #2112
Comments
@jorenham This is caused by #2000, any thoughts? I think we should do something here. I don't understand why for example >>> from django.contrib.auth.models import Permission
>>> Permission.Meta
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: type object 'Permission' has no attribute 'Meta'. Did you mean: '_meta'?
>>> from django.contrib.auth.models import AbstractUser
>>> AbstractUser.Meta
<class 'django.contrib.auth.models.AbstractUser.Meta'> Even though model class |
So there are two ways to define the metaclass of a model that inherits from another model (it doesn't matter if it's abstract or not). So given class Dad(models.Model):
class Meta: ... Option 1class Child(Dad):
class Meta: ... This is what Advantages:
Disadvantages:
Option 2class Child(Dad):
class Meta(Dad.Meta): ... Advantages:
Disadvantages:
From a typing perspective, option 2 seems to be the least incorrect. But perhaps option 1 might be easier: I suspect (based on a vague recollection of 3rd party code of django plugins I've seen over the years) that most django users aren't using Meta inheritance very often. Personally, I'd prefer option 2. But keep in mind that I'm the kind of person that can watch a football match without saying a word, but starts cursing out loud after seeing that |
Not too sure iff Anyway, in my opinion option 2 must be supported, as it is the one suggested by django docs - although it sure is not used too often, when used according to docs there should not be faults given by the typechecker for perfectly working code. |
@prauscher |
Sorry, I only read now how my comment could be miss-interpreted: I was just speculating iff |
@prauscher Ah I see now. But I believe that this issue in particular is caused by the fact that |
Anyway, let's just see which classes have I created a hack script to enumerate all model classes and check if they have Meta. https://gist.github.com/intgr/352cac61b4f95e921bb33a0d4b76d324 Results...
|
I've also observed the same issue with forms, notably trying to use a custom from django.contrib.auth.forms import BaseUserCreationForm
from .models import User
class UserCreationForm(BaseUserCreationForm):
class Meta(BaseUserCreationForm.Meta): # error: Name "BaseUserCreationForm.Meta" is not defined [name-defined]
model = User
fields = ["email"]
Can we do the same for the Form classes? (I couldn't really follow why they were removed in #2000) |
If But it could very well be that we missed something in #2000, or that since then, django has changed this. Either way, iff
Yea to me it all feels rather inconsistent; it would've saved us a lot of trouble if all models would have had a
I think that the there are only two viable options for dealing with this
|
Bug report
What's wrong
Since version 5.0.0 (probably since #2000), the following code fails:
obviously this example is shortened, but I think you get the Idea. While it is true that a basic
Model
does not have aMeta
-attribute, this is not valid for basically any Subclass for Meta, and using this inheritance is actually the recommended way by django docs:How is that should be
AbstractUser.Meta
should exist for inheritance.System information
python
version: 3.12django
version: 5.0.4mypy
version: 1.10.0django-stubs
version: 5.0.0django-stubs-ext
version: noneThe text was updated successfully, but these errors were encountered: