Support enum member `value` types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Not sure if this would be in mypy or the typeshed, though it may need support in both.
Currently enum.IntEnum provides a way to have an enum whose value is an int, however all other enum.Enums have a .value of Any.
While this can be worked around in more recent versions of Python by adding a member without a value within the enum:
class MyEnum(str, enum.Enum):
value: str
FIRST = 'first'
This isn't ideal as it means that there are two places where this needs to be specified -- as an inherited type and within the class body.
It feels like it would be better if mypy was able to infer the member value type from the inheritance.
Aside: had generics existed before Enum, it feels like that might alternatively solved this, something like:
class MyEnum(enum.Enum[str]):
FIRST = 'first'
Edit:
Note that this issue is about the general use-case of variables typed for the enum (such as x: MyEnum) -- mypy has long supported determining the type of specific members and their .values (e.g: reveal_type(MyEnum.FIRST.value) is already str or more recently Literal['first']).
I've put a more complete demo of this at https://gist.github.com/PeterJCLaw/d4334e498ab1391ee306886748e675f1.
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 damit, die vollständige Demo des Issues zu prüfen und die Behandlung von Enums in mypy und typeshed nachzuverfolgen; das Issue nennt keine bestimmten Dateien oder Tests. Ermittle, wie der Werttyp einer Enum-Variablen aus der Vererbung abgeleitet werden sollte, und füge anschließend Testabdeckung hinzu, die zeigt, dass allgemeine Enum-Instanzen den vorgesehenen Werttyp ohne eine separate Annotation im Klassenrumpf offenlegen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100