python / python/mypy

Support enum member `value` types

Abierto
#8,722 6 comentarios 20 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature priority-2-low topic-enum
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza revisando la demo completa del issue y siguiendo el manejo de enums en mypy y typeshed; el issue no identifica archivos ni tests específicos. Determina cómo debe inferirse el tipo de valor de una variable enum a partir de la herencia y, después, añade cobertura que demuestre que las instancias generales de enum exponen el tipo de valor previsto sin una anotación independiente en el cuerpo de la clase.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.