Skip to content

Commit f84d5a1

Browse files
authored
bpo-42128: __match_args__ can't be a list anymore (GH-25203)
1 parent 3d4af4a commit f84d5a1

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

Lib/test/test_dataclasses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3401,7 +3401,7 @@ class C:
34013401
self.assertEqual(C(42).__match_args__, ('a',))
34023402

34033403
def test_explicit_match_args(self):
3404-
ma = []
3404+
ma = ()
34053405
@dataclass
34063406
class C:
34073407
a: int

Lib/test/test_patma.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def no_perf(f):
1717
class MyClass:
1818
x: int
1919
y: str
20-
__match_args__ = ["x", "y"]
20+
__match_args__ = ("x", "y")
2121

2222

2323
@dataclasses.dataclass
@@ -2018,7 +2018,7 @@ def f(color):
20182018

20192019
def test_patma_200(self):
20202020
class Class:
2021-
__match_args__ = ["a", "b"]
2021+
__match_args__ = ("a", "b")
20222022
c = Class()
20232023
c.a = 0
20242024
c.b = 1
@@ -2046,7 +2046,7 @@ def test_patma_202(self):
20462046
class Parent:
20472047
__match_args__ = "a", "b"
20482048
class Child(Parent):
2049-
__match_args__ = ["c", "d"]
2049+
__match_args__ = ("c", "d")
20502050
c = Child()
20512051
c.a = 0
20522052
c.b = 1
@@ -2500,7 +2500,7 @@ class Class:
25002500
@no_perf
25012501
def test_patma_248(self):
25022502
class Class:
2503-
__match_args__ = [None]
2503+
__match_args__ = (None,)
25042504
x = Class()
25052505
y = z = None
25062506
with self.assertRaises(TypeError):
@@ -2513,7 +2513,7 @@ class Class:
25132513
@no_perf
25142514
def test_patma_249(self):
25152515
class Class:
2516-
__match_args__ = []
2516+
__match_args__ = ()
25172517
x = Class()
25182518
y = z = None
25192519
with self.assertRaises(TypeError):
@@ -2560,7 +2560,7 @@ class Keys:
25602560
@no_perf
25612561
def test_patma_253(self):
25622562
class Class:
2563-
__match_args__ = ["a", "a"]
2563+
__match_args__ = ("a", "a")
25642564
a = None
25652565
x = Class()
25662566
w = y = z = None
@@ -2575,7 +2575,7 @@ class Class:
25752575
@no_perf
25762576
def test_patma_254(self):
25772577
class Class:
2578-
__match_args__ = ["a"]
2578+
__match_args__ = ("a",)
25792579
a = None
25802580
x = Class()
25812581
w = y = z = None
@@ -2841,6 +2841,22 @@ def test_patma_281(self):
28412841
self.assertEqual(x, range(10))
28422842
self.assertIs(y, None)
28432843

2844+
@no_perf
2845+
def test_patma_282(self):
2846+
class Class:
2847+
__match_args__ = ["spam", "eggs"]
2848+
spam = 0
2849+
eggs = 1
2850+
x = Class()
2851+
w = y = z = None
2852+
with self.assertRaises(TypeError):
2853+
match x:
2854+
case Class(y, z):
2855+
w = 0
2856+
self.assertIs(w, None)
2857+
self.assertIs(y, None)
2858+
self.assertIs(z, None)
2859+
28442860

28452861
class PerfPatma(TestPatma):
28462862

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:data:`~object.__match_args__` is no longer allowed to be a list.

Python/ceval.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,15 +1029,8 @@ match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
10291029
int match_self = 0;
10301030
match_args = PyObject_GetAttrString(type, "__match_args__");
10311031
if (match_args) {
1032-
if (PyList_CheckExact(match_args)) {
1033-
Py_SETREF(match_args, PyList_AsTuple(match_args));
1034-
}
1035-
if (match_args == NULL) {
1036-
goto fail;
1037-
}
10381032
if (!PyTuple_CheckExact(match_args)) {
1039-
const char *e = "%s.__match_args__ must be a list or tuple "
1040-
"(got %s)";
1033+
const char *e = "%s.__match_args__ must be a tuple (got %s)";
10411034
_PyErr_Format(tstate, PyExc_TypeError, e,
10421035
((PyTypeObject *)type)->tp_name,
10431036
Py_TYPE(match_args)->tp_name);

0 commit comments

Comments
 (0)