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

Multiple inheritance: Cannot determine type of "X" in base class "Y" error #2871

Closed
sacha-senchuk opened this issue Feb 15, 2017 · 9 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high topic-usability

Comments

@sacha-senchuk
Copy link

sacha-senchuk commented Feb 15, 2017

This is probably more of a question than a real issue.

The following code is considered erroneous (tested in Python 3):

class BaseA:
    class Sub:
        pass

class BaseB:
    class Sub:
        pass

class Test(BaseA, BaseB):
    pass

mypy responds with:

…: error: Cannot determine type of 'Sub' in base class 'BaseA'
…: error: Cannot determine type of 'Sub' in base class 'BaseB'

But it doesn't complain if I don't use multiple inheritance:

class BaseA:
    class Sub:
        pass

class BaseB:
    class Sub:
        pass

class Test(BaseA):
    pass
@gvanrossum
Copy link
Member

gvanrossum commented Feb 15, 2017 via email

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 16, 2017

Based on the error message, it looks likely that the multiple inheritance validity check doesn't expect to see nested classes and gets confused.

@sacha-senchuk
Copy link
Author

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.

@gvanrossum
Copy link
Member

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.

@JukkaL JukkaL added bug mypy got something wrong priority-1-normal labels Feb 24, 2017
@JukkaL
Copy link
Collaborator

JukkaL commented Feb 24, 2017

Marked as a bug, because the error message is confusing.

@sacha-senchuk
Copy link
Author

sacha-senchuk commented Nov 8, 2018

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.

@ilevkivskyi
Copy link
Member

(Raising priority to high since this is a very common idiom in Django.)

@ilevkivskyi
Copy link
Member

@mkurnikov you might want to try fixing this.

ilevkivskyi pushed a commit that referenced this issue Jan 9, 2019
…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.
@ilevkivskyi
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high topic-usability
Projects
None yet
Development

No branches or pull requests

4 participants