Skip to content

Commit eee5ab3

Browse files
added test for legacy unions
1 parent 00a7f11 commit eee5ab3

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Lib/test/test_patma.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3411,16 +3411,28 @@ def test_class_pattern_not_type(self):
34113411
self.assertIsNone(w)
34123412

34133413
def test_class_or_union_not_specialform(self):
3414-
w = None
34153414
from typing import Literal
34163415
name = type(Literal).__name__
3417-
msg = rf"called match pattern must be a class or a union \(got {name}\)"
3416+
msg = rf"called match pattern must be a class or types.UnionType \(got {name}\)"
3417+
w = None
34183418
with self.assertRaisesRegex(TypeError, msg):
34193419
match 1:
34203420
case Literal():
34213421
w = 0
34223422
self.assertIsNone(w)
34233423

3424+
def test_patma_legacy_union_type(self):
3425+
from typing import Union
3426+
IntOrStr = Union[int, str]
3427+
name = type(IntOrStr).__name__
3428+
msg = rf"called match pattern must be a class or types.UnionType \(got {name}\)"
3429+
w = None
3430+
with self.assertRaisesRegex(TypeError, msg):
3431+
match 1:
3432+
case IntOrStr():
3433+
w = 0
3434+
self.assertIsNone(w)
3435+
34243436
def test_regular_protocol(self):
34253437
from typing import Protocol
34263438
class P(Protocol): ...

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ _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)";
748+
const char *e = "called match pattern must be a class or types.UnionType (got %s)";
749749
_PyErr_Format(tstate, PyExc_TypeError, e, Py_TYPE(type)->tp_name);
750750
return NULL;
751751
}

0 commit comments

Comments
 (0)