python / python/mypy

Inlining a list argument into call of function that accepts a protocol produces "incompatible type" error

Abierto
#21,563 0 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
Merge medio
1 d 18 h
PR fusionados (30 d)
54

Descripción

Bug Report

When passing a list of ints as an argument to a function that accepts a generic protocol, for some reason it is necessary to first bind the list to a variable. Inlining the list as a literal expression in the function call produces a [list-item] error from mypy. What's interesting is that this error references a generic type T, which is not declared anywhere in my program.

To Reproduce

from typing import TypeVar, Protocol, Self

K = TypeVar('K', covariant=True)
class Concattable(Protocol[K]):
    def __add__(self, other: Self) -> Self: ...

J = TypeVar('J')
def duplicate(seq: Concattable[J]) -> Concattable[J]:
    return seq + seq

# No issue
mylist = [1,2,3]
assert duplicate(mylist) == [1,2,3,1,2,3]

# Issue
assert duplicate([1,2,3]) == [1,2,3,1,2,3]


Expected Behavior

I expect that both function calls do not produce any mypy errors.

Actual Behavior

While the first call is fine, the second one produces an error for each element of the list.
It also makes mention of a generic type T, which is not declared anywhere in my program.

construct-new-sequence.py:16: error: List item 0 has incompatible type "int"; expected "T"  [list-item]
construct-new-sequence.py:16: error: List item 1 has incompatible type "int"; expected "T"  [list-item]
construct-new-sequence.py:16: error: List item 2 has incompatible type "int"; expected "T"  [list-item]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

$ pip --version
pip 26.1.1 from /venv/lib/python3.12/site-packages/pip (python 3.12)
$ python --version
Python 3.12.13
$ mypy --version
mypy 2.1.0 (compiled: yes)
$ pip freeze | grep mypy
mypy==2.1.0
mypy_extensions==1.1.0
  • There is no mypy.ini or other mypy configuration files.
  • I invoke mypy simply from the command line by running mypy --strict myfile.py

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 de construct-new-sequence.py y ejecuta mypy --strict, comparando la llamada con una lista vinculada con el literal de lista inline. Rastrea la inferencia del protocolo genérico que produce los diagnósticos inesperados T y [list-item]. Se considera terminado cuando ambas llamadas pasan sin errores y el tipo reportado ya no es espurio.

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
Tranquilo
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.