python / python/mypy

Protocol incorrectly matches when positional or keyword parameter matches *args

Aperta
#21,014 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

When a protocol defines a function with a keyword or positional parameter (a: int) -> None, a concrete function where said parameter is missing, but matches the *args parameter (*args: int) -> None will be seen as implementing the protocol.

I would expect (a: int) -> None to also need matching **kwargs such as (*args: int, **kwargs: int) -> None and for it to only match *args alone when specified as a positional only argument (e.g. (a: int, /) -> None)

Otherwise it cannot be called safely, pyright and ty both successfully raise errors for this case.

To Reproduce

https://mypy-play.net/?mypy=master&python=3.14&flags=strict&gist=a5f86e32e70cbc84c45a53db5426f78f

Expected Behavior

main.py:13: error: Argument 1 to "f" has incompatible type "C"; expected "P"  [arg-type]

because calling the method as foo(a=1) would fail.

Actual Behavior

mypy:

Success: no issues found in 1 source file

pyright:

Argument of type "C" cannot be assigned to parameter "o" of type "P" in function "f"
  "C" is incompatible with protocol "P"
    "foo" is an incompatible type
      Type "(*args: int) -> None" is not assignable to type "(a: int) -> None"
        Missing keyword parameter "a"  (reportArgumentType)

ty:

Argument to function `f` is incorrect: Expected `P`, found `C` (invalid-argument-type) [Ln 17, Col 3]

python:

Traceback (most recent call last):
  File "xxx.py", line 17, in <module>
    f(C())
    ~^^^^^
  File "xxx.py", line 14, in f
    o.foo(a=1)
    ~~~~~^^^^^
TypeError: C.foo() got an unexpected keyword argument 'a'

Your Environment

  • Mypy version used: 1.19.1 and master
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 1.14

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

Riprodurre l'esempio mypy-play collegato usando mypy master, Python 3.14 e --strict. Tracciare la gestione della compatibilità dei protocolli per le firme foo mostrate e confrontare il risultato con pyright e ty. Il lavoro è completato quando mypy segnala l'errore incompatible-protocol previsto, preservando il caso positional-only.

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.