[Python-checkins] Add test cases for dataclasses. (#17909)

Eric V. Smith webhook-mailer at python.org
Thu Jan 9 08:42:01 EST 2020


https://github.com/python/cpython/commit/eef1b027ab70704bcaa60a089e4ae1592c504b86
commit: eef1b027ab70704bcaa60a089e4ae1592c504b86
branch: master
author: Karthikeyan Singaravelan <tir.karthi at gmail.com>
committer: Eric V. Smith <ericvsmith at users.noreply.github.com>
date: 2020-01-09T08:41:46-05:00
summary:

Add test cases for dataclasses. (#17909)

* Add test cases for dataclasses.

* Add test for repr output of field.
* Add test for ValueError to be raised when both default and default_factory are passed.

files:
M Lib/test/test_dataclasses.py

diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index 8f9fb2ce8c169..e8fe455fc19b4 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -45,6 +45,25 @@ class C:
         o = C(42)
         self.assertEqual(o.x, 42)
 
+    def test_field_default_default_factory_error(self):
+        msg = "cannot specify both default and default_factory"
+        with self.assertRaisesRegex(ValueError, msg):
+            @dataclass
+            class C:
+                x: int = field(default=1, default_factory=int)
+
+    def test_field_repr(self):
+        int_field = field(default=1, init=True, repr=False)
+        int_field.name = "id"
+        repr_output = repr(int_field)
+        expected_output = "Field(name='id',type=None," \
+                           f"default=1,default_factory={MISSING!r}," \
+                           "init=True,repr=False,hash=None," \
+                           "compare=True,metadata=mappingproxy({})," \
+                           "_field_type=None)"
+
+        self.assertEqual(repr_output, expected_output)
+
     def test_named_init_params(self):
         @dataclass
         class C:



More information about the Python-checkins mailing list