aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_dataclasses.py
diff options
context:
space:
mode:
authorEric V. Smith <ericvsmith@users.noreply.github.com>2021-04-26 13:14:28 -0400
committerGitHub <noreply@github.com>2021-04-26 13:14:28 -0400
commit94549ee728cd88d1ef053aab50da422f9e99b434 (patch)
tree6c67351b1e83fcf7e0de9e1cfe6c3af90ba4e69d /Lib/test/test_dataclasses.py
parentbd25bcd37a3a41a0e08208e969f0c02287850c61 (diff)
downloadcpython-94549ee728cd88d1ef053aab50da422f9e99b434.tar.gz
cpython-94549ee728cd88d1ef053aab50da422f9e99b434.zip
Add additional keyword-only tests. (GH-25633)
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rw-r--r--Lib/test/test_dataclasses.py30
1 files changed, 29 insertions, 1 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index edb08485be2..670648a1b11 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()