python / python/mypy

Better error message for incompatible type assignment when using Generics

Abierto
#8,934 9 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

diagnostics needs discussion priority-1-normal
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?

Feature Request

  • Please insert below the code you are checking with mypy.
from typing import Generic, TypeVar


T = TypeVar("T", int, float)


class Foo(Generic[T]):
    def __init__(self, val: T):
        self.data: T = val 


def bar(x: T) -> Foo[T]:
    return Foo(x)


x = bar(1)
x = bar(2.0) # error thrown by mypy
  • What is the actual behavior/output?
    mypy throws this error:
error: Argument 1 to "bar" has incompatible type "float"; expected "int"
  • What is the behavior/output you expect?

This error message is thrown because of the combination of assigning a different type to x and using Generic. The error message is unclear because it indicates a type error on the argument passed to bar and can be interpreted as bar not accepting arguments of type float.

Having a different error message that mentions "Incompatible types in assignment" would be clearer.
A message to make it clearer that expression has type Foo[float], variable has type Foo[int]

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

mypy 0.770 and Python 3.7.6

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

None

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 ejemplo Generic con mypy 0.770 y Python 3.7.6, centrándote en la asignación de Foo[int] a Foo[float]. Rastrea el diagnóstico producido para la llamada a bar y la asignación a x; se considera terminado cuando el mensaje identifica claramente los tipos incompatibles de la variable y la expresión, en lugar de dar a entender que bar rechaza argumentos float.

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

Evaluación

Stack tecnológico
python
Área
compilers
Tipo de issue
Nueva funcionalidad
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.