How to use a dynamic tuple of types as a type parameter?

I use pydantic create_model to dynamically create models. The method expects a list of fields and their types, so I have to dynamically build types.

For the sake of simplicity, let us consider this code where a dynamic union of types is built:

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]

While this code is correct for python, mypy raises the following error:

example.py:6: error: Variable "example.a" is not valid as a type  [valid-type]
example.py:6: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases

How can I solve this mypy error?

mypy will be perfectly happy with a union over int, str and bool. I believe there is a way to statically type a variable-length tuple (e.g. *args). Otherwise think again. Also, what is the purpose of this?

Python is a dynamically typed language, so unsurprisingly not all valid Python code is accepted by static type checking tools, such as mypy. Mypy et al typically use hints based on tuple[...] not tuple(...).

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]

The above code example we are requested to examine, goes one further, and attempts to incorporate dynamic run time only quantities, random ones too no less, into the static typing hints.

Perhaps there is a valid use case, such as generating random test programs. But that should be done in two separate steps (possibly with a third orchestrating them) - firstly the program generation, then secondly type checking of each such program. In all other cases this approach just seems naive and flawed, in my opinion.