mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-98886: Fix issues with dataclass fields with special underscore names (#102032)
This commit prefixes `__dataclass` to several things in the locals dict: - Names like `_dflt_` (which cause trouble, see first test) - Names like `_type_` (not known to be able to cause trouble) - `_return_type` (not known to able to cause trouble) - `_HAS_DEFAULT_FACTORY` (which causes trouble, see second test) In addition, this removes `MISSING` from the locals dict. As far as I can tell, this wasn't needed even in the initial implementation of dataclasses.py (and tests on that version passed with it removed). This makes me wary :-) This is basically a continuation of #96151, where fixing this was welcomed in https://github.com/python/cpython/pull/98143#issuecomment-1280306360
This commit is contained in:
parent
027223db96
commit
718e86671f
3 changed files with 27 additions and 10 deletions
|
@ -285,6 +285,23 @@ class TestCase(unittest.TestCase):
|
|||
c = C(5)
|
||||
self.assertEqual(c.BUILTINS, 5)
|
||||
|
||||
def test_field_with_special_single_underscore_names(self):
|
||||
# gh-98886
|
||||
|
||||
@dataclass
|
||||
class X:
|
||||
x: int = field(default_factory=lambda: 111)
|
||||
_dflt_x: int = field(default_factory=lambda: 222)
|
||||
|
||||
X()
|
||||
|
||||
@dataclass
|
||||
class Y:
|
||||
y: int = field(default_factory=lambda: 111)
|
||||
_HAS_DEFAULT_FACTORY: int = 222
|
||||
|
||||
assert Y(y=222).y == 222
|
||||
|
||||
def test_field_named_like_builtin(self):
|
||||
# Attribute names can shadow built-in names
|
||||
# since code generation is used.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue