mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
bpo-43764: Add match_args=False parameter to dataclass decorator and to make_dataclasses function. (GH-25337)
Add match_args=False parameter to dataclass decorator and to make_dataclass function.
This commit is contained in:
parent
c3a478b7e5
commit
750f484752
4 changed files with 81 additions and 19 deletions
|
@ -3440,6 +3440,47 @@ class TestMatchArgs(unittest.TestCase):
|
|||
c: int
|
||||
self.assertEqual(X.__match_args__, ("a", "b", "c"))
|
||||
|
||||
def test_match_args_argument(self):
|
||||
@dataclass(match_args=False)
|
||||
class X:
|
||||
a: int
|
||||
self.assertNotIn('__match_args__', X.__dict__)
|
||||
|
||||
@dataclass(match_args=False)
|
||||
class Y:
|
||||
a: int
|
||||
__match_args__ = ('b',)
|
||||
self.assertEqual(Y.__match_args__, ('b',))
|
||||
|
||||
@dataclass(match_args=False)
|
||||
class Z(Y):
|
||||
z: int
|
||||
self.assertEqual(Z.__match_args__, ('b',))
|
||||
|
||||
# Ensure parent dataclass __match_args__ is seen, if child class
|
||||
# specifies match_args=False.
|
||||
@dataclass
|
||||
class A:
|
||||
a: int
|
||||
z: int
|
||||
@dataclass(match_args=False)
|
||||
class B(A):
|
||||
b: int
|
||||
self.assertEqual(B.__match_args__, ('a', 'z'))
|
||||
|
||||
def test_make_dataclasses(self):
|
||||
C = make_dataclass('C', [('x', int), ('y', int)])
|
||||
self.assertEqual(C.__match_args__, ('x', 'y'))
|
||||
|
||||
C = make_dataclass('C', [('x', int), ('y', int)], match_args=True)
|
||||
self.assertEqual(C.__match_args__, ('x', 'y'))
|
||||
|
||||
C = make_dataclass('C', [('x', int), ('y', int)], match_args=False)
|
||||
self.assertNotIn('__match__args__', C.__dict__)
|
||||
|
||||
C = make_dataclass('C', [('x', int), ('y', int)], namespace={'__match_args__': ('z',)})
|
||||
self.assertEqual(C.__match_args__, ('z',))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue