Skip to content

Commit f3dd2fb

Browse files
added test for class or union error message
1 parent 8104fe7 commit f3dd2fb

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Lib/test/test_patma.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3401,6 +3401,16 @@ def test_class_pattern_not_type(self):
34013401
w = 0
34023402
self.assertIsNone(w)
34033403

3404+
def test_class_or_union_not_specialform(self):
3405+
w = None
3406+
from typing import Literal
3407+
msg = 'called match pattern must be a class or a union'
3408+
with self.assertRaisesRegex(TypeError, msg):
3409+
match 1:
3410+
case Literal():
3411+
w = 0
3412+
self.assertIsNone(w)
3413+
34043414
def test_regular_protocol(self):
34053415
from typing import Protocol
34063416
class P(Protocol): ...

Python/ceval.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type,
461461
if (_PyUnion_Check(type)) {
462462
// get union members
463463
PyObject *members = _Py_union_args(type);
464-
Py_ssize_t n = PyTuple_GET_SIZE(members);
464+
const Py_ssize_t n = PyTuple_GET_SIZE(members);
465465

466466
// iterate over union members and return first match
467467
for (Py_ssize_t i = 0; i < n; i++) {
@@ -476,8 +476,8 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type,
476476
return NULL;
477477
}
478478
if (!PyType_Check(type)) {
479-
const char *e = "called match pattern must be a class or a union, (got %s, %s)";
480-
_PyErr_Format(tstate, PyExc_TypeError, e, Py_TYPE(type)->tp_name, subject->ob_type->tp_name);
479+
const char *e = "called match pattern must be a class or a union (got %s)";
480+
_PyErr_Format(tstate, PyExc_TypeError, e, Py_TYPE(type)->tp_name);
481481
return NULL;
482482
}
483483
assert(PyTuple_CheckExact(kwargs));

0 commit comments

Comments
 (0)