Skip to content
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

Add support for UnionType in get_args #87

Merged
merged 1 commit into from Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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