-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Multiple inheritance: Cannot determine type of "X" in base class "Y" error #2871
Comments
I think just the error is a bit confusing; in the M.I. case it is trying to
point out that you are inheriting two unrelated class attributes named
'Sub'.
|
Based on the error message, it looks likely that the multiple inheritance validity check doesn't expect to see nested classes and gets confused. |
I have the impression that it gets confused, because the following code passes without errors: class BaseA:
sub = 1
class BaseB:
sub = 1
class Test(BaseA, BaseB):
pass I don't really understand what's wrong with nested classes. |
Well, for one thing, two integers always have the same type. But two independently declared classes (even if they have the same name and contents) are not considered to have the same type. |
Marked as a bug, because the error message is confusing. |
As a side note, I stumbled upon this issue by trying to use two different mixins in Django: class CreatedDateMixin(models.Model):
date_created = models.DateTimeField(_("created on"), auto_now_add=True)
class Meta:
abstract = True
class UpdatedDateMixin(models.Model):
date_updated = models.DateTimeField(_("updated on"), auto_now=True)
class Meta:
abstract = True
class Foo(CreatedDateMixin, UpdatedDateMixin, models.Model):
name = models.CharField(_("name"), max_length=255, blank=True)
# error: Cannot determine type of 'Meta' in base class 'CreatedDateMixin'
# error: Cannot determine type of 'Meta' in base class 'UpdatedDateMixin' Not exactly a bug, but I don't know how to fix this… And I feel like others might easily stumble upon this issue as well. |
(Raising priority to high since this is a very common idiom in Django.) |
@mkurnikov you might want to try fixing this. |
…e accurate (#5926) Fixes #2871. Initially, in discussion with @ilevkivskyi in Gitter, he suggested to just remove error, if there are two nested classes in a multiple inheritance with the same name ``` class Mixin1: class Meta: pass class Mixin2: class Meta: pass class A(Mixin1, Mixin2): pass ``` However, later we decided to make it safe and emit a better error message, including for cases with nested class and non-class for obvious cases. Note that for class objects we ignore the `__init__` method signature of nested class and only check subclassing relationship between them.
Note that mypy still emits an error on the original example, because it is technically unsafe. Note however that @mkurnikov works on a mypy plugin for Django that will suppress this error for Django specific use cases. |
This is probably more of a question than a real issue.
The following code is considered erroneous (tested in Python 3):
mypy responds with:
But it doesn't complain if I don't use multiple inheritance:
The text was updated successfully, but these errors were encountered: