Skip to content

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

Merged

Conversation

ericvsmith
Copy link
Member

@ericvsmith ericvsmith commented Sep 24, 2024

@ericvsmith ericvsmith changed the title gt-90562: Support zero argument super with dataclasses when slots=True gh-90562: Support zero argument super with dataclasses when slots=True Sep 24, 2024
@ericvsmith ericvsmith self-assigned this Sep 24, 2024
@ericvsmith ericvsmith requested a review from carljm September 24, 2024 19:25
Copy link
Member

@carljm carljm left a 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
Copy link
Member

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" :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh. Will do.

Comment on lines 1304 to 1308
_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)
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Will change.

Copy link
Member Author

@ericvsmith ericvsmith left a 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!

Comment on lines 1304 to 1308
_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)
Copy link
Member Author

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh. Will do.

ericvsmith and others added 2 commits September 24, 2024 19:38
Apply review suggestions.

Co-authored-by: Carl Meyer <[email protected]>
Copy link
Member

@carljm carljm left a 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")

@ericvsmith ericvsmith merged commit 5c6e3b7 into python:main Sep 25, 2024
36 checks passed
@ericvsmith ericvsmith deleted the issue-90562-dataclasses-zero-argument-super branch September 25, 2024 01:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants