TypedDict accepts an Enum member as a key and reveals the value type, but KeyErrors at runtime
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 20.6k
- Forks
- 3.3k
- Métriques de merge des PR
- Métriques de PR en attente
Description
mypy lets you index a TypedDict with an Enum member, and reveal_type gives the correct value type. But it's a KeyError at runtime. Under the hood mypy substitutes the member's .value for the key, a substitution it refuses to make anywhere else.
To Reproduce
from enum import Enum
from typing import Literal, TypedDict
class Foo(Enum):
One = "One"
class TD(TypedDict):
One: int
td: TD = {"One": 1}
d: dict[str, int] = {"One": 1}
reveal_type(td[Foo.One]) # Revealed type is "int" -- accepted
x: Literal["One"] = Foo.One # error: incompatible types -- member is not the literal
d[Foo.One] # error: invalid index type -- not a valid str key
Only the TypedDict line gets through. The other two are rejected, and correctly so: Foo.One is Literal[Foo.One], not Literal["One"], and it isn't a str. So mypy already knows the member isn't its value, everywhere except TypedDict subscription.
At runtime a plain Enum member doesn't compare equal to its value, so the "type-safe" access is the thing that breaks:
>>> td[Foo.One]
KeyError: <Foo.One: 'One'>
Expected Behavior
td[Foo.One] should be an error, the same way d[Foo.One] on a dict[str, int] is. pyright rejects it: "Could not access item in TypedDict."
Actual Behavior
Accepted. reveal_type is int. KeyError when you run it.
One wrinkle worth noting: the only case where the runtime agrees with mypy is a str-mixin enum (StrEnum, or class Foo(str, Enum)), because there the member genuinely is the string. So the acceptance happens to be sound for str enums and is a false negative for every other Enum.
Your Environment
- mypy 2.2.0 (compiled). Reproduces with and without
--strict. - Python 3.12.8
- No flags beyond the above; no plugins.
- For comparison: pyright 1.1.411 flags it.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par exécuter avec mypy la reproduction de TypedDict et Enum, puis suivez le chemin de vérification de type de la souscription TypedDict responsable de l’acceptation de Foo.One. Le travail est terminé lorsque les membres Enum simples sont rejetés comme clés de dict[str, int], tandis que les enums avec un mixin str restent acceptés ; ajoutez ou mettez à jour un test de régression pour les deux cas.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- compilers, devtools
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- Calme
- Clarté
- Clairement spécifiée
- Accessibilité débutants
- 62/100