python / python/mypy

mypy doesn't understand function signature compatibility

Offen
#11,807 1 Kommentar 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das bereitgestellte Python-Snippet mit mypy 0.920 auszuführen und sein Ergebnis mit pyright zu vergleichen. Verfolge, wie Listenausdrücke und erweiterte Zuweisungen Signaturen aufrufbarer Objekte ableiten, und überprüfe dann, dass das inkompatible Schlüsselwortargument abgelehnt wird oder der Listentyp wie beschrieben erweitert wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.