PEP 764: Inlined typed dictionaries

I would also find this capability useful, although I have a slightly different syntax in mind:


In the PEP I see the syntax:

from typing import TypedDict

def get_movie() -> TypedDict[{'name': str, 'year': int}]:
    return {
        'name': 'Blade Runner',
        'year': 1982,
    }

I’d like to additionally propose that TypedDict[…] (with a literal …) be recognized. It would infer the fields and their types using type checker inference rules. For example:

from typing import TypedDict

def get_movie() -> TypedDict[…]:
    return {
        'name': 'Blade Runner',
        'year': 1982,
    }

This syntax would allow big dictionaries to be defined/returned, yet be given the obvious TypedDict type without needing to spell it out explicitly.

This automatic/inferred TypedDict[…] syntax would eliminate a lot of boilerplate in a particular large .py file from one of my (closed source) projects. (Trivia: It’s the same file that inspired me to propose TypedDict itself many years ago.)

9 Likes