Type aliases

In certain situations, type names may end up being long and painful to type:

  1. def f() -> Union[List[Dict[Tuple[int, str], Set[int]]], Tuple[str, List[str]]]:
  2. ...

When cases like this arise, you can define a type alias by simplyassigning the type to a variable:

  1. AliasType = Union[List[Dict[Tuple[int, str], Set[int]]], Tuple[str, List[str]]]
  2.  
  3. # Now we can use AliasType in place of the full name:
  4.  
  5. def f() -> AliasType:
  6. ...

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.