I need to build a dynamic union of types (with a dynamic length) to be used to create pydantic models dynamically. I am looking how to type the variable to be passed to the Union
so mypy is happy.
A simplified snipped would look like this (without the dynamical parts):
<code>from typing import Union
a = (int, str)
b = Union[a]
</code>
<code>from typing import Union
a = (int, str)
b = Union[a]
</code>
from typing import Union
a = (int, str)
b = Union[a]
mypy raises a valid-type with this code, and I am not really sure how to solve it.
<code>example.py:4: error: Variable "example.a" is not valid as a type [valid-type]
example.py:4: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
</code>
<code>example.py:4: error: Variable "example.a" is not valid as a type [valid-type]
example.py:4: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
</code>
example.py:4: error: Variable "example.a" is not valid as a type [valid-type]
example.py:4: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
The error is the same with a : Tuple[Type, ...] = (int, str)
.
Here is a piece of code with the dynamical parts that I would like to type correctly:
<code>from random import choice, randint
from typing import Union
types = (int, str, bool)
a = tuple(choice(types) for _ in range(randint(1, 10)))
b = Union[a]
</code>
<code>from random import choice, randint
from typing import Union
types = (int, str, bool)
a = tuple(choice(types) for _ in range(randint(1, 10)))
b = Union[a]
</code>
from random import choice, randint
from typing import Union
types = (int, str, bool)
a = tuple(choice(types) for _ in range(randint(1, 10)))
b = Union[a]