Skip to content

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

Closed

Description

@syenchuk

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

Activity

  1. gvanrossum commented on Feb 15, 2017

    @gvanrossum
    Member
  2. JukkaL commented on Feb 16, 2017

    @JukkaL
    Collaborator

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

  3. syenchuk commented on Feb 16, 2017

    @syenchuk
    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.

  4. gvanrossum commented on Feb 16, 2017

    @gvanrossum
    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.

  5. JukkaL commented on Feb 24, 2017

    @JukkaL
    Collaborator

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

  6. syenchuk commented on Nov 8, 2018

    @syenchuk
    Author

    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.

  7. ilevkivskyi commented on Nov 19, 2018

    @ilevkivskyi
    Member

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

  8. ilevkivskyi commented on Nov 19, 2018

    @ilevkivskyi
    Member

    @mkurnikov you might want to try fixing this.

  9. ilevkivskyi commented on Jan 9, 2019

    @ilevkivskyi
    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions