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/test/test_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/test/test_dataclasses.py')
-rwxr-xr-x | Lib/test/test_dataclasses.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index c5140e8..4c93513 100755 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -6,6 +6,7 @@ from dataclasses import * import pickle import inspect +import builtins import unittest from unittest.mock import Mock from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional @@ -192,6 +193,55 @@ class TestCase(unittest.TestCase): 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') + + 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_names = sorted( + b for b in builtins.__dict__.keys() + if not b.startswith('__') and b not in exclusions + ) + attributes = [(name, str) for name in builtins_names] + C = make_dataclass('C', attributes) + + c = C(*[name for name in builtins_names]) + + for name in builtins_names: + 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_names = sorted( + b for b in builtins.__dict__.keys() + if not b.startswith('__') and b not in exclusions + ) + attributes = [(name, str) for name in builtins_names] + C = make_dataclass('C', attributes, frozen=True) + + c = C(*[name for name in builtins_names]) + + for name in builtins_names: + self.assertEqual(getattr(c, name), name) + def test_0_field_compare(self): # Ensure that order=False is the default. @dataclass |