Typealias-like definition of enum members makes mypy forget we're in an enum
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Python-Reproducer im Issue und führe ihn durch das verlinkte mypy-play-Beispiel. Vergleiche die aufgedeckten Typen für E.A, E.B und E.C mit den Laufzeitwerten und ihren .value-Aufrufen; abgeschlossen ist die Aufgabe, wenn Enum-Mitglieder Enum-Typen behalten, während ihre Werte die entsprechenden Typen erhalten, ohne fälschlicherweise Attributfehler zu erzeugen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100