-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-96151: Use a private name for passing builtins to dataclass #98143
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
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3e03d52
gh-96151: Use a private name for passing builtins to dataclass
hauntsaninja 7f9f91e
📜🤖 Added by blurb_it.
blurb-it[bot] 2d5f290
add a test case
hauntsaninja d7e8782
separate out test
hauntsaninja 96cc82b
use dunder builtins
hauntsaninja e7456f8
actually, prefix with __dataclass
hauntsaninja 8f6e968
Remove builtins logic from _create_fn
hauntsaninja 9d44429
also update comment
hauntsaninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2022-10-10-07-07-31.gh-issue-96151.K9fwoq.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Allow ``BUILTINS`` to be a valid field name for frozen dataclasses. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'd suggest using
__BUILTINS__because dunder names are supposed to be reserved for use by the stdlib.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.
Before I commit this, I'd like to spend some time researching why this test is even present, instead of just unconditionally assigning to
locals. At the very least it could use a comment.Also, I'm not sure that exposing all of
builtinsinlocalsis a good idea, versus just exposingbuiltins.object.Uh oh!
There was an error while loading. Please reload this page.
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 catch, looks like the relevant history is:
That is, I think this check was made dead in #9518, but wasn't noticed in that PR
(Also note that the comment at the top of
_create_fnis out of date: we do mutate locals, but not via exec)Uh oh!
There was an error while loading. Please reload this page.
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 can do some double checking (
localsshould all be created within dataclasses.py) and clean that up + change the PR to only pass alongobject.(I'll also note that there's still the inscrutable
().__class__.__base__option on the table, in case we don't want to expose anything at all)Uh oh!
There was an error while loading. Please reload this page.
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.
Okay, I audited all the call sites, it looks like there is one case where this check is not dead, but it's accidental.
Over here we reuse the same locals dict for two different
_create_fncalls:cpython/Lib/dataclasses.py
Line 619 in 87b5fd9
so the second time round we already have the entry for builtins in the dict. shrug
So my conclusion is:
a) It's safe to remove the check.
b) We should actually go a little further. Since we only need builtins for the frozen init, we should pass that in explicitly when creating
__init__and remove this from_create_fnI've gone ahead and pushed this change to the PR