Skip to content
Merged
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
9 changes: 9 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2283,6 +2283,15 @@ class Ints(enum.IntEnum):
self.assertEqual(Union[Literal[1], Literal[Ints.B], Literal[True]].__args__,
(Literal[1], Literal[Ints.B], Literal[True]))

def test_allow_non_types_in_or(self):
# gh-140348: Test that using | with a Union object allows things that are
# not allowed by is_unionable().
U1 = Union[int, str]
self.assertEqual(U1 | float, Union[int, str, float])
self.assertEqual(U1 | "float", Union[int, str, "float"])
self.assertEqual(float | U1, Union[float, int, str])
self.assertEqual("float" | U1, Union["float", int, str])


class TupleTests(BaseTestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix regression in Python 3.14.0 where using the ``|`` operator on a
:class:`typing.Union` object combined with an object that is not a type
would raise an error.
7 changes: 1 addition & 6 deletions Objects/unionobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,8 @@ is_unionable(PyObject *obj)
PyObject *
_Py_union_type_or(PyObject* self, PyObject* other)
{
if (!is_unionable(self) || !is_unionable(other)) {
Py_RETURN_NOTIMPLEMENTED;
}

unionbuilder ub;
// unchecked because we already checked is_unionable()
if (!unionbuilder_init(&ub, false)) {
if (!unionbuilder_init(&ub, true)) {
return NULL;
}
if (!unionbuilder_add_single(&ub, self) ||
Expand Down
Loading