[Python-checkins] bpo-43764: Fix `__match_args__` generation logic for dataclasses (GH-25284)

brandtbucher webhook-mailer at python.org
Thu Apr 8 15:54:46 EDT 2021


https://github.com/python/cpython/commit/d92c59f48680122ce0e4d1ccf69d92b983e8db01
commit: d92c59f48680122ce0e4d1ccf69d92b983e8db01
branch: master
author: Brandt Bucher <brandt at python.org>
committer: brandtbucher <brandtbucher at gmail.com>
date: 2021-04-08T12:54:34-07:00
summary:

bpo-43764: Fix `__match_args__` generation logic for dataclasses (GH-25284)

files:
A Misc/NEWS.d/next/Library/2021-04-08-09-59-20.bpo-43764.tHjO60.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index afc4b8282abe3..ceda8220f1f6c 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -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)
 
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index 12c1918602d6a..29f29e1e6895e 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -3432,6 +3432,14 @@ class C:
             __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
index 0000000000000..838dd0200f1d6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-04-08-09-59-20.bpo-43764.tHjO60.rst
@@ -0,0 +1,2 @@
+Fix an issue where :data:`~object.__match_args__` generation could fail for
+some :mod:`dataclasses`.



More information about the Python-checkins mailing list