python / python/mypy

Errors when calling a function that has **kwargs and default value parameter (non-trivial type)

Offen
#8,862 0 Kommentare 18 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Hello, when I declare a function that has **kwargs along with another parameter with a default value, mypy reports errors when trying to call this function without the default argument.

Repro

def foo(option: bool = None, **kwargs):
    pass

def bar(option: str = '', **kwargs):
    pass


d = {'test': True}

foo(**{'test': True})
foo(**d)
foo(other_option=True, option=True, **d)
foo(option=True, **{'test': True})

bar(**{'test': True})  # Error
bar(**d)  # Error
bar(other_option=True, **d)  # Error
bar(option='Hello, world!', **{'test': True})

Mypy output:

test.py:15: error: Argument 1 to "bar" has incompatible type "**Dict[str, bool]"; expected "str"
test.py:16: error: Argument 1 to "bar" has incompatible type "**Dict[str, bool]"; expected "str"
test.py:17: error: Argument 2 to "bar" has incompatible type "**Dict[str, bool]"; expected "str"
Found 3 errors in 1 file (checked 1 source file)

Other failing inputs

From my observations: default parameters with {bool, int, float} types work fine (like the foo function). But if the default parameter has type of any of the following types:

  • Custom classes
  • Enums
  • bytes
  • str

mypy will report errors.

Expected result

foo and bar should behave the same way (e.g. no errors).

Versions

mypy 0.770 and Python 3.8.2

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, den gemeldeten Reproducer mit dem aktuellen mypy-Testsetup auszuführen, und verfolge, wie Aufrufe mit **kwargs geprüft werden, wenn ein anderer Parameter einen nicht-trivialen Standardtyp hat. Füge eine Regressionstestabdeckung für die gezeigten bar-Aufrufe hinzu und bestätige, dass sie keine Fehler erzeugen, während inkompatible explizite Argumente weiterhin diagnostiziert werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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