mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-44015: dataclasses should allow KW_ONLY to be specified only once per class (GH-25841)
bpo-44015: Raise a TypeError if KW_ONLY is specified more than once.
This commit is contained in:
parent
72720a2639
commit
99ad742ea9
3 changed files with 83 additions and 0 deletions
|
@ -3699,6 +3699,83 @@ class TestKeywordArgs(unittest.TestCase):
|
|||
self.assertEqual(c.b, 3)
|
||||
self.assertEqual(c.c, 2)
|
||||
|
||||
def test_KW_ONLY_as_string(self):
|
||||
@dataclass
|
||||
class A:
|
||||
a: int
|
||||
_: 'dataclasses.KW_ONLY'
|
||||
b: int
|
||||
c: int
|
||||
A(3, c=5, b=4)
|
||||
msg = "takes 2 positional arguments but 4 were given"
|
||||
with self.assertRaisesRegex(TypeError, msg):
|
||||
A(3, 4, 5)
|
||||
|
||||
def test_KW_ONLY_twice(self):
|
||||
msg = "'Y' is KW_ONLY, but KW_ONLY has already been specified"
|
||||
|
||||
with self.assertRaisesRegex(TypeError, msg):
|
||||
@dataclass
|
||||
class A:
|
||||
a: int
|
||||
X: KW_ONLY
|
||||
Y: KW_ONLY
|
||||
b: int
|
||||
c: int
|
||||
|
||||
with self.assertRaisesRegex(TypeError, msg):
|
||||
@dataclass
|
||||
class A:
|
||||
a: int
|
||||
X: KW_ONLY
|
||||
b: int
|
||||
Y: KW_ONLY
|
||||
c: int
|
||||
|
||||
with self.assertRaisesRegex(TypeError, msg):
|
||||
@dataclass
|
||||
class A:
|
||||
a: int
|
||||
X: KW_ONLY
|
||||
b: int
|
||||
c: int
|
||||
Y: KW_ONLY
|
||||
|
||||
# But this usage is okay, since it's not using KW_ONLY.
|
||||
@dataclass
|
||||
class A:
|
||||
a: int
|
||||
_: KW_ONLY
|
||||
b: int
|
||||
c: int = field(kw_only=True)
|
||||
|
||||
# And if inheriting, it's okay.
|
||||
@dataclass
|
||||
class A:
|
||||
a: int
|
||||
_: KW_ONLY
|
||||
b: int
|
||||
c: int
|
||||
@dataclass
|
||||
class B(A):
|
||||
_: KW_ONLY
|
||||
d: int
|
||||
|
||||
# Make sure the error is raised in a derived class.
|
||||
with self.assertRaisesRegex(TypeError, msg):
|
||||
@dataclass
|
||||
class A:
|
||||
a: int
|
||||
_: KW_ONLY
|
||||
b: int
|
||||
c: int
|
||||
@dataclass
|
||||
class B(A):
|
||||
X: KW_ONLY
|
||||
d: int
|
||||
Y: KW_ONLY
|
||||
|
||||
|
||||
def test_post_init(self):
|
||||
@dataclass
|
||||
class A:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue