python / python/mypy

mypy doesn't understand function signature compatibility

Aperta
#11,807 1 commento 2 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

mypy doesn't understand the signature compatibility between functions when determining valid uses in lists. This means mypy can miss type errors.

To Reproduce
The following code type checks despite containing a type error that will lead to runtime failure. The expectation was that appending to the list will show a type error or warning for the incompatible signatures.

funclist = [lambda x: print(1)]
funclist += [lambda k: print(2)]  # incompatible signature, but type not widened and no error
reveal_type(funclist)  # builtins.list[def (x: Any)]
funclist[1](x=4)  # this will error at runtime because of a typing mistake

Expected Behavior
Type error. I compared to pyright and pyright does show a type error for the incompatible signatures. Other possible behaviors would be to widen the type of funclist on line 1 or 2 to no longer think the first argument has keyword variable named x, but this is probably a bit odd/inconsistent. In this case mypy would be able to throw an error on line 4 with the keyword argument usage. Normally you would need to annotate funclist when it's defined if you wanted the wider behavior.

Actual Behavior
no type error

Your Environment

  • Mypy version used: 0.920
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10
  • Operating system and version: Gentoo/Linux stable

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 lo snippet Python fornito con mypy 0.920 e confrontando il risultato con pyright. Traccia il modo in cui le espressioni di lista e le assegnazioni aumentate inferiscono le firme degli oggetti richiamabili, quindi verifica che l'argomento con nome incompatibile venga rifiutato oppure che il tipo della lista venga ampliato come descritto.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.