Skip to content

Indexing into a single-field TypedDict with an invalid key incorrectly evaluates to the type associated with that key #3029

@AlexWaygood

Description

@AlexWaygood

Summary

from typing_extensions import TypedDict

class Foo(TypedDict):
    name: str

def _(td: Foo, key: str) -> None:
    # error: [invalid-key]
    reveal_type(td["anything"])  # revealed: str

The error here is correct but the revealed type is wrong -- it should either be Unknown or object. This only seems to occur with TypedDicts that have exactly one key. Inferring str might be correct for closed=True or extra_items TypedDicts, but it definitely isn't for open TypedDicts.

Version

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions