Skip to content

Commit 8de5f17

Browse files
committed
Resolve string enum values when checking TypedDict keys
1 parent a4fe82b commit 8de5f17

2 files changed

Lines changed: 133 additions & 3 deletions

File tree

‎mypy/checkexpr.py‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,14 +4810,48 @@ def visit_typeddict_index_expr(
48104810
key_types = [typ]
48114811

48124812
key_names = []
4813+
str_type = self.named_type("builtins.str")
48134814
for key_type in get_proper_types(key_types):
4814-
if isinstance(key_type, Instance) and key_type.last_known_value is not None:
4815-
key_type = key_type.last_known_value
4815+
seen_enum_keys: set[LiteralType] = set()
4816+
while True:
4817+
if isinstance(key_type, Instance) and key_type.last_known_value is not None:
4818+
key_type = key_type.last_known_value
4819+
if (
4820+
not isinstance(key_type, LiteralType)
4821+
or not key_type.is_enum_literal()
4822+
or not is_subtype(key_type.fallback, str_type)
4823+
):
4824+
break
4825+
enum_type = key_type.fallback.copy_modified(last_known_value=key_type)
4826+
new_type = enum_type.type.get_containing_type_info("__new__")
4827+
if key_type in seen_enum_keys or (
4828+
new_type is not None
4829+
and not new_type.is_enum
4830+
and new_type.fullname != "builtins.str"
4831+
):
4832+
self.msg.typeddict_key_must_be_string_literal(td_type, index)
4833+
return AnyType(TypeOfAny.from_error), set()
4834+
seen_enum_keys.add(key_type)
4835+
# Enum literals store member names. Use the declared value,
4836+
# without following a user-defined value property.
4837+
key_type = get_proper_type(
4838+
analyze_member_access(
4839+
"value",
4840+
enum_type,
4841+
index,
4842+
is_lvalue=False,
4843+
is_super=False,
4844+
is_operator=False,
4845+
original_type=enum_type,
4846+
override_info=self.chk.lookup_typeinfo("enum.Enum"),
4847+
chk=self.chk,
4848+
)
4849+
)
48164850

48174851
if (
48184852
isinstance(key_type, LiteralType)
48194853
and isinstance(key_type.value, str)
4820-
and is_subtype(key_type.fallback, self.named_type("builtins.str"))
4854+
and is_subtype(key_type.fallback, str_type)
48214855
):
48224856
key_names.append(key_type.value)
48234857
else:

‎test-data/unit/check-typeddict.test‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,6 +3030,102 @@ reveal_type(d[StringMixinEnum.foo]) # N: Revealed type is "builtins.int"
30303030
[builtins fixtures/enum.pyi]
30313031
[typing fixtures/typing-typeddict.pyi]
30323032

3033+
[case testTypedDictStringEnumKeyValue]
3034+
# flags: --python-version 3.11
3035+
from enum import Enum, StrEnum
3036+
from typing import Literal, TypedDict
3037+
3038+
class StringEnum(StrEnum):
3039+
name = "value"
3040+
unknown = "missing"
3041+
3042+
class StringMixinEnum(str, Enum):
3043+
name = "value"
3044+
3045+
class NestedEnum(StrEnum):
3046+
name = StringEnum.name
3047+
3048+
class TD(TypedDict):
3049+
name: int
3050+
value: str
3051+
3052+
d: TD
3053+
reveal_type(d[StringEnum.name]) # N: Revealed type is "builtins.str"
3054+
reveal_type(d[StringMixinEnum.name]) # N: Revealed type is "builtins.str"
3055+
reveal_type(d[NestedEnum.name]) # N: Revealed type is "builtins.str"
3056+
d[StringEnum.name] = 1 # E: Argument 2 to "__setitem__" has incompatible type "int"; expected "str"
3057+
d[StringEnum.unknown] # E: TypedDict "TD" has no key "missing"
3058+
key: Literal[StringEnum.name, StringMixinEnum.name]
3059+
reveal_type(d[key]) # N: Revealed type is "builtins.str"
3060+
[builtins fixtures/enum.pyi]
3061+
[typing fixtures/typing-typeddict.pyi]
3062+
3063+
[case testTypedDictStringEnumKeyValueProperty]
3064+
# flags: --python-version 3.11
3065+
from enum import StrEnum
3066+
from typing import Literal, TypedDict
3067+
3068+
class Key(StrEnum):
3069+
name = "value"
3070+
3071+
# The enum stub models value as a writable attribute.
3072+
@property
3073+
def value(self) -> Literal["name"]: # type: ignore[override, misc]
3074+
return "name"
3075+
3076+
class TD(TypedDict):
3077+
name: int
3078+
value: str
3079+
3080+
d: TD
3081+
reveal_type(d[Key.name]) # N: Revealed type is "builtins.str"
3082+
reveal_type(d[Key.name.value]) # N: Revealed type is "builtins.int"
3083+
[builtins fixtures/property.pyi]
3084+
[typing fixtures/typing-typeddict.pyi]
3085+
3086+
[case testTypedDictStringEnumKeyCustomConstructor]
3087+
from enum import Enum
3088+
from typing import TypedDict
3089+
3090+
class StringKey(str, Enum):
3091+
name = "name"
3092+
def __new__(cls, value: str) -> "StringKey": pass
3093+
3094+
class CustomString(str):
3095+
def __new__(cls, value: str) -> "CustomString": pass
3096+
3097+
class CustomKey(CustomString, Enum):
3098+
name = "name"
3099+
3100+
class TD(TypedDict):
3101+
name: int
3102+
3103+
d: TD
3104+
d[StringKey.name] # E: TypedDict key must be a string literal; expected one of ("name")
3105+
d[CustomKey.name] # E: TypedDict key must be a string literal; expected one of ("name")
3106+
[builtins fixtures/enum.pyi]
3107+
[typing fixtures/typing-typeddict.pyi]
3108+
3109+
[case testTypedDictStringEnumKeyRecursiveValue]
3110+
# flags: --python-version 3.11
3111+
from enum_values import Key
3112+
from typing import TypedDict
3113+
3114+
class TD(TypedDict):
3115+
ITEM: int
3116+
3117+
d: TD
3118+
d[Key.ITEM] # E: TypedDict key must be a string literal; expected one of ("ITEM")
3119+
[builtins fixtures/enum.pyi]
3120+
[typing fixtures/typing-typeddict.pyi]
3121+
[file enum_values.pyi]
3122+
from enum import StrEnum
3123+
from typing import Literal
3124+
3125+
class Key(StrEnum):
3126+
_value_: Literal[Key.ITEM]
3127+
ITEM = ...
3128+
30333129
[case testTypedDictUppercaseKey]
30343130
from typing import TypedDict
30353131

0 commit comments

Comments
 (0)