Search code examples
pythontype-hintingtyping

How to define an alias for a type in Python for type hinting


How to define an alias for a type to use type hint:

import typing
type Ticker str # How to do this? I used golang notation. How do you do it in python?
Report = typing.Dict[Ticker, typing.List] 

Which means Ticker is a type of string and Report is a dict from Ticker to a list. Thank you. The good thing about this aliasing is that anyone knows that it is a ticker. Much clear and more readable than writing Report = typing.Dict[str, typing.List]


Solution

  • As you do with all other names that you bind to objects:

    Ticker = typing.Text
    

    See https://docs.python.org/3/library/typing.html#type-aliases