python / python/mypy

Misleading warning message when using ABCMeta instead of Protocol

Abierto
#14,825 2 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

To Reproduce

I run this code, expecting it to type-check:

https://mypy-play.net/?mypy=latest&python=3.11&gist=d4c1190de2b786aea78cc801ee2e7829

Expected Behavior

I would expect an error perhaps like this:

main.py:42: error: Argument "key" to "max_if" has incompatible type "Callable[[str], Optional[int]]"; expected "Callable[[str], Optional[S]]" where S is a subclass of "Comparable"  [arg-type]

Actual Behavior

main.py:42: error: Argument "key" to "max_if" has incompatible type "Callable[[str], Optional[int]]"; expected "Callable[[str], None]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Note

This was very confusing. While investigating this bug, I found out that I had to define Comparable using typing.Protocol instead of ABCMeta, and then I got the code to type-check.

I guess that since mypy tries to find S which is Comparable, so that Optional[int] would match S | None, and since it recognizes that int isn't a subclass of Comparable, it is left with the None and this is what it shows in the error message. However, this is very confusing.

Your Environment

  • Mypy version used: 1.0.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11

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 la reproducción enlazada en mypy-play y compara los diagnósticos producidos cuando Comparable usa ABCMeta frente a typing.Protocol. Sigue el recorrido de la inferencia de tipos y de la notificación de errores para el argumento de clave max_if. Se considera completado cuando el caso de ABCMeta produce un diagnóstico que explica correctamente la incompatibilidad en lugar de mostrar Callable[[str], None].

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

Evaluación

Stack tecnológico
python
Área
compilers
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.