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

This commit is contained in:
Eric V. Smith 2021-04-26 13:14:28 -04:00 committed by GitHub
parent bd25bcd37a
commit 94549ee728
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 1 deletions

View File

@ -3502,7 +3502,7 @@ class TestMatchArgs(unittest.TestCase):
self.assertEqual(C.__match_args__, ('z',)) self.assertEqual(C.__match_args__, ('z',))
class TestKwArgs(unittest.TestCase): class TestKeywordArgs(unittest.TestCase):
def test_no_classvar_kwarg(self): def test_no_classvar_kwarg(self):
msg = 'field a is a ClassVar but specifies kw_only' msg = 'field a is a ClassVar but specifies kw_only'
with self.assertRaisesRegex(TypeError, msg): with self.assertRaisesRegex(TypeError, msg):
@ -3659,6 +3659,34 @@ class TestKwArgs(unittest.TestCase):
b = B(1, c=2, b=3, d=4) b = B(1, c=2, b=3, d=4)
self.assertEqual(asdict(b), {'a': 3, 'c': 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__': if __name__ == '__main__':
unittest.main() unittest.main()