django-stubs
django-stubs copied to clipboard
error: Definition of "_meta" in base class "PermissionsMixin" is incompatible with definition in base class "AbstractBaseUser"
Bug report
What's wrong
We have defined a custom AuthUser
model in our project like:
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
class AuthUser(PermissionsMixin, AbstractBaseUser):
class Meta:
verbose_name = _('Auth User')
abstract = False
db_table = 'auth_user'
mypy gives an error:
myproject/models/auth.py:3: error: Definition of "_meta" in base class "PermissionsMixin" is incompatible with definition in base class "AbstractBaseUser"
(from the class AuthUser(PermissionsMixin, AbstractBaseUser):
line)
The slightly weird part is Django's PermissionMixin
and AbstractBaseUser
both have an identical Meta
:
class Meta:
abstract = True
How is that should be
No error
System information
- OS:
-
python
version: 3.8.3 -
django
version: 2.2.12 -
mypy
version: 0.782 -
django-stubs
version: 1.5.0
@anentropic I'm not able to reproduce this bug, could you post a result of reveal_type(AuthUser)
and mypy --tb .myproject/models/auth.py
?
myproject/models/auth.py:19: error: Definition of "_meta" in base class "PermissionsMixin" is incompatible with definition in base class "AbstractBaseUser"
myproject/models/auth.py:111: note: Revealed type is 'def (*args: Any, **kwargs: Any) -> myproject.models.auth.AuthUser'
@anentropic Shouldn't you use from django.contrib.auth.base_user import AbstractBaseUser
? Or maybe AbstractUser
from django.contrib.auth.models
would do the job?
importing the same class as from django.contrib.auth.base_user import AbstractBaseUser
does not make a difference in the mypy error
we couldn't use AbstractUser
as it defines stuff we don't want
Ok, so I'm out of ideas at this point, sorry. Maybe @sobolevn will come up with something?
I would also love to see ._meta
attributes of all AbstractBaseUser
, PermissionsMixin
, and your type.
Thanks a lot for the help! 👍
if TYPE_CHECKING:
reveal_type(AuthUser._meta)
reveal_type(PermissionsMixin._meta)
reveal_type(AbstractBaseUser._meta)
myproject/models/auth.py:111: note: Revealed type is 'django.db.models.options.Options[django.contrib.auth.models.PermissionsMixin]'
myproject/models/auth.py:112: note: Revealed type is 'django.db.models.options.Options[django.contrib.auth.models.PermissionsMixin]'
myproject/models/auth.py:113: note: Revealed type is 'django.db.models.options.Options[django.contrib.auth.base_user.AbstractBaseUser]'
We can try to tweak the variance of _meta
to be contravariant
. My wild guess that this can be the case (I am totally not sure).
if you have a branch with the contravariant change at some point I'll be happy to try it out on my problem Django project
@anentropic According to https://code.djangoproject.com/ticket/31437 (thanks to @syastrov for the link):
(...) overriding multiply inherited fields has never been supported
So, it seems like # type: ignore
is the only solution (maybe?) I've spent quite a lot of time trying to fix the error coming from the piece of code mentioned in the link above as it was the last one failing in CI (see my PR #445).
@sobolevn we definitely need to update Django version we test on more often 😂
@kszmigiel I'm not saying it's not somehow... but it's not clear to me if that issue (about overriding fields of abstract models in multiple inheritance scenario) is related to this error
presumably Django itself has the same problem since their AbstractUser
class looks like:
class AbstractUser(AbstractBaseUser, PermissionsMixin):
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
abstract = True
Reproduced:
git clone https://github.com/syastrov/django-stubs-471.git
cd django-stubs-471
python3 -m venv venv
source ./venv/bin/activate
pip3 install -r requirements.txt
mypy app1/
Output
authtest/settings.py:28: error: Need type annotation for 'ALLOWED_HOSTS' (hint: "ALLOWED_HOSTS: List[<type>] = ...")
app1/models.py:4: error: Definition of "_meta" in base class "PermissionsMixin" is incompatible with definition in base class "AbstractBaseUser"
Found 2 errors in 2 files (checked 7 source files)
Disabling the plugin removes the error...
@syastrov thank you! I'll get back to this as soon as I get back home from holidays.
Hey all, is there any update for this issue? I'm having the same problem.
After upgrading mypy to 0.800
this issue was resolved for me.
I'm on mypy version 0.931
and I'm having this same issue.
I'm having this issue to. Is there a fix?
mypy 0.950
django-stubs 1.11.0
Still having this issue
I'm still having this issue too. How can we fix it? Or at least set it to ignore if you aren't going to.
is there a workaround for this?