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:
Eric V. Smith 2021-04-10 21:28:42 -04:00 committed by GitHub
parent c3a478b7e5
commit 750f484752
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 19 deletions

View file

@ -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()