[Python-checkins] Add additional keyword-only tests. (GH-25633)

ericvsmith webhook-mailer at python.org
Mon Apr 26 13:14:36 EDT 2021


https://github.com/python/cpython/commit/94549ee728cd88d1ef053aab50da422f9e99b434
commit: 94549ee728cd88d1ef053aab50da422f9e99b434
branch: master
author: Eric V. Smith <ericvsmith at users.noreply.github.com>
committer: ericvsmith <ericvsmith at users.noreply.github.com>
date: 2021-04-26T13:14:28-04:00
summary:

Add additional keyword-only tests. (GH-25633)

files:
M Lib/test/test_dataclasses.py

diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index edb08485be223..670648a1b112b 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -3502,7 +3502,7 @@ def test_make_dataclasses(self):
         self.assertEqual(C.__match_args__, ('z',))
 
 
-class TestKwArgs(unittest.TestCase):
+class TestKeywordArgs(unittest.TestCase):
     def test_no_classvar_kwarg(self):
         msg = 'field a is a ClassVar but specifies kw_only'
         with self.assertRaisesRegex(TypeError, msg):
@@ -3659,6 +3659,34 @@ def __post_init__(self, b, d):
         b = B(1, c=2, b=3, d=4)
         self.assertEqual(asdict(b), {'a': 3, 'c': 4})
 
+    def test_defaults(self):
+        # For kwargs, make sure we can have defaults after non-defaults.
+        @dataclass
+        class A:
+            a: int = 0
+            _: KW_ONLY
+            b: int
+            c: int = 1
+            d: int
+
+        a = A(d=4, b=3)
+        self.assertEqual(a.a, 0)
+        self.assertEqual(a.b, 3)
+        self.assertEqual(a.c, 1)
+        self.assertEqual(a.d, 4)
+
+        # Make sure we still check for non-kwarg non-defaults not following
+        # defaults.
+        err_regex = "non-default argument 'z' follows default argument"
+        with self.assertRaisesRegex(TypeError, err_regex):
+            @dataclass
+            class A:
+                a: int = 0
+                z: int
+                _: KW_ONLY
+                b: int
+                c: int = 1
+                d: int
 
 if __name__ == '__main__':
     unittest.main()



More information about the Python-checkins mailing list