Skip to content

Commit 0a1dc96

Browse files
added test for legacy unions
1 parent 690ac3d commit 0a1dc96

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

Lib/test/test_patma.py

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

34043404
def test_class_or_union_not_specialform(self):
3405-
w = None
34063405
from typing import Literal
34073406
name = type(Literal).__name__
3408-
msg = rf"called match pattern must be a class or a union \(got {name}\)"
3407+
msg = rf"called match pattern must be a class or types.UnionType \(got {name}\)"
3408+
w = None
34093409
with self.assertRaisesRegex(TypeError, msg):
34103410
match 1:
34113411
case Literal():
34123412
w = 0
34133413
self.assertIsNone(w)
34143414

3415+
def test_patma_legacy_union_type(self):
3416+
from typing import Union
3417+
IntOrStr = Union[int, str]
3418+
name = type(IntOrStr).__name__
3419+
msg = rf"called match pattern must be a class or types.UnionType \(got {name}\)"
3420+
w = None
3421+
with self.assertRaisesRegex(TypeError, msg):
3422+
match 1:
3423+
case IntOrStr():
3424+
w = 0
3425+
self.assertIsNone(w)
3426+
34153427
def test_regular_protocol(self):
34163428
from typing import Protocol
34173429
class P(Protocol): ...
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Since Python 3.10, it was possible to use unions as the second argument to ``isinstance``. Now, unions can also be used as match patterns.
1+
Since Python 3.10, it was possible to use unions as the second argument to ``isinstance``. Now, PEP604 style unions can also be used as match patterns.

Python/ceval.c

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

0 commit comments

Comments
 (0)