python / python/mypy

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

Open
#17,093 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Python snippet in the linked mypy playground with Python 3.12 and --strict, then compare the diagnostics for go(ok) and go(error). Done means the ParamSpec and Concatenate case behaves consistently with the stated expectation and has regression coverage in mypy.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.