Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cannot specifyweakref_slot=True twice in dataclass MRO #93521

Closed
Assignees
ericvsmith
Labels
3.11only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@Bluenix2

Description

@Bluenix2

Summary

If a child dataclass of a parent dataclass who specifiedweakref_slot, also decides to setweakref_slot to True, Python will raise a TypeError as seen below:

TypeError: __weakref__ slot disallowed: either we already got one, or __itemsize__ != 0

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 theweakref_slot because 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:

fromdataclassesimportdataclass@dataclass(slots=True,weakref_slot=True)classA:field:str@dataclass(slots=True,weakref_slot=True)classB(A):    ...
Traceback (most recent call last):  File "<stdin>", line 2, in <module>  File "C:\Users\%username%\Projects\testing\copyclasses.py", line 1207, in wrap    return _process_class(cls, init, repr, eq, order, unsafe_hash,  File "C:\Users\%username%\Projects\testing\copyclasses.py", line 1108, in _process_class    cls = _add_slots(cls, frozen, weakref_slot)  File "C:\Users\%username%\Projects\testing\copyclasses.py", line 1176, in _add_slots        cls = type(cls)(cls.__name__, cls.__bases__, cls_dict)TypeError: __weakref__ slot disallowed: either we already got one, or __itemsize__ != 0

Environment

This was tested by creating a file namedcopyclasses.py with the current contents ofLib/dataclasses.py ran 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.

Metadata

Metadata

Assignees

Labels

3.11only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp