-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-90562: Support zero argument super with dataclasses when slots=True #124455
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
gh-90562: Support zero argument super with dataclasses when slots=True #124455
Conversation
… showing that the original class, if saved, will have in incorrect __class__ value.
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.
Thanks for working on this!
There is the possible case where all methods of a class are decorated with opaque decorators that don't use functools.wraps
, and thus we have no route to find the cell to update, and thus this approach simply fails. Should this case be mentioned in the tests? In the docs? Would it be worth explicitly introducing a __classcell__
member on classes through which the cell can reliably be accessed, so as to avoid this problem?
B = dataclass(slots=True)(A) | ||
self.assertIs(B().cls(), B) | ||
|
||
# This is probably undesirable behavior, but is a function of |
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.
I think we can safely remove this "probably" :)
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.
Heh. Will do.
Lib/dataclasses.py
Outdated
_update_func_cell_for__class__(member, cls, newcls) | ||
elif isinstance(member, property): | ||
_update_func_cell_for__class__(member.fget, cls, newcls) | ||
_update_func_cell_for__class__(member.fset, cls, newcls) | ||
_update_func_cell_for__class__(member.fdel, cls, newcls) |
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.
I think technically we could break out of the loop as soon as we've successfully updated one cell, because all the closures for a given class will always share a single __class__
cell. This may improve performance noticeably for larger classes?
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.
Good point. Will change.
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.
Thanks for the review, @carljm!
Lib/dataclasses.py
Outdated
_update_func_cell_for__class__(member, cls, newcls) | ||
elif isinstance(member, property): | ||
_update_func_cell_for__class__(member.fget, cls, newcls) | ||
_update_func_cell_for__class__(member.fset, cls, newcls) | ||
_update_func_cell_for__class__(member.fdel, cls, newcls) |
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.
Good point. Will change.
B = dataclass(slots=True)(A) | ||
self.assertIs(B().cls(), B) | ||
|
||
# This is probably undesirable behavior, but is a function of |
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.
Heh. Will do.
Apply review suggestions. Co-authored-by: Carl Meyer <[email protected]>
…loop; minor change to comments; fix a test.
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.
Looks good!
(Discussed in person that if people hit the "all methods use an opaque decorator" case, the answer is "fix the decorator to use functools.wraps
")
📚 Documentation preview 📚: https://cpython-previews--124455.org.readthedocs.build/