-
Hello, I wrote a decorator code adds custom keyword-only argument on the function using import typing
P = typing.ParamSpec("P")
Ret = typing.TypeVar("Ret", covariant=True) # Mypy complains this should be covariant
class CustomFunctionProtocol(typing.Protocol[P, Ret]):
def __call__(
self,
*args: P.args,
special_kwarg: str,
**kwargs: P.kwargs,
) -> Ret: ...
def decorator(f: typing.Callable[P, Ret]) -> CustomFunctionProtocol[P, Ret]:
def inner_func(*args: P.args, special_kwarg: str, **kwargs: P.kwargs) -> Ret:
print("Special kwarg given: ", special_kwarg)
return f(*args, **kwargs)
return inner_func
@decorator
def example_function(a: int, b: int, c: int) -> int:
return a + b + c
typing.reveal_type(example_function) In this example code, When I run
However, running
When I try to use that function on code, VSCode intellisense does not reflect the Perhaps showing correct intellisense for generic |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There's no way to do this currently, even the You can perform slightly more complex parameter list extensions using This was a deliberate design choice of PEP-612 to keep the scope of the change smaller and easier to understand and implement. Even with the simplification it proved to be one of the most challenging changes for type checkers to implement. It took quite a while for support in mypy to land and in pytype there's still no full
|
Beta Was this translation helpful? Give feedback.
There's no way to do this currently, even the
Protocol
way doesn't work and pyright is correct to complain. You are only allowed prepend positional only parameters usingtyping.Concatenate
and*args: P.args
must always be directly followed by**kwargs: P.kwargs
in a signature.You can perform slightly more complex parameter list extensions using
TypeVarTuple
, but this is once again only useful for positional-only arguments.This was a deliberate design choice of PEP-612 to keep the scope of the change smaller and easier to understand and implement. Even with the simplification it proved to be one of the most challenging changes for type checkers to implement. It took quite a while for sup…