Skip to content

bpo-44653: Support typing.Union in parameter substitution of the union type #27232

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

Closed
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,21 @@ def test_union_parameter_chaining(self):
self.assertEqual((list[T] | list[S])[int, T], list[int] | list[T])
self.assertEqual((list[T] | list[S])[int, int], list[int])

def test_union_parameter_substitution_union(self):
T = typing.TypeVar("T")
res = (int | T)[str | list]
self.assertEqual(res, int | str | list)
self.assertIsInstance(res, types.Union)
res = (int | T)[str | int]
self.assertEqual(res, int | str)
self.assertIsInstance(res, types.Union)
res = (int | T)[typing.Union[str, list]]
self.assertEqual(res, int | str | list)
self.assertIsInstance(res, types.Union)
res = (int | T)[typing.Union[str, int]]
self.assertEqual(res, int | str)
self.assertIsInstance(res, types.Union)

def test_union_parameter_substitution_errors(self):
T = typing.TypeVar("T")
x = int | T
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support ``typing.Union`` in parameter substitution of the union type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is more specific:

Suggested change
Support ``typing.Union`` in parameter substitution of the union type.
Flatten ``typing.Union`` during parameter substitution of the union type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better? Before this PR typing.Union was not supported at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm oh right I forgot. I think it's good to also mention that the arguments are flattened though, since none of the other types are treated like that. Some people might expect (int | T)[Union[str, int]] to become int | Union[str, int].

Suggested change
Support ``typing.Union`` in parameter substitution of the union type.
Support ``typing.Union`` in parameter substitution of the union type.
Arguments in ``typing.Union`` are flattened and merged into the
union type.

45 changes: 41 additions & 4 deletions Objects/unionobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ is_new_type(PyObject *obj)
return is_typing_module(obj);
}

static int
is_typing_union(PyObject *obj)
{
return is_typing_name(obj, "_UnionGenericAlias");
}

// Emulates short-circuiting behavior of the ``||`` operator
// while also checking negative values.
#define CHECK_RES(res) { \
Expand Down Expand Up @@ -429,6 +435,19 @@ static PyMethodDef union_methods[] = {
{0}};


static PyObject *
from_typing_union(PyObject *obj)
{
_Py_IDENTIFIER(__args__);
PyObject *args = _PyObject_GetAttrId(obj, &PyId___args__);
if (args == NULL) {
return NULL;
}
PyObject *result = make_union(args);
Py_DECREF(args);
return result;
}

static PyObject *
union_getitem(PyObject *self, PyObject *item)
{
Expand All @@ -447,16 +466,34 @@ union_getitem(PyObject *self, PyObject *item)
}

// Check arguments are unionable.
assert(Py_REFCNT(newargs) == 1);
Py_ssize_t nargs = PyTuple_GET_SIZE(newargs);
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
PyObject *arg = PyTuple_GET_ITEM(newargs, iarg);
int is_arg_unionable = is_unionable(arg);
if (is_arg_unionable <= 0) {
Py_DECREF(newargs);
if (is_arg_unionable == 0) {
int r = is_unionable(arg);
if (r == 0) {
r = is_typing_union(arg);
if (r == 0) {
PyErr_Format(PyExc_TypeError,
"Each union argument must be a type, got %.100R", arg);
Py_DECREF(newargs);
return NULL;
}
if (r > 0) {
// Replace typing.Union with types.Union.
PyObject *newarg = from_typing_union(arg);
if (newarg == NULL) {
Py_DECREF(newargs);
return NULL;
}
assert(Py_REFCNT(newargs) == 1);
assert(PyTuple_GET_ITEM(newargs, iarg) == arg);
PyTuple_SET_ITEM(newargs, iarg, newarg);
Py_DECREF(arg);
}
}
if (r < 0) {
Py_DECREF(newargs);
return NULL;
}
}
Expand Down