]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43764: Fix `__match_args__` generation logic for dataclasses (GH-25284)
authorBrandt Bucher <brandt@python.org>
Thu, 8 Apr 2021 19:54:34 +0000 (12:54 -0700)
committerGitHub <noreply@github.com>
Thu, 8 Apr 2021 19:54:34 +0000 (12:54 -0700)
Lib/dataclasses.py
Lib/test/test_dataclasses.py
Misc/NEWS.d/next/Library/2021-04-08-09-59-20.bpo-43764.tHjO60.rst [new file with mode: 0644]

index afc4b8282abe31bb10e6cf89bd913a4c6469d691..ceda8220f1f6cdf2ab0cd074ca24148d187bfd64 100644 (file)
@@ -1017,7 +1017,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
                        str(inspect.signature(cls)).replace(' -> NoneType', ''))
 
     if '__match_args__' not in cls.__dict__:
-        cls.__match_args__ = tuple(f.name for f in flds if f.init)
+        cls.__match_args__ = tuple(f.name for f in field_list if f.init)
 
     abc.update_abstractmethods(cls)
 
index 12c1918602d6af1f37b4a9068fd52fbfb7f50c4d..29f29e1e6895e4218de8ea54e9c4485e67f534ca 100644 (file)
@@ -3432,6 +3432,14 @@ class TestMatchArgs(unittest.TestCase):
             __match_args__ = ma
         self.assertIs(C(42).__match_args__, ma)
 
+    def test_bpo_43764(self):
+        @dataclass(repr=False, eq=False, init=False)
+        class X:
+            a: int
+            b: int
+            c: int
+        self.assertEqual(X.__match_args__, ("a", "b", "c"))
+
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2021-04-08-09-59-20.bpo-43764.tHjO60.rst b/Misc/NEWS.d/next/Library/2021-04-08-09-59-20.bpo-43764.tHjO60.rst
new file mode 100644 (file)
index 0000000..838dd02
--- /dev/null
@@ -0,0 +1,2 @@
+Fix an issue where :data:`~object.__match_args__` generation could fail for
+some :mod:`dataclasses`.