Skip to content

Commit d65100e

Browse files
added test for class or union error message
1 parent 0a7ceb5 commit d65100e

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
@@ -3410,6 +3410,16 @@ def test_class_pattern_not_type(self):
34103410
w = 0
34113411
self.assertIsNone(w)
34123412

3413+
def test_class_or_union_not_specialform(self):
3414+
w = None
3415+
from typing import Literal
3416+
msg = 'called match pattern must be a class or a union'
3417+
with self.assertRaisesRegex(TypeError, msg):
3418+
match 1:
3419+
case Literal():
3420+
w = 0
3421+
self.assertIsNone(w)
3422+
34133423
def test_regular_protocol(self):
34143424
from typing import Protocol
34153425
class P(Protocol): ...

Python/ceval.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type,
730730
if (_PyUnion_Check(type)) {
731731
// get union members
732732
PyObject *members = _Py_union_args(type);
733-
Py_ssize_t n = PyTuple_GET_SIZE(members);
733+
const Py_ssize_t n = PyTuple_GET_SIZE(members);
734734

735735
// iterate over union members and return first match
736736
for (Py_ssize_t i = 0; i < n; i++) {
@@ -745,8 +745,8 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type,
745745
return NULL;
746746
}
747747
if (!PyType_Check(type)) {
748-
const char *e = "called match pattern must be a class or a union, (got %s, %s)";
749-
_PyErr_Format(tstate, PyExc_TypeError, e, Py_TYPE(type)->tp_name, subject->ob_type->tp_name);
748+
const char *e = "called match pattern must be a class or a union (got %s)";
749+
_PyErr_Format(tstate, PyExc_TypeError, e, Py_TYPE(type)->tp_name);
750750
return NULL;
751751
}
752752
assert(PyTuple_CheckExact(kwargs));

0 commit comments

Comments
 (0)