Type aliases
In certain situations, type names may end up being long and painful to type:
- def f() -> Union[List[Dict[Tuple[int, str], Set[int]]], Tuple[str, List[str]]]:
- ...
When cases like this arise, you can define a type alias by simplyassigning the type to a variable:
- AliasType = Union[List[Dict[Tuple[int, str], Set[int]]], Tuple[str, List[str]]]
- # Now we can use AliasType in place of the full name:
- def f() -> AliasType:
- ...
Note
A type alias does not create a new type. It’s just a shorthand notation foranother type – it’s equivalent to the target type except forgeneric aliases.