python / python/mypy

Errors for union of callables could be better

Offen
#15,048 2 Kommentare 1 Reaktion 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

Given this definition (playground):

f: Callable[[int, int], None] | Callable[[int, str], None]

this code results in the following errors:

f(5, 42)  # E: Argument 2 has incompatible type "int"; expected "str"  [arg-type]
f(5, '42')  # E: Argument 2 has incompatible type "str"; expected "int"  [arg-type]

Looking at the errors (which are probably emitted in a for-loop) one would miss the big picture, which is that it's a union of two incompatible types.

Perhaps we could "meet" the callables, thus resulting in:

f(5, 42)  # E: Argument 2 has incompatible type "int"; expected <nothing>  [arg-type]
f(5, '42')  # E: Argument 2 has incompatible type "str"; expected <nothing>  [arg-type]

Notably we handle defaults well, i.e. in this code:

class FInt:
    def __call__(self, a: int, b: int=42) -> None:
        return

class FStr:
    def __call__(self, a: int, b: str='42') -> None:
        return

f: FInt | FStr

we get

f(5, 42)  # E: Argument 2 to "__call__" of "FStr" has incompatible type "int"; expected "str"  [arg-type]
f(5, '42')  # E: Argument 2 to "__call__" of "FInt" has incompatible type "str"; expected "int"  [arg-type]

but no error on

f(5)

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 mit den beiden verknüpften mypy playground-Beispielen und vergleiche die Diagnosen für Vereinigungen aufrufbarer Typen, einschließlich des Falls mit einem Standardargument. Verfolge den Pfad der Argumentprüfung für Callables, der die gemeldeten Fehler erzeugt. Die Aufgabe ist abgeschlossen, wenn die Diagnosen vermitteln, dass inkompatible Callable-Signaturen nicht erfüllt werden können, während das gültige Verhalten des Standardarguments erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
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.