Determine whether _type is a union.
Based on
https://docs.python.org/3/library/typing.html#typing.Union
https://discuss.python.org/t/how-to-check-if-a-type-annotation-represents-an-union/77692/2.
Source code in fractal_task_tools/_union_types.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 | def is_union(_type) -> bool:
"""
Determine whether _type is a union.
Based on
https://docs.python.org/3/library/typing.html#typing.Union
https://discuss.python.org/t/how-to-check-if-a-type-annotation-represents-an-union/77692/2.
"""
result = typing.get_origin(_type) in _UNION_TYPES
alternative_result = (
type(_type) is typing._UnionGenericAlias
or type(_type) is types.UnionType
or "Union[" in str(_type)
or "|" in str(_type)
)
if result != alternative_result:
# This is a safety check, which is meant to be unreachable
raise ValueError(
f"Could not determine whether {_type} is a union. Please report "
"this at https://github.com/fractal-analytics-platform/"
"fractal-task-tools/issues."
)
return result
|