python / python/mypy

Better message for overloaded functions with required kwargs

Offen
#16,528 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature topic-overloads topic-usability
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Feature

The following:

from typing import overload, Union

def fun(a: Union[int, str], *, b: bool) -> None:
    pass

fun(1, True)

says helpfully:

main.py:6: error: Too many positional arguments for "fun"  [misc]
Found 1 error in 1 file (checked 1 source file)

mypy-playground

In the following case however:

from typing import overload, Union

@overload
def fun(a: int, *, b: bool) -> None: ...

@overload
def fun(a: str, *, b: bool) -> None: ...

def fun(a: Union[int, str], *, b: bool) -> None:
    pass

fun(1, True)

we get:

main.py:12: error: No overload variant of "fun" matches argument types "int", "bool"  [call-overload]
main.py:12: note: Possible overload variants:
main.py:12: note:     def fun(a: int, *, b: bool) -> None
main.py:12: note:     def fun(a: str, *, b: bool) -> None
Found 1 error in 1 file (checked 1 source file)

mypy-playground

I think the error should also point out that the number of positional arguments is incorrect.

Pitch

I found myself confused by this a couple of times and have wasted good few hours trying to understand what's wrong with the call which is clearly correct, and just glancing straight past the * - something that's easy to do with a longer / more complicated function signature. I think it would save a meaningful amount of time for users and it feels like a simple change.

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

Reproduziere beide Python-Beispiele in den verknüpften Fällen von mypy-playground und vergleiche ihre Diagnosen. Als erledigt gilt die Aufgabe, wenn der Fehler der überladenen Funktion zusätzlich erklärt, dass die Anzahl der Positionsargumente ungültig ist, während die Details der Überladungen erhalten bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

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