]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42128: __match_args__ can't be a list anymore (GH-25203)
authorBrandt Bucher <brandt@python.org>
Tue, 6 Apr 2021 02:17:08 +0000 (19:17 -0700)
committerGitHub <noreply@github.com>
Tue, 6 Apr 2021 02:17:08 +0000 (19:17 -0700)
Lib/test/test_dataclasses.py
Lib/test/test_patma.py
Misc/NEWS.d/next/Core and Builtins/2021-04-05-17-38-08.bpo-42128.1uVeGK.rst [new file with mode: 0644]
Python/ceval.c

index 4f5c3c8aab167b31f2c324f9e791c8c40ec8396d..5515ca49409b315ee1c3fc450b913be29745a9a5 100644 (file)
@@ -3401,7 +3401,7 @@ class TestMatchArgs(unittest.TestCase):
         self.assertEqual(C(42).__match_args__, ('a',))
 
     def test_explicit_match_args(self):
-        ma = []
+        ma = ()
         @dataclass
         class C:
             a: int
index 286b190adcf3d898d5753051c43e9feb5d5d6854..40580be86b08489fe747afb485b17671bd482f87 100644 (file)
@@ -17,7 +17,7 @@ def no_perf(f):
 class MyClass:
     x: int
     y: str
-    __match_args__ = ["x", "y"]
+    __match_args__ = ("x", "y")
 
 
 @dataclasses.dataclass
@@ -2018,7 +2018,7 @@ class TestPatma(unittest.TestCase):
 
     def test_patma_200(self):
         class Class:
-            __match_args__ = ["a", "b"]
+            __match_args__ = ("a", "b")
         c = Class()
         c.a = 0
         c.b = 1
@@ -2046,7 +2046,7 @@ class TestPatma(unittest.TestCase):
         class Parent:
             __match_args__ = "a", "b"
         class Child(Parent):
-            __match_args__ = ["c", "d"]
+            __match_args__ = ("c", "d")
         c = Child()
         c.a = 0
         c.b = 1
@@ -2500,7 +2500,7 @@ class TestPatma(unittest.TestCase):
     @no_perf
     def test_patma_248(self):
         class Class:
-            __match_args__ = [None]
+            __match_args__ = (None,)
         x = Class()
         y = z = None
         with self.assertRaises(TypeError):
@@ -2513,7 +2513,7 @@ class TestPatma(unittest.TestCase):
     @no_perf
     def test_patma_249(self):
         class Class:
-            __match_args__ = []
+            __match_args__ = ()
         x = Class()
         y = z = None
         with self.assertRaises(TypeError):
@@ -2560,7 +2560,7 @@ class TestPatma(unittest.TestCase):
     @no_perf
     def test_patma_253(self):
         class Class:
-            __match_args__ = ["a", "a"]
+            __match_args__ = ("a", "a")
             a = None
         x = Class()
         w = y = z = None
@@ -2575,7 +2575,7 @@ class TestPatma(unittest.TestCase):
     @no_perf
     def test_patma_254(self):
         class Class:
-            __match_args__ = ["a"]
+            __match_args__ = ("a",)
             a = None
         x = Class()
         w = y = z = None
@@ -2841,6 +2841,22 @@ class TestPatma(unittest.TestCase):
         self.assertEqual(x, range(10))
         self.assertIs(y, None)
 
+    @no_perf
+    def test_patma_282(self):
+        class Class:
+            __match_args__ = ["spam", "eggs"]
+            spam = 0
+            eggs = 1
+        x = Class()
+        w = y = z = None
+        with self.assertRaises(TypeError):
+            match x:
+                case Class(y, z):
+                    w = 0
+        self.assertIs(w, None)
+        self.assertIs(y, None)
+        self.assertIs(z, None)
+
 
 class PerfPatma(TestPatma):
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-04-05-17-38-08.bpo-42128.1uVeGK.rst b/Misc/NEWS.d/next/Core and Builtins/2021-04-05-17-38-08.bpo-42128.1uVeGK.rst
new file mode 100644 (file)
index 0000000..b87c97c
--- /dev/null
@@ -0,0 +1 @@
+:data:`~object.__match_args__` is no longer allowed to be a list.
index b9d784a6298400fd7f3b31d42f67f0e48942a0cd..d9a754fb9107b04e7977e4ed5845d8a23988b377 100644 (file)
@@ -1029,15 +1029,8 @@ match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
         int match_self = 0;
         match_args = PyObject_GetAttrString(type, "__match_args__");
         if (match_args) {
-            if (PyList_CheckExact(match_args)) {
-                Py_SETREF(match_args, PyList_AsTuple(match_args));
-            }
-            if (match_args == NULL) {
-                goto fail;
-            }
             if (!PyTuple_CheckExact(match_args)) {
-                const char *e = "%s.__match_args__ must be a list or tuple "
-                                "(got %s)";
+                const char *e = "%s.__match_args__ must be a tuple (got %s)";
                 _PyErr_Format(tstate, PyExc_TypeError, e,
                               ((PyTypeObject *)type)->tp_name,
                               Py_TYPE(match_args)->tp_name);