typing.get_type_hints function for this use case, see https://docs.python.org/3.9/library/typ
You could use the __args__ attribute of Union which holds a tuple of the "possible contents:
>>> from typing import Union
>>> x = Union[int, str]
>>> x.__args__
(int, str)
>>> isinstance(3, x.__args__)
True
>>> isinstance('a', x.__args__)
True
The __args__ argument is not documented so it could be considered "messing with implementation details" but it seems like a better way than parsing the repr.