Skip to content

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

Closed as not planned
@crusaderky

Description

@crusaderky

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions