-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-135228: Properly update closure cells for __setattr__ and __detattr__ in frozen dataclasses with slots
#144021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
gh-135228: Properly update closure cells for __setattr__ and __detattr__ in frozen dataclasses with slots
#144021
Conversation
|
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
a6de29b
Compare
|
Added 3 tests:
I will add news a bit later. |
Prometheus3375
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, 3.12 also suffers from the same issue. I do not have 3.13, but I guess there it is also present. This PR should be backported to 3.13.
|
Added news entry. Not sure how to properly format Moved previously added tests to test case Also added another test to verify raised exceptions when from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class B:
b: int
self = B(1)
del self.any_field
# ^^^^^^^^^^^^^^
# TypeError: super(type, obj): obj (instance of B) is not an instance or subtype of type (B). |
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.More details in these comments: 1, 2.
Any help with tests for the issue would be appreciated. I guess just checking that
__setattr__and__delattr__reference the new class would be sufficient.