Inlining a list argument into call of function that accepts a protocol produces "incompatible type" error
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der Reproduktion von construct-new-sequence.py und führe mypy --strict aus, wobei du den Aufruf mit gebundener Liste mit dem Inline-Listenliteral vergleichst. Verfolge die Inferenz des generischen Protokolls, die die unerwarteten T- und [list-item]-Diagnosen erzeugt. Fertig, wenn beide Aufrufe fehlerfrei durchlaufen und der gemeldete Typ nicht mehr fälschlicherweise angezeigt wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100