Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4817,7 +4817,7 @@ def visit_typeddict_index_expr(
if (
isinstance(key_type, LiteralType)
and isinstance(key_type.value, str)
and key_type.fallback.type.fullname != "builtins.bytes"
and is_subtype(key_type.fallback, self.named_type("builtins.str"))
):
key_names.append(key_type.value)
else:
Expand Down
26 changes: 26 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -3004,6 +3004,32 @@ d[True] # E: TypedDict key must be a string literal; expected one of ("foo")
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictEnumKey]
# flags: --python-version 3.11
from enum import Enum, StrEnum
from typing import TypedDict

class PlainEnum(Enum):
foo = "foo"

class StringEnum(StrEnum):
foo = "foo"

class StringMixinEnum(str, Enum):
foo = "foo"

class TD(TypedDict):
foo: int

d: TD
d[PlainEnum.foo] # E: TypedDict key must be a string literal; expected one of ("foo")
d[PlainEnum.foo] = 1 # E: TypedDict key must be a string literal; expected one of ("foo") \
# E: Argument 1 to "__setitem__" has incompatible type "PlainEnum"; expected "str"
reveal_type(d[StringEnum.foo]) # N: Revealed type is "builtins.int"
reveal_type(d[StringMixinEnum.foo]) # N: Revealed type is "builtins.int"
[builtins fixtures/enum.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictUppercaseKey]
from typing import TypedDict

Expand Down
Loading