Get Union values from function annotations?

I’m looking to get the functions’ annotations, specifically the Union values of each annotation. Lemme show for an example:

from typing import Union

async def func(arg1: str, arg2: int, arg3: Union[str, int]):

# Want to get
# {arg3: [str, int], arg1: str, ...}

I know func.__annotations__ returns the dict but I’m not sure about the Union typehints


Use typing.get_args:

>>> func.__annotations__['arg3']
typing.Union[str, int]
>>> from typing import get_args
>>> get_args(func.__annotations__['arg3'])
(<class 'str'>, <class 'int'>)