diff options
author | Shantanu <12621235+hauntsaninja@users.noreply.github.com> | 2023-03-25 21:40:11 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-25 21:40:11 (GMT) |
commit | 718e86671fe62a706c460b7f049b196e434cb5b3 (patch) | |
tree | 26cd28586e4b45a8507769c1bcc56b54034e366a /Lib/test/test_dataclasses.py | |
parent | 027223db96b0464c49a74513f82a1bf25aa510bd (diff) | |
download | cpython-718e86671fe62a706c460b7f049b196e434cb5b3.zip cpython-718e86671fe62a706c460b7f049b196e434cb5b3.tar.gz cpython-718e86671fe62a706c460b7f049b196e434cb5b3.tar.bz2 |
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
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rw-r--r-- | Lib/test/test_dataclasses.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index affd9ce..6888680 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -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. |