python / python/mypy

ParamSpec with Concatenate issue when function has union and kw args with strict=True

Aperta
#17,093 0 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

Hi,

Found a strange issue triggered by strict only when kw-args are present in the function.
Below I believe that both function should either pass or fail (I think the correct behavior is pass here)
For reference pyright finds no issues in this snippet

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

# Ideally, a small sample program that demonstrates the problem.
# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)

from typing import Any, Callable, Concatenate, ParamSpec, TypeVar
P = ParamSpec("P")
T = TypeVar("T")

def go(x: Callable[Concatenate[str, P], T], *a: P.args, **k: P.kwargs) -> T:
    return x("foo", *a, **k)

def ok(a: str | int, one: int) -> int:
    return 1

def error(a: str | int, one: int, **kw: Any) -> int:
    return 2

go(ok, 1)
go(error, 1)

mypy playground link here: https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=22221f205a86573a0c4db8cb529dc824

Expected Behavior

No error is reported

Actual Behavior

main.py:15: error: Argument 1 to "go" has incompatible type "Callable[[str | int, int, KwArg(Any)], int]"; expected "Callable[[str, int, KwArg(Any)], int]"  [arg-type]
main.py:15: note: This is likely because "error" has named arguments: "a". Consider marking them positional-only
Found 1 error in 1 file (checked 1 source file)

Your Environment

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

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 lo snippet Python fornito nel mypy playground collegato con Python 3.12 e --strict, quindi confronta le diagnosi per go(ok) e go(error). Il lavoro è concluso quando il caso ParamSpec e Concatenate si comporta in modo coerente con l’aspettativa indicata e dispone di una copertura di regressione in mypy.

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
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.