Skip to content

gh-106246: Allow the use of unions as match patterns #118644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow the use of unions as match patterns
  • Loading branch information
tmke8 authored and randolf-scholz committed Jul 20, 2025
commit cb2b850d4589445d3d7319da824e7b5f4ab13c75
34 changes: 34 additions & 0 deletions Lib/test/test_patma.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dis
import enum
import inspect
from re import I
import sys
import unittest
from test import support
Expand Down Expand Up @@ -2888,6 +2889,14 @@ class B(A): ...
h = 1
self.assertEqual(h, 1)

def test_patma_union_type(self):
IntOrStr = int | str
x = 0
match x:
case IntOrStr():
x = 1
self.assertEqual(x, 1)


class TestSyntaxErrors(unittest.TestCase):

Expand Down Expand Up @@ -3370,6 +3379,31 @@ class A:
w = 0
self.assertIsNone(w)

def test_union_type_postional_subpattern(self):
IntOrStr = int | str
x = 1
w = None
with self.assertRaises(TypeError):
match x:
case IntOrStr(x):
w = 0
self.assertEqual(x, 1)
self.assertIsNone(w)

def test_union_type_keyword_subpattern(self):
@dataclasses.dataclass
class Point2:
x: int
y: int
EitherPoint = Point | Point2
x = Point(x=1, y=2)
w = None
with self.assertRaises(TypeError):
match x:
case EitherPoint(x=1, y=2):
w = 0
self.assertIsNone(w)


class TestValueErrors(unittest.TestCase):

Expand Down
15 changes: 13 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "pycore_template.h" // _PyTemplate_Build()
#include "pycore_traceback.h" // _PyTraceBack_FromFrame
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "pycore_unionobject.h" // _PyUnion_Check()
#include "pycore_uop_ids.h" // Uops

#include "dictobject.h"
Expand Down Expand Up @@ -725,8 +726,8 @@ PyObject*
_PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type,
Py_ssize_t nargs, PyObject *kwargs)
{
if (!PyType_Check(type)) {
const char *e = "called match pattern must be a class";
if (!PyType_Check(type) && !_PyUnion_Check(type)) {
const char *e = "called match pattern must be a class or a union";
_PyErr_Format(tstate, PyExc_TypeError, e);
return NULL;
}
Expand All @@ -735,6 +736,16 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type,
if (PyObject_IsInstance(subject, type) <= 0) {
return NULL;
}
// Subpatterns are not supported for union types:
if (_PyUnion_Check(type)) {
// Return error if any positional or keyword arguments are given:
if (nargs || PyTuple_GET_SIZE(kwargs)) {
const char *e = "union types do not support sub-patterns";
_PyErr_Format(tstate, PyExc_TypeError, e);
return NULL;
}
return PyTuple_New(0);
}
// So far so good:
PyObject *seen = PySet_New(NULL);
if (seen == NULL) {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy