You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Misleading "Not an instance or subtype" error when assigning to property of frozen, slotted dataclass #143969
File "test_dataclass.py", line 12, in <module>
example.prop = 0
^^^^^^^^^^^^
File "<string>", line 15, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'prop'
And when the class is slotted, if the property has no setter, it also fails the way one would expect:
Traceback (most recent call last):
File "test_dataclass.py", line 12, in <module>
example.prop = 0
^^^^^^^^^^^^
AttributeError: property 'prop' of 'Example' object has no setter
(In this case, of course, adding a setter makes it work fine.)
What's unexpected (and confusing) is when the class is both frozen and slotted:
Traceback (most recent call last):
File "test_dataclass.py", line 12, in <module>
example.prop = 0
^^^^^^^^^^^^
File "<string>", line 16, in __setattr__
TypeError: super(type, obj): obj (instance of Example) is not an instance or subtype of type (Example).
One would, of course, expect an instance of Example to in fact be an instance of Example, but maybe this is somehow a collision between the initial vs. dataclass-ified class definitions?
@carljm : Yes, I think it's related to how #90562 modifies cells, but I can't noodle through exactly what the problem is. I'll try to give it some attention, but it's crazy complicated and as far as I can tell the best outcome is a less confusing error message.
» ./python.exe ex.py
Traceback (most recent call last):
File "/Users/sobolev/Desktop/cpython/ex.py", line 17, in <module>
example.prop = 0
^^^^^^^^^^^^
File "<string>", line 15, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'prop'
Edit:
Turns out I was unexpectedly running patch version 3.14.4, which was a version before the below patch hit release. Seems to work fine on 3.14.6 & 3.14.7 (the two versions I re-tested). I refactored my dynamic patch and have 3.10.x to 3.14.x working fine now. You can probably disregard my original comment below.
I can verify this is also an issue in CPython 3.14.x. Appears to me that #105936 fixed most of the issue, but left out property and property sub-classed decorated methods. I am currently working around it with a custom base class that intercepts __setattr__ and __delattr__, sending the calls back through the properties' __set__ and __delete__ methods.
FWIW: the issue is that slots=True creates a brand new class and the __setattr__ and __delattr__ closures created by frozen=True still point to the original class:
Activity
pythongh-143969: Fix frozen+slotted dataclass __setattr__ crash ambig…
pythongh-143969: Add NEWS entry for frozen slotted dataclass fix
pythongh-143969: Add NEWS entry for frozen slotted dataclass fix
pythongh-143969: Add NEWS entry for frozen slotted dataclass fix
pythongh-143969: Fix crash in frozen slots dataclasses by patching cl…
carljm commentedon May 18, 2026
This looks like #90562 -- maybe a case that was missed in the PR that closed that issue? cc @ericvsmith
ericvsmith commentedon May 19, 2026
@carljm : Yes, I think it's related to how #90562 modifies cells, but I can't noodle through exactly what the problem is. I'll try to give it some attention, but it's crazy complicated and as far as I can tell the best outcome is a less confusing error message.
HalfWhitt commentedon May 19, 2026
Functioning correctly / intuitively would be ideal, but yeah, a clear error message would definitely be an improvement!
sobolevn commentedon May 19, 2026
3.15andmainversions already produce:And changing error message for 3.14 does not feel right at this moment :(
sobolevn commentedon May 19, 2026
However, it is still not ideal: it says that
propis a field, while it is a property, not a field, and is not listed indataclasses.fields(Example).produces:
chet-manley commentedon Aug 15, 2026
Edit:
Turns out I was unexpectedly running patch version
3.14.4, which was a version before the below patch hit release. Seems to work fine on3.14.6&3.14.7(the two versions I re-tested). I refactored my dynamic patch and have3.10.xto3.14.xworking fine now. You can probably disregard my original comment below.I can verify this is also an issue in CPython 3.14.x. Appears to me that #105936 fixed most of the issue, but left out property and property sub-classed decorated methods. I am currently working around it with a custom base class that intercepts
__setattr__and__delattr__, sending the calls back through the properties'__set__and__delete__methods.FWIW: the issue is that
slots=Truecreates a brand new class and the__setattr__and__delattr__closures created byfrozen=Truestill point to the original class: