Check A Variable Against Union Type At Runtime In Python 3.6
Answer : 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 . The existing accepted answer by MSeifert (https://stackoverflow.com/a/45959000/7433423) does not distinguish Union s from other generic types, and it is difficult to determine at runtime whether a type annotation is a Union or some other generic type like Mapping due to the behavior of isinstance() and issubclass() on parameterized Union types. It appears that generic types will have an undocumented __origin__ attribute which will contain a reference to the original generic type used to create ...