python / python/typing

ParamSpec: use P.args and P.kwargs in other scopes, such as return types

Open
#1,252 14 comments 25 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

I have a number of cases where it would be very useful to use P.args and P.kwargs of a ParamSpec to annotate tuple and dict objects, for example, when extracting arguments to pass to a function in another context.

Here's an example:

from typing import Callable, TypeVar, Tuple, Any
from typing_extensions import ParamSpec

P = ParamSpec('P')
T = TypeVar('T')


def complex(value: str, reverse: bool =False, capitalize: bool =False) -> str:
    if reverse:
        value = str(reversed(value))
    if capitalize:
        value = value.capitalize()
    return value


def call_it(func: Callable[P, T], args: P.args, kwargs: P.kwargs) -> T:
    print("calling", func)
    return func(*args, **kwargs)


def get_callable_and_args() -> Tuple[Callable[P, Any], P.args, P.kwargs]:
    return complex, ('foo',), {'reverse': True}


call_it(*get_callable_and_args())

In this scenario P.args and P.kwargs represent a kind of ad-hoc NamedTuple and TypedDict, respectively.

Does this seem like a reasonable extension for ParamSpecs?

Contributor guide

No contributing guide indexed for this repository

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 reviewing the ParamSpec behavior described in the issue and the example using P.args and P.kwargs in a return type. No source files or tests are named; done would require deciding whether this extension is supported and identifying the specification, implementation, or documentation changes needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.