-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Changes from all commits
3e03d52
7f9f91e
2d5f290
d7e8782
96cc82b
e7456f8
8f6e968
9d44429
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,6 +257,14 @@ class C: | |
c = C('foo') | ||
self.assertEqual(c.object, 'foo') | ||
|
||
def test_field_named_BUILTINS_frozen(self): | ||
# gh-96151 | ||
@dataclass(frozen=True) | ||
class C: | ||
BUILTINS: int | ||
c = C(5) | ||
self.assertEqual(c.BUILTINS, 5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a separate function for this, and don't mix it in with existing tests. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review, made the change :-) |
||
|
||
def test_field_named_like_builtin(self): | ||
# Attribute names can shadow built-in names | ||
# since code generation is used. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Allow ``BUILTINS`` to be a valid field name for frozen dataclasses. |
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
builtins
inlocals
is 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_fn
is 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 (
locals
should 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_fn
calls: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_fn
I've gone ahead and pushed this change to the PR