Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ParamSpec when the wrapper has more keyword-only arguments than wrapped function #16479

Closed
crusaderky opened this issue Nov 13, 2023 · 2 comments
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate topic-usability

Comments

@crusaderky
Copy link
Contributor

PEP-612 explains how Concatenate can be used to write a wrapper where the output function requires less arguments than the wrapped one.
The opposite, where the output function requires more arguments than the wrapped one, is not clarified by the PEP, nor in my opinion it should, as it seems to be fairly non-controversial.

These work fine with mypy 1.7.0:

from typing import Callable, ParamSpec, TypeVar
P = ParamSpec("P")
R = TypeVar("R")

def w1(f: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
    return f(*args, **kwargs)

def w2(f: Callable[P, R], extra_arg: int, *args: P.args, **kwargs: P.kwargs) -> R:
    return f(*args, **kwargs)

This however doesn't:

def w3(f: Callable[P, R], *args: P.args, extra_arg: int, **kwargs: P.kwargs) -> R:
    return f(*args, **kwargs)
error: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"  [valid-type]
@crusaderky crusaderky added the bug mypy got something wrong label Nov 13, 2023
@erictraut
Copy link

PEP 612 expressly forbids the signature in w3 in your example. Here's the exact quote:

Placing keyword-only parameters between the *args and **kwargs is forbidden.

The "rejected alternatives" section of the PEP gives some indication of why this limitation was added.

Mypy should generate an error for w3, but it doesn't currently.

@AlexWaygood AlexWaygood added topic-paramspec PEP 612, ParamSpec, Concatenate topic-usability labels Nov 13, 2023
@ilevkivskyi
Copy link
Member

Duplicate of #13966

@ilevkivskyi ilevkivskyi marked this as a duplicate of #13966 Nov 13, 2023
@ilevkivskyi ilevkivskyi closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate topic-usability
Projects
None yet
Development

No branches or pull requests

4 participants