Skip to content

Commit

Permalink
Add support for UnionType in get_args (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vovetta committed Jul 18, 2022
1 parent f052816 commit 0f45982
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test_typing_inspect.py
Expand Up @@ -438,6 +438,10 @@ def test_args_evaluated(self):
# This would return (~T,) before Python 3.9.
self.assertEqual(get_args(List), ())

if sys.version_info >= (3, 10):
self.assertEqual(get_args(int | str), (int, str))
self.assertEqual(get_args((int | tuple[T, int])[str]), (int, tuple[str, int]))

def test_bound(self):
T = TypeVar('T')
TB = TypeVar('TB', bound=int)
Expand Down
4 changes: 4 additions & 0 deletions typing_inspect.py
Expand Up @@ -223,6 +223,8 @@ def is_union_type(tp):
is_union_type(Union) == True
is_union_type(Union[int, int]) == False
is_union_type(Union[T, int]) == True
is_union_type(int | int) == False
is_union_type(T | int) == True
"""
if NEW_TYPING:
return (tp is Union or
Expand Down Expand Up @@ -512,6 +514,8 @@ def get_args(tp, evaluate=None):
if get_origin(tp) is collections.abc.Callable and res[0] is not Ellipsis:
res = (list(res[:-1]), res[-1])
return res
if MaybeUnionType and isinstance(tp, MaybeUnionType):
return tp.__args__
return ()
if is_classvar(tp) or is_final_type(tp):
return (tp.__type__,) if tp.__type__ is not None else ()
Expand Down

0 comments on commit 0f45982

Please sign in to comment.