Uh oh!
There was an error while loading. Please reload this page.
gh-105936: Properly update closure cells for __setattr__ and __delattr__ in frozen dataclasses with slots - #144021
Conversation
JelleZijlstra
commented
Jan 19, 2026
Thanks! For the test you can do something like that in the original fix, https://github.com/python/cpython/pull/137047/files#diff-212e368b34eb9b134f87e765787d6d26b747235a358e13da8922f83861c0d676 . |
fdc81bb to
a6de29bCompare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as outdated.
This comment was marked as outdated.
__setattr__ and __detattr__ in frozen dataclasses with slots__setattr__ and __detattr__ in frozen dataclasses with slotsUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Prometheus3375
commented
Jan 24, 2026
While this PR is not merged, should AKCS be updated? |
JelleZijlstra
commented
Jan 25, 2026
If I remember correctly the latest policy on Misc/ACKS is that people can add themselves if they want but it's not required. The file is a pain to keep fully correct and sort of redundant with |
__setattr__ and __detattr__ in frozen dataclasses with slots__setattr__ and __delattr__ in frozen dataclasses with slotsThanks @Prometheus3375 for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
…`__delattr__` in frozen dataclasses with slots (pythonGH-144021) (cherry picked from commit 8a398bf) Co-authored-by: Sviataslau <35541026+Prometheus3375@users.noreply.github.com>
Sorry, @Prometheus3375 and @gpshead, I could not cleanly backport this to |
GH-148469 is a backport of this pull request to the 3.14 branch. |
…`__delattr__` in frozen dataclasses with slots (pythonGH-144021) (cherry picked from commit 8a398bf) Co-authored-by: Sviataslau <35541026+Prometheus3375@users.noreply.github.com>
… `__delattr__` in frozen dataclasses with slots (GH-144021) (#148469) gh-105936: Properly update closure cells for `__setattr__` and `__delattr__` in frozen dataclasses with slots (GH-144021) (cherry picked from commit 8a398bf) Co-authored-by: Prometheus3375 <prometheus3375@gmail.com> Co-authored-by: Sviataslau <35541026+Prometheus3375@users.noreply.github.com>
…_` and `__delattr__` in frozen dataclasses with slots (pythonGH-144021) pythongh-105936: Properly update closure cells for `__setattr__` and `__delattr__` in frozen dataclasses with slots (pythonGH-144021) (cherry picked from commit 8a398bf) The cherry-pick required additional changes beyond the original commit because 3.13 lacks the `__class__` closure cell fixup machinery that was added in 3.14 by pythonGH-124455 (pythongh-90562). Specifically: - Backported `_update_func_cell_for__class__()` helper function and the closure fixup loop in `_add_slots()` from pythonGH-124455. Without these, renaming the closure variable from `cls` to `__class__` has no effect because nothing updates the cell when the class is recreated with slots. - Changed `_add_slots()` to use `newcls` instead of reusing `cls` for the recreated class, so both old and new class references are available for the fixup loop. - Replaced `assertNotHasAttr` with `assertFalse(hasattr(...))` in tests (assertNotHasAttr was added in 3.14). - Dropped `test_original_class_is_gced` additions (that test does not exist on 3.13; it was added by pythonGH-137047 for pythongh-135228 which was not backported to 3.13). Co-authored-by: Prometheus3375 <prometheus3375@gmail.com> Co-authored-by: Sviataslau <35541026+Prometheus3375@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
GH-148476 is a backport of this pull request to the 3.13 branch. |
…__` and `__delattr__` in frozen dataclasses with slots (pythonGH-144021)" This reverts commit 8a398bf.
…`__delattr__` in frozen dataclasses with slots (pythonGH-144021)
…_delattr__ in frozen dataclasses with slots (GH-144021) (GH-148476) gh-105936: Properly update closure cells for `__setattr__` and `__delattr__` in frozen dataclasses with slots (GH-144021) (cherry picked from commit 8a398bf) The cherry-pick required additional changes beyond the original commit because 3.13 lacks the `__class__` closure cell fixup machinery that was added in 3.14 by GH-124455 (gh-90562). Specifically: - Backported `_update_func_cell_for__class__()` helper function and the closure fixup loop in `_add_slots()` from GH-124455. Without these, renaming the closure variable from `cls` to `__class__` has no effect because nothing updates the cell when the class is recreated with slots. - Changed `_add_slots()` to use `newcls` instead of reusing `cls` for the recreated class, so both old and new class references are available for the fixup loop. - Replaced `assertNotHasAttr` with `assertFalse(hasattr(...))` in tests (assertNotHasAttr was added in 3.14). - Dropped `test_original_class_is_gced` additions (that test does not exist on 3.13; it was added by GH-137047 for gh-135228 which was not backported to 3.13). gh-148947: dataclasses: fix error on empty __class__ cell (GH-148948) Also add a test demonstrating the need for the existing "is oldcls" check. (cherry picked from commit 6d7bbee) --------- Co-authored-by: Prometheus3375 <prometheus3375@gmail.com> Co-authored-by: Sviataslau <35541026+Prometheus3375@users.noreply.github.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
Specifying
frozen=Truefor a dataclass adds methods__setattr__and__detattr__to it. These methods reference the class via closure. Ifslots=Trueis also present, then the class is recreated with slots with all methods being copied. Notably, every method referencing the old class via closure in variable__class__is properly updated, but__setattr__and__detattr__reference the old class viacls; hence, they are still referencing the old class instead of the new one. This PR fixes this issue.__setattr__and__delattr__does not work. #105936Additional details:
The issue is present in Python as early as in 3.10. This PR must be backported to 3.14 and possibly 3.13.
Originally this PR was referencing gh-135228, but soon I realized that the issue is not connected to regression in 3.14. There is an older PR #105937 that fixes the issue, but it is heavily outdated.