-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
bpo-34213: frozen dataclass with "object" attr bug #8452
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 1 commit
fea7cf1
3171e9c
4d7ee27
4b0ea3e
d08afd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -192,6 +192,20 @@ class C: | |
| first = next(iter(sig.parameters)) | ||
| self.assertEqual('self', first) | ||
|
|
||
| def test_field_named_object(self): | ||
| @dataclass | ||
| class C: | ||
| object: str | ||
| c=C('foo') | ||
|
||
| self.assertEqual(c.object, 'foo') | ||
|
|
||
| def test_field_named_object_frozen(self): | ||
| @dataclass(frozen=True) | ||
| class C: | ||
| object: str | ||
| c=C('foo') | ||
|
||
| self.assertEqual(c.object, 'foo') | ||
|
|
||
|
Member
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. In addition to these tests, please add a test for field names that are all identifiers in
Member
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. Something like: Which works in 3.7.0, and Which fails in 3.7.0. [Edit: fix frozen example] |
||
| def test_0_field_compare(self): | ||
| # Ensure that order=False is the default. | ||
| @dataclass | ||
|
|
||
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 the only change you need to make in this file is to use
__builtins__.objectinstead ofobjecthere. Since identifiers that start with double underscores are reserved for Python, we don't need to support a field named__builtins__.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.
That's indeed a right thing do, I completely forgot about
__builtins__.However, surprisingly (at least for me) you should do
__builtins__['object']__, not__builtins__.objectsince__builtins__is a dictionary insideexec, not a module: “If the globals dictionary does not contain a value for the key__builtins__, a reference to the dictionary of the built-in module builtins is inserted under that key.” — https://docs.python.org/3/library/functions.html#exec