-
-
Notifications
You must be signed in to change notification settings - Fork 34k
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 |
|---|---|---|
|
|
@@ -196,16 +196,49 @@ def test_field_named_object(self): | |
| @dataclass | ||
| class C: | ||
| object: str | ||
| c=C('foo') | ||
| 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') | ||
| 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_field_named_like_builtin(self): | ||
| # Attribute names can shadow built-in names | ||
| # since code generation is used. | ||
| # Ensure that this is not happening. | ||
| exclusions = {'None', 'True', 'False'} | ||
| builtins = sorted( | ||
| b for b in __builtins__.__dict__.keys() | ||
| if not b.startswith('__') and b not in exclusions | ||
| ) | ||
| C = make_dataclass('C', [(name, str) for name in builtins]) | ||
|
|
||
| c = C(*[name for name in builtins]) | ||
|
|
||
| for name in builtins: | ||
| self.assertEqual(getattr(c, name), name) | ||
|
|
||
| def test_field_named_like_builtin_frozen(self): | ||
| # Attribute names can shadow built-in names | ||
| # since code generation is used. | ||
| # Ensure that this is not happening | ||
| # for frozen data classes. | ||
| exclusions = {'None', 'True', 'False'} | ||
| builtins = sorted( | ||
| b for b in __builtins__.__dict__.keys() | ||
| if not b.startswith('__') and b not in exclusions | ||
| ) | ||
| C = make_dataclass('C', [(name, str) for name in builtins], frozen=True) | ||
|
|
||
| c = C(*[name for name in builtins]) | ||
|
|
||
| for name in builtins: | ||
| self.assertEqual(getattr(c, name), name) | ||
|
|
||
| def test_0_field_compare(self): | ||
| # Ensure that order=False is the default. | ||
| @dataclass | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1 @@ | ||
| Frozen dataclasses didn't work properly with an attribute called "object". | ||
| Now they do. | ||
| Allow frozen dataclasses to have a field named "object". Previously this conflicted with an internal use of "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.
Per your other comment: I did not know this was how you had to access
__builtins__insideexec. Although we could get around this by passing adding{"__builtins__": __builtins__}to the globals passed in toexec. I'll have to think about that: there might be other reasons to setglobals["__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 remarkable, isn't that.
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.
Well, it is
dictonly if you provide theglobalsargument:That means that the right thing to do is to include
__builtins__in theglobalsdict or always use{}instead ofNone.