April 22, 2022    Share on: Twitter | Facebook | HackerNews | Reddit

Use python TypedDict to type hint dictionaries

Define types for the fields in dictionary using TypedDict as base class to inherit from and then use this class in hints.

from typing import TypedDict


class NameInfo(TypedDict):
    name: str
    first_letter: str


def get_info(name: str) -> NameInfo:
    return {'name': name, 'first_letter': name[0]}

from: https://stackoverflow.com/a/54198204/3247880