Skip to content

TypeError: with a diamond-like multiple inheritance #1116

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

Closed
roynico opened this issue Apr 3, 2023 · 2 comments
Closed

TypeError: with a diamond-like multiple inheritance #1116

roynico opened this issue Apr 3, 2023 · 2 comments

Comments

@roynico
Copy link

roynico commented Apr 3, 2023

Hello,

I encounter a problem when trying to use attrs class with a diamond-like inheritance:
The error :

...  class D(B, C):
TypeError: multiple bases have instance lay-out conflict

Minimal Example:

from attrs import define, field

@define
class A:
    a: str = field()

@define
class B(A):
    b: str = field()

@define
class C(A):
    c: str = field()

@define
class D(B, C):
    d: str = field()


I do not understand why there should be any conflict in this example.

@hynek
Copy link
Member

hynek commented Apr 3, 2023

This is because @define uses slots by default and it's not possible to inherit from more than one slotted class.

See also our glossary: https://www.attrs.org/en/stable/glossary.html#term-slotted-classes

Usually that's not a problem, because diamond-like inheritance is generally considered bad design. If you want to force it, pass (slots=False) to the last define.

@hynek hynek closed this as completed Apr 3, 2023
@roynico
Copy link
Author

roynico commented Apr 3, 2023

Many thanks for the quick answer !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants