python / python/mypy

Typealias-like definition of enum members makes mypy forget we're in an enum

Abierto
#14,281 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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.

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

Comienza con el reproductor de Python del issue y ejecútalo mediante el ejemplo enlazado de mypy-play. Compara los tipos revelados para E.A, E.B y E.C con los valores en tiempo de ejecución y sus llamadas a .value; se considera terminado cuando los miembros del enum conservan tipos de enum, mientras que sus valores reciben los tipos apropiados sin errores de atributo incorrectos.

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

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.