python / python/mypy

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

Offen
#17,093 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

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

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

Führe zunächst das bereitgestellte Python-Snippet im verlinkten mypy playground mit Python 3.12 und --strict aus und vergleiche dann die Diagnosen für go(ok) und go(error). Als erledigt gilt die Aufgabe, wenn sich der Fall mit ParamSpec und Concatenate konsistent mit der angegebenen Erwartung verhält und durch Regressionstests in mypy abgedeckt ist.

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

Neue Issues direkt in Ihr Postfach

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