python / python/mypy

Unable to stubify Enum so it can be used with Literals, without using type ignore

Offen
#16,007 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-enum
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

Extending the example for #3434 I want to define an enum Flag, combine some of the members, and then also reference those members as a TypeAlias in a stub file.

class Color1(Flag):
    RED = auto()
    BLUE = auto()
    GREEN = auto()
    WHITE = RED | BLUE | GREEN  # <-- This line works at runtime but not in a stub (see the gist)
# Invalid type: try using Literal[Color1.RED] instead
Favorites: TypeAlias = Literal[Color3.RED | Color3.BLUE]

(A clear and concise description of what the bug is.)

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=d270d7541c61b2aea87e99c9afc77534

Expected Behavior

I expect mypy to suggest working code, or to accept the | operator for Flag members.

Actual Behavior

See the gist, but depending on how I write the stub I get these errors for the member definition

# foo.pyi:9: error: Invalid type: try using Literal[Color1.RED] instead?  [valid-type]
# foo.pyi:9: error: Invalid type: try using Literal[Color1.BLUE] instead?  [valid-type]
# foo.pyi:9: error: Invalid type: try using Literal[Color1.GREEN] instead?  [valid-type]

or these for the TypeAlias

# foo.pyi:12: error: Parameter 1 of Literal[...] cannot be of type "Any"  [valid-type]

Your Environment

  • Mypy version used: 1.5.1
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10.13

I was curious what stubgen came up with:

from _typeshed import Incomplete
from enum import Flag
from typing import TypeAlias

class Color(Flag):
    RED: Incomplete
    BLUE: Incomplete
    GREEN: Incomplete
    WHITE: Incomplete

Favorites: TypeAlias

but that loses information about the Literal Favorite Colors.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der mypy-play-Reproduktion und den foo.pyi-Beispielen im Bericht und konzentriere dich auf Definitionen von Flag-Mitgliedern und den Literal TypeAlias. Vergleiche die gemeldeten valid-type- und Any-Fehler mit der erwarteten Behandlung kombinierter Flag-Mitglieder; abgeschlossen ist die Aufgabe, wenn der beabsichtigte Stub die Literal-Farbinformationen bewahrt, ohne type ignores zu erfordern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.