python / python/mypy

Weird error message for incompatible override when using a `TypeVar` for `self`

Abierto
#12,786 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-error-reporting topic-inheritance topic-self-types topic-usability
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug Report

Minimal repro:

from typing import TypeVar

Self = TypeVar("Self")

class Flag:
    def __or__(self: Self, other: Self) -> Self: ...
    __ror__ = __or__

class IntFlag(int, Flag):
    def __or__(self: Self, other: int) -> Self: ...
    def __ror__(self: Self, other: int) -> Self: ...

mypy output:

main.py:11: error: Signature of "__ror__" incompatible with supertype "Flag"
main.py:11: note:      Superclass:
main.py:11: note:          def [Self] __or__(self, Self, Self) -> Self
main.py:11: note:      Subclass:
main.py:11: note:          def __ror__(self, int) -> IntFlag

Mypy playground link here: https://mypy-play.net/?mypy=latest&python=3.10&gist=26b60881cd02e608bb9e555db4c3f627

Expected Behavior

I'm not sure an error is even appropriate here: no incompatible override is reported for __or__.

Even if an error is appropriate here, however, the error message is weird and garbled. According to the error message, mypy seems to think that __ror__ in the superclass expects three arguments (it only expects two). It also seems to think the method in the subclass will return IntFlag (not necessarily true; the return type will be whatever self is, and self could be any subtype of IntFlag).

The function that creates this error message is here: https://github.com/python/mypy/blob/9b4bce9065cbef6185fbc77f2849b63dc9e5e293/mypy/messages.py#L824

Mypy version used

  • 0.950.
  • This doesn't seem to be a recent regression: I can reproduce as far back as 0.920, which is the earliest mypy version that has the verbose error messages.
  • I've also reproduced the bug on mypy master.

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 mínima en Python e inspecciona la construcción del diagnóstico en mypy/messages.py alrededor de la línea 824. Compara las firmas informadas de la superclase y la subclase con los tipos self basados en TypeVar y determina después si la sobrescritura debería aceptarse o cómo debería mostrarse su mensaje. Se considera terminado cuando el comportamiento esté corregido y la reproducción esté cubierta por una prueba de regresión.

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.