error: "key" to "max" has incompatible type
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
mypy fails to typecheck set(max(S, key=len)) in the below code. Splitting it into first computing X = max(S, key=len) and then computing set(X) passes the type checks. The problem persists when providing type annotations.
To Reproduce
S = [[1, 2], [3]]
X = max(S, key=len)
Y = set(X)
Z = set(max(S, key=len))
print(Y, Z)
L : list[list[int]] = [[1, 2], [3]]
A : list[int] = max(L, key=len)
B : set[int] = set(A)
C : set[int] = set(max(L, key=len))
print(B, C)
Running with Python 3.14.3 gives the correct output
{1, 2} {1, 2}
{1, 2} {1, 2}
Running with mypy 3.19.1 reports the following errors for the lines computing Z and C.
mypy_max.py:5: error: Argument "key" to "max" has incompatible type "Callable[[Sized], int]"; expected "Callable[[Iterable[int]], SupportsDunderLT[Any] | SupportsDunderGT[Any]]" [arg-type]
mypy_max.py:13: error: Argument "key" to "max" has incompatible type "Callable[[Sized], int]"; expected "Callable[[Iterable[int]], SupportsDunderLT[Any] | SupportsDunderGT[Any]]" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
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 damit, die bereitgestellte Reproduktion mit mypy 3.19.1 auszuführen, und vergleiche die fehlschlagenden Inline-Aufrufe mit der akzeptierten Form mit Zwischenvariable. Verfolge die Typprüfung von max(..., key=len) und füge einen Regressionstest hinzu, der sowohl Z als auch C abdeckt; abgeschlossen ist die Aufgabe, wenn die Reproduktion ohne die gemeldeten arg-type-Fehler erfolgreich ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 58/100