Skip to content

Using variables as key of TypedDict  #7178

@RonNabuurs

Description

@RonNabuurs

With the following example I get an error.

from mypy_extensions import TypedDict

class Test(TypedDict):
    a: str
    b: str

def read(test: Test) -> str:
    key = 'a'
    return test[key]
error: TypedDict key must be a string literal; expected one of ('a', 'b')

Can mypy not figure out that key is set to a literal?

In my own code im building up key dynamically to get a key from a TypedDict.

mypy version: 0.711

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions