From: Eric V. Smith Date: Mon, 26 Apr 2021 17:14:28 +0000 (-0400) Subject: Add additional keyword-only tests. (GH-25633) X-Git-Tag: v3.10.0b1~149 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=94549ee728cd88d1ef053aab50da422f9e99b434;p=thirdparty%2FPython%2Fcpython.git Add additional keyword-only tests. (GH-25633) --- diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index edb08485be22..670648a1b112 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -3502,7 +3502,7 @@ class TestMatchArgs(unittest.TestCase): 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 @@ class TestKwArgs(unittest.TestCase): 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()