[Python-checkins] bpo-35960: Fix dataclasses.field throwing away empty metadata. (GH-11815)

Eric V. Smith webhook-mailer at python.org
Tue Feb 12 06:50:57 EST 2019


https://github.com/python/cpython/commit/b01786c8812c4cc24dd561b5941025bdd6f444c0
commit: b01786c8812c4cc24dd561b5941025bdd6f444c0
branch: master
author: Christopher Hunt <chrahunt at gmail.com>
committer: Eric V. Smith <ericvsmith at users.noreply.github.com>
date: 2019-02-12T06:50:49-05:00
summary:

 bpo-35960: Fix dataclasses.field throwing away empty metadata.  (GH-11815)

files:
A Misc/NEWS.d/next/Library/2019-02-10-20-57-12.bpo-35960.bh-6Ja.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 71d9896a1052..325b822d9f06 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -241,7 +241,7 @@ def __init__(self, default, default_factory, init, repr, hash, compare,
         self.hash = hash
         self.compare = compare
         self.metadata = (_EMPTY_METADATA
-                         if metadata is None or len(metadata) == 0 else
+                         if metadata is None else
                          types.MappingProxyType(metadata))
         self._field_type = None
 
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index ff6060c6d283..9c83459f09e7 100755
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -1737,23 +1737,33 @@ class C:
                 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']
diff --git a/Misc/NEWS.d/next/Library/2019-02-10-20-57-12.bpo-35960.bh-6Ja.rst b/Misc/NEWS.d/next/Library/2019-02-10-20-57-12.bpo-35960.bh-6Ja.rst
new file mode 100644
index 000000000000..67135843877f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-02-10-20-57-12.bpo-35960.bh-6Ja.rst
@@ -0,0 +1,2 @@
+Fix :func:`dataclasses.field` throwing away empty mapping objects passed as
+metadata.



More information about the Python-checkins mailing list