python / python/mypy

Unexpected incompatible types in assignment (Failed in recursive type checking, nested protocol with generic type)

Abierto
#16,612 9 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
Here I write some nested protocol with generic type.

The basic protocol classes with generic type succeed to match the classes written in the pyi file, like this:

t: Tempo[int] = core.TempoTick(0, 0) #  pass
t: Temop = core.TemoTick(0,0)        #  pass

However, for upper layer class, list List[Tempo], it just started to fail (with generic), like this:

Note that List is a self defined protocol here for classes generated by pybind11 bind_vector

tempos2: List[Tempo] = core.TempoTickList()     #  not pass
tempos3: List = core.TempoTickList()            #  pass

图片

The full errors are listed here:

Incompatible types in assignment (expression has type "TempoTickList", variable has type "symusic.types.List[Tempo[Any]]")Mypy[assignment](https://mypy.readthedocs.io/en/latest/_refs.html#code-assignment)
Following member(s) of "TempoTickList" have conflicts:
    Expected:
        @overload
        def __setitem__(self, int, Tempo[Any], /) -> None
        @overload
        def __setitem__(self, slice, TempoTickList, /) -> None
    Got:
        @overload
        def __setitem__(self, int, TempoTick, /) -> None
        @overload
        def __setitem__(self, slice, TempoTickList, /) -> None
    Expected:
        def append(self, x: Tempo[Any]) -> None
    Got:
        def append(self, x: TempoTick) -> None
    <1 more conflict(s) not shown>Mypy

Although Tempo and Tempo[int] have matched TempoTick, they are said failed to match in the errors.

Even if I remove some of the nested generic type like below, it still failed:

class Score(Protocol[TimeDtype]):
    ticks_per_quarter: int
    tracks: List
    key_signatures: List
    time_signatures: List
    tempos: List
    lyrics: List
    markers: List

图片

By the way, in pycharm, it says, all of these classes fail to match, quite different from mypy. And different from mypy, pycharm don't offer me any useful debug information. (But pycharm works as expected in my following test while mypy failed. So I guess there are at least 2 problems in total.)

图片

To Reproduce
I put my codes here (just a few files):
symusic.zip

And the code in picture is at the bottom of the types.py. I just use the vscode MyPy Type Checker plugin.

Expected Behavior

Well, I just expect it to match.

Actual Behavior

They don't match when the protocol with generic type is nested

Your Environment

  • Mypy version used: mypy 1.7.1 (compiled: yes)
  • Mypy command-line flags: default in vscode
  • Mypy configuration options from mypy.ini (and other config files): default
  • Python version used: 3.11.6

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 adjunta symusic.zip, especialmente con el código al final de types.py, y ejecútala con mypy 1.7.1 usando la configuración predeterminada. Compara las asignaciones de protocolos genéricos anidados con los errores informados; se considera terminado cuando TempoTickList y las asignaciones de protocolos relacionadas se acepten de forma coherente con los casos más simples.

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.