Repository navigation
Cannot specify weakref_slot=True twice in dataclass MRO #93521
Copy link
Copy link
Closed
Labels
3.11only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jun 5, 2022 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Jun 5, 2022 Here's an example of the same thing without dataclasses:
class A: __slots__='__weakref__' class B(A): __slots__='__weakref__'
Traceback (most recent call last): File "gh-93521.py", line 4, in <module> class B(A): TypeError: __weakref__ slot disallowed: either we already got one, or __itemsize__ != 0I'm not sure this is a bug, just the way things work.
It would be nice if dataclasses took care of this issue automatically though.
Example scenario: I have a library in which I expose a dataclass. A user subclasses my class in another dataclass, and wants to use weakrefs, so they use
weakref_slots. Then I change my library to allow weakrefs on the base class, but now the user's child class is broken.Locally I fixed this already, and I am planning on opening a PR with an added test. Should that PR point to
mainor the3.11branch (seeing as both branches should get the patch)?main
Reacted by Bluenix- added a commit that references this issue
on Jun 6, 2022 - added a commit that references this issue
on Jun 8, 2022 - added a commit that references this issue
on Jun 26, 2022
Metadata
Metadata
Assignees
Labels
3.11only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Summary
If a child dataclass of a parent dataclass who specified
weakref_slot, also decides to setweakref_slotto True, Python will raise a TypeError as seen below:Description
I am porting dataclasses' slots and weakref slots kwargs into a decorator I can use. While doing so, I decided to change the default of the
weakref_slotbecause I would like all of my classes to be weakref:able. This is when I ran into the issue which can be reproduced by the code below:Environment
This was tested by creating a file named
copyclasses.pywith the current contents ofLib/dataclasses.pyran using a 3.10 CPython interpreter. Note that I've restored the naming of the module in the codeblock above assuming those who will be running it will have a built 3.11 CPython interpreter.Footnotes: I will mention @ericvsmith as the code owner of the dataclasses file.