Skip to content

Support more than just string literals with TypedDict indexing #7867

@wadetregaskis-linkedin

Description

@wadetregaskis-linkedin
from mypy_extensions import TypedDict

T = TypedDict('T', {'a': float, 'b': float, 'c': float})

foo: T = {'a': 0.0, 'b': 0.0, 'c': 0.0}

for key in ('a', 'b', 'c'):
    foo[key] = 1.0

mypy currently (0.730) complains about that last line, saying error: TypedDict key must be a string literal; expected one of ('a', 'b', 'c'). It's understandable, but limiting - it'd be great if mypy could recognise that key is in fact one of the valid keys for T.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions