python / python/mypy

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

Aperta
#8,862 0 commenti 18 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic-calls
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

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 il riproduttore segnalato con l’attuale configurazione dei test di mypy e traccia come vengono controllate le chiamate che usano **kwargs quando un altro parametro ha un tipo predefinito non banale. Aggiungi una copertura di regressione per le chiamate a bar mostrate e conferma che non producano errori, mentre gli argomenti espliciti incompatibili continuano a essere diagnosticati.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.