python / python/mypy

error: "key" to "max" has incompatible type

Aperta
#21,104 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

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)

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 eseguendo la riproduzione fornita con mypy 3.19.1 e confronta le chiamate inline che falliscono con la forma accettata che usa una variabile intermedia. Traccia il controllo dei tipi di max(..., key=len) e aggiungi un test di regressione che copra sia Z sia C; il lavoro è completato quando la riproduzione passa senza gli errori arg-type segnalati.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
58/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.