Typealias-like definition of enum members makes mypy forget we're in an enum
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
See this code
from enum import Enum
from typing import NamedTuple
from typing_extensions import reveal_type
class E(Enum):
A = list[str]
B = tuple[int, ...]
C = NamedTuple("C", [("x", int), ("y", int)])
reveal_type(E.A)
reveal_type(E.A.value())
reveal_type(E.B)
reveal_type(E.B.value())
reveal_type(E.C)
reveal_type(E.C.value(0, 0))
mypy-play link: https://mypy-play.net/?mypy=latest&python=3.11&gist=24eebb55be028f1d9e21566f7b9bb27f
It seems to me that mypy thinks A = list[str] defines a type alias in the class E, but that's not what happens at runtime.
Expected Behavior
Runtime output:
Runtime type is 'E'
Runtime type is 'list'
Runtime type is 'E'
Runtime type is 'tuple'
Runtime type is 'E'
Runtime type is 'C'
Actual Behavior
mypy output:
main.py:10: note: Revealed type is "Overload(def () -> builtins.list[builtins.str], def (typing.Iterable[builtins.str]) -> builtins.list[builtins.str])"
main.py:11: error: "Type[List[Any]]" has no attribute "value" [attr-defined]
main.py:11: note: Revealed type is "Any"
main.py:12: note: Revealed type is "def (typing.Iterable[builtins.int] =) -> builtins.tuple[builtins.int, ...]"
main.py:13: error: "Type[Tuple[Any, ...]]" has no attribute "value" [attr-defined]
main.py:13: note: Revealed type is "Any"
main.py:14: note: Revealed type is "def (x: builtins.int, y: builtins.int) -> Tuple[builtins.int, builtins.int, fallback=enum_with_types_testcase.E.C]"
main.py:15: error: "Type[C]" has no attribute "value" [attr-defined]
main.py:15: note: Revealed type is "Any"
Found 3 errors in 1 file (checked 1 source file)
That may be a bit hard to parse, but the gist is that mypy thinks it should work like this:
reveal_type(E.A())
reveal_type(E.B())
reveal_type(E.C(0, 0))
without the .value.
Mypy is happy with this:
enum_with_types_testcase.py:10: note: Revealed type is "builtins.list[builtins.str]"
enum_with_types_testcase.py:11: note: Revealed type is "builtins.tuple[builtins.int, ...]"
enum_with_types_testcase.py:12: note: Revealed type is "Tuple[builtins.int, builtins.int, fallback=enum_with_types_testcase.E.C]"
Success: no issues found in 1 source file
but it doesn't work at runtime.
Your Environment
See the mypy play link above.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il riproduttore Python nell’issue ed eseguilo tramite l’esempio mypy-play collegato. Confronta i tipi rivelati per E.A, E.B ed E.C con i valori a runtime e le relative chiamate .value; il lavoro è completato quando i membri dell’enum mantengono i tipi enum, mentre i loro valori ricevono i tipi appropriati senza errori di attributo errati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100