10.4. Mapping

10.4.1. Dict

my_dict: dict = {
    'a': 'a',
    2: 2,
    3.3: 3.3
}
from typing import Dict

my_dict: Dict[str, int] = {
    'a': 1,
    'b': 2,
    'c': 3,
}

10.4.2. TypedDict

New in version Python: 3.8 See PEP 589

from typing import TypedDict


class Movie(TypedDict):
    name: str
    year: int


movie: Movie = {
    'name': 'Blade Runner',
    'year': 1982
}

def record_movie(movie: Movie) -> None:
    ...

record_movie({'name': 'Blade Runner', 'year': 1982})
Listing 10.13. The code below should be rejected, since 'title' is not a valid key, and the 'name' key is missing
from typing import TypedDict


class Movie(TypedDict):
    name: str
    year: int

movie2: Movie = {
    'title': 'Blade Runner',
    'year': 1982
}
from typing import TypedDict


class Movie(TypedDict):
    name: str
    year: int

m = Movie(name='Blade Runner', year=1982)
from typing import TypedDict


class Movie(TypedDict):
    name: str
    year: int

m: Movie = dict(
    name='Alien',
    year=1979,
    director='Ridley Scott')  # error: Unexpected key 'director'
from typing import TypedDict


class Movie(TypedDict):
    name: str
    year: int

class BookBasedMovie(Movie):
    based_on: str
from typing import TypedDict


class X(TypedDict):
    x: int

class Y(TypedDict):
    y: str

class XYZ(X, Y):
    z: bool