python / python/mypy

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

Aperta
#21,563 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione di construct-new-sequence.py ed esegui mypy --strict, confrontando la chiamata con lista associata con il literal di lista inline. Traccia l’inferenza del protocollo generico che produce le diagnosi inattese T e [list-item]. Il lavoro è completato quando entrambe le chiamate passano senza errori e il tipo riportato non è più spurio.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.