ParamSpec: use P.args and P.kwargs in other scopes, such as return types
Nobody has claimed this yet.
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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