This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ariebovenberg
Recipients ariebovenberg
Date 2022-01-14.19:31:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1642188703.34.0.911855592293.issue46382@roundup.psfhosted.org>
In-reply-to
Content
@dataclass(slots=True) adds slots to dataclasses. It adds a slot per field. 
However, it doesn't account for slots already present in base classes:

>>> class Base:
...     __slots__ = ('a', )
...
>>> @dataclass(slots=True)
... class Foo(Base):
...     a: int
...     b: float
...
>>> Foo.__slots__
('a', 'b')  # should be: ('b', )


The __slots__ documentation says:

    If a class defines a slot also defined in a base class, the instance variable 
    defined by the base class slot is inaccessible (except by retrieving its descriptor 
    directly from the base class). This renders the meaning of the program undefined. 
    In the future, a check may be added to prevent this.

Solution: don't add slots which are already defined in any base classes:

>>> @dataclass
... class Bla(Base):
...     __slots__ = ('b', )
...     a: int
...     b: float
...
>>> Bla(4, 5.65)
Bla(a=4, b=5.65)

If you agree, I'd like to submit a PR to fix this. I already have a prototype working.
History
Date User Action Args
2022-01-14 19:31:43ariebovenbergsetrecipients: + ariebovenberg
2022-01-14 19:31:43ariebovenbergsetmessageid: <1642188703.34.0.911855592293.issue46382@roundup.psfhosted.org>
2022-01-14 19:31:43ariebovenberglinkissue46382 messages
2022-01-14 19:31:43ariebovenbergcreate