python / python/mypy

Generics is not supported with some operators

Abierto
#6,157 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug priority-1-normal topic-type-variables topic-union-types
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

  • Are you reporting a bug, or opening a feature request?
    Bug
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

Supporting operators with union does not work with generics.

S = TypeVar('S', bound=Union[int, float])
T = TypeVar('T', bound=Union[int, float])

def some_function(a:S, b:T) -> S:
    // do some operation and returns the type based on first parameter.
     c = a + b
    // if there is an addition involved in the computation, then we would get the below error. 
    // some_computed_variable would be finally computed and has the value based on first parameter type.
    return some_computed_variable

  • What is the actual behavior/output?
    error: Unsupported operand types for + ("S" and "T")

    We can use alias (Union[int, float] as type) instead of generics (S) as a workaround but it loses generic support.

  • What is the behavior/output you expect?
    Should not report error

  • What are the versions of mypy and Python you are using?
    Python: 3.6 mypy: 0.650

  • What are the mypy flags you are using? (For example --strict-optional)
    --ignore-missing-imports

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 reproduciendo el caso de operador genérico del issue con un checkout actual de mypy y, después, revisa el cambio supporting-operators-with-union en PR #5545. Sigue cómo se comprueban las variables de tipo acotadas para la suma y añade cobertura que muestre que se aceptan operandos genéricos compatibles sin perder el tipo de retorno basado en el primer parámetro.

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
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.