Skip to content

Commit 5129959

Browse files
committed
gh-140348: Fix using | on unusual objects plus Unions
1 parent 38d4b43 commit 5129959

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

Lib/test/test_typing.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,15 @@ class Ints(enum.IntEnum):
22832283
self.assertEqual(Union[Literal[1], Literal[Ints.B], Literal[True]].__args__,
22842284
(Literal[1], Literal[Ints.B], Literal[True]))
22852285

2286+
def test_allow_non_types_in_or(self):
2287+
# gh-140348: Test that using | with a Union object allows things that are
2288+
# not allowed by is_unionable().
2289+
U1 = Union[int, str]
2290+
self.assertEqual(U1 | float, Union[int, str, float])
2291+
self.assertEqual(U1 | "float", Union[int, str, "float"])
2292+
self.assertEqual(float | U1, Union[float, int, str])
2293+
self.assertEqual("float" | U1, Union["float", int, str])
2294+
22862295

22872296
class TupleTests(BaseTestCase):
22882297

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix regression in Python 3.14.0 where using the ``|`` operator on a
2+
:class:`typing.Union` object combined with an object that is not a type
3+
would raise an error.

Objects/unionobject.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,8 @@ is_unionable(PyObject *obj)
256256
PyObject *
257257
_Py_union_type_or(PyObject* self, PyObject* other)
258258
{
259-
if (!is_unionable(self) || !is_unionable(other)) {
260-
Py_RETURN_NOTIMPLEMENTED;
261-
}
262-
263259
unionbuilder ub;
264-
// unchecked because we already checked is_unionable()
265-
if (!unionbuilder_init(&ub, false)) {
260+
if (!unionbuilder_init(&ub, true)) {
266261
return NULL;
267262
}
268263
if (!unionbuilder_add_single(&ub, self) ||

0 commit comments

Comments
 (0)