needleinthehay.de

Python Type Hints

Using TypeAlias to abstract complicated hints or to provide additional information:

from typing import TypeAlias

milliseconds: TypeAlias = int
time_record = dict[str, list[int]]

def add_to_record(time: milliseconds, record: time_record):
    ...

Type hint for Class (available for Python 3.11+):

from typing import Self  
  
class Node:  
    def __init__(self, prev_node: Self):  
        pass

Hinting literals:

from typing import Literal

def openings_on_weekend(day_of_week: Literal["saturday", "sunday"]):
    ...