Skip to content

Multiple inheritance error when using slots #667

Closed

Description

@jacobg

The following classes definitions causes an error:

    @attr.s(auto_attribs=True, frozen=True, slots=True, collect_by_mro=True)
    class Foo:
        foo: str

    @attr.s(auto_attribs=True, frozen=True, slots=True, collect_by_mro=True)
    class Bar:
        bar: str

    @attr.s(auto_attribs=True, frozen=True, slots=True, collect_by_mro=True)
    class Baz(Foo, Bar):
        baz: str
>       class Baz(Foo, Bar):
E       TypeError: multiple bases have instance lay-out conflict

Note that this error occurs on master with a commit that seems to purport to fix this problem:
#635

Removing the slots decorator kwargs resolves the error.

Is there a workaround that supports slots?

Activity

  1. changed the title [-]Multiple inheritance error[/-] [+]Multiple inheritance error when using slots[/+] on Aug 18, 2020
  2. hynek commented on Aug 18, 2020

    @hynek
    Member

    To be clear: #635 has nothing to do with this problem and the problem exists in 19.3 too.

    I suspect it's related to the way we create slotted classes in

    cls = type(self._cls)(self._cls.__name__, self._cls.__bases__, cd)

    Since slots become True by default in NG, we should have a look if we can fix this.

  3. added this to the 20.1.0 milestone on Aug 18, 2020
  4. Tinche commented on Aug 18, 2020

    @Tinche
    Member

    An even smaller test case:

    import attr
    
    
    @attr.s(slots=True)
    class A:
        a = attr.ib()
    
    
    @attr.s(slots=True)
    class B:
        b = attr.ib()
    
    
    @attr.s(slots=True)
    class C(A, B):
        pass
    
    
  5. Tinche commented on Aug 18, 2020

    @Tinche
    Member

    Actually, C doesn't even have to be an attrs class:

    import attr
    
    
    @attr.s(slots=True)
    class A:
        a = attr.ib()
    
    
    @attr.s(slots=True)
    class B:
        b = attr.ib()
    
    
    class C(A, B):
        pass
    

    In fact, browsing the Python docs (https://docs.python.org/3/reference/datamodel.html#notes-on-using-slots):

    Multiple inheritance with multiple slotted parent classes can be used, but only one parent is allowed to have attributes created by slots (the other bases must have empty slot layouts) - violations raise TypeError.

    So this is a Python limitation.

  6. hynek commented on Aug 19, 2020

    @hynek
    Member

    Thank you for the research Tin! I will add an entry to the glossary.

  7. jacobg commented on Aug 19, 2020

    @jacobg
    Author

    @Tinche @hynek Thank you for looking into this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions