diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-08-13 03:32:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-13 03:32:44 (GMT) |
commit | 32e58fc32188753d2a3604feacdf9540fe9515fb (patch) | |
tree | fc1ed6bd98a7925e6203c558c57fd4b9890270fa /Lib/dataclasses.py | |
parent | 393151e241e0c2b1826e3aefac0beb42e04e6ea8 (diff) | |
download | cpython-32e58fc32188753d2a3604feacdf9540fe9515fb.zip cpython-32e58fc32188753d2a3604feacdf9540fe9515fb.tar.gz cpython-32e58fc32188753d2a3604feacdf9540fe9515fb.tar.bz2 |
bpo-34213: Allow dataclasses to work with a field named 'object'. (GH-8452)
(cherry picked from commit 4d12e4dc28b7c782c368bae2e8fd3815167ed37d)
Co-authored-by: Vadim Pushtaev <pushtaev.vm@gmail.com>
Diffstat (limited to 'Lib/dataclasses.py')
-rw-r--r-- | Lib/dataclasses.py | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index e00a125..a43d076 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -4,6 +4,7 @@ import copy import types import inspect import keyword +import builtins __all__ = ['dataclass', 'field', @@ -343,6 +344,11 @@ def _create_fn(name, args, body, *, globals=None, locals=None, # worries about external callers. if locals is None: locals = {} + # __builtins__ may be the "builtins" module or + # the value of its "__dict__", + # so make sure "__builtins__" is the module. + if globals is not None and '__builtins__' not in globals: + globals['__builtins__'] = builtins return_annotation = '' if return_type is not MISSING: locals['_return_type'] = return_type @@ -365,7 +371,7 @@ def _field_assign(frozen, name, value, self_name): # self_name is what "self" is called in this function: don't # hard-code "self", since that might be a field name. if frozen: - return f'object.__setattr__({self_name},{name!r},{value})' + return f'__builtins__.object.__setattr__({self_name},{name!r},{value})' return f'{self_name}.{name}={value}' |