diff options
author | Christopher Hunt <chrahunt@gmail.com> | 2019-02-12 11:50:49 (GMT) |
---|---|---|
committer | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2019-02-12 11:50:49 (GMT) |
commit | b01786c8812c4cc24dd561b5941025bdd6f444c0 (patch) | |
tree | 7ce684adbbc729ae848b7190a87631ed9d92ef14 /Lib/test/test_dataclasses.py | |
parent | a31f4cc881992e84d351957bd9ac1a92f882fa39 (diff) | |
download | cpython-b01786c8812c4cc24dd561b5941025bdd6f444c0.zip cpython-b01786c8812c4cc24dd561b5941025bdd6f444c0.tar.gz cpython-b01786c8812c4cc24dd561b5941025bdd6f444c0.tar.bz2 |
bpo-35960: Fix dataclasses.field throwing away empty metadata. (GH-11815)
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rwxr-xr-x | Lib/test/test_dataclasses.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index ff6060c..9c83459 100755 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -1737,23 +1737,33 @@ class TestCase(unittest.TestCase): i: int = field(metadata=0) # Make sure an empty dict works. + d = {} @dataclass class C: - i: int = field(metadata={}) + i: int = field(metadata=d) self.assertFalse(fields(C)[0].metadata) self.assertEqual(len(fields(C)[0].metadata), 0) + # Update should work (see bpo-35960). + d['foo'] = 1 + self.assertEqual(len(fields(C)[0].metadata), 1) + self.assertEqual(fields(C)[0].metadata['foo'], 1) with self.assertRaisesRegex(TypeError, 'does not support item assignment'): fields(C)[0].metadata['test'] = 3 # Make sure a non-empty dict works. + d = {'test': 10, 'bar': '42', 3: 'three'} @dataclass class C: - i: int = field(metadata={'test': 10, 'bar': '42', 3: 'three'}) + i: int = field(metadata=d) self.assertEqual(len(fields(C)[0].metadata), 3) self.assertEqual(fields(C)[0].metadata['test'], 10) self.assertEqual(fields(C)[0].metadata['bar'], '42') self.assertEqual(fields(C)[0].metadata[3], 'three') + # Update should work. + d['foo'] = 1 + self.assertEqual(len(fields(C)[0].metadata), 4) + self.assertEqual(fields(C)[0].metadata['foo'], 1) with self.assertRaises(KeyError): # Non-existent key. fields(C)[0].metadata['baz'] |