Difference between constrained 'TypeVar' and 'Union' in Python

from blog Redowan's Reflections, | ↗ original
↗ original
If you want to define a variable that can accept values of multiple possible types, using typing.Union is one way of doing that: from typing import Union U = Union[int, str] However, there’s another way you can express a similar concept via constrained TypeVar. You’d do so as follows: from typing import TypeVar T = TypeVar("T", int, str) So,...