Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Define type for enum and complex dictionary

from typing import Literal
LevelType = Literal["debug", "info", "warning", "error"]

size: LevelType = "debug"
#call it label size = "eror"

# ------------------------------------------

from datetime import datetime
from typing import TypedDict
HistoryType = TypedDict('HistoryType', {
    "date" : datetime,
    "level": LevelType,
    "text": str,
})

event: HistoryType = {
        "date": datetime.now(),
        "level": "debug",
        "text": "Demo typing",
}