Skip to content

Commit 869e193

Browse files
committed
Support get_origin(Union[...]) in python 3.14
In Python 3.14, `Union` is no longer a `typingGenericAlias`, so `get_origin(int | str)` didn't return `Union`. This changes that by explicitly checking for `is_union_type`. This adds a test for `get_origin(Union[...])`.
1 parent 58c98c0 commit 869e193

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

test_typing_inspect.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ def test_origin(self):
355355
if GENERIC_TUPLE_PARAMETRIZABLE:
356356
tp = List[Tuple[T, T]][int]
357357
self.assertEqual(get_origin(tp), list if NEW_TYPING else List)
358+
self.assertIs(get_origin(Union[str, int]), typing.Union)
358359

359360
def test_parameters(self):
360361
T = TypeVar('T')

typing_inspect.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,13 @@ def get_origin(tp):
359359
get_origin(List[Tuple[T, T]][int]) == list # List prior to Python 3.7
360360
"""
361361
if NEW_TYPING:
362-
if isinstance(tp, typingGenericAlias):
362+
if (
363+
isinstance(tp, typingGenericAlias)
364+
# In Python 3.14, Union[...] is not an instance of
365+
# typingGenericAlias, but it does have an `__origin__`, so check for
366+
# Unions explicitly.
367+
or is_union_type(tp)
368+
):
363369
return tp.__origin__ if tp.__origin__ is not ClassVar else None
364370
if tp is Generic:
365371
return Generic

0 commit comments

Comments
 (0)