How to Annotate Call Decorator with Fixed Position-only Arguments Followed by Arbitrary Arguments? #8778
-
Hello, I need help on annotating a call decorator using param spec. It used to work with older version of pyright. However in 1.1.376, it fails type-checking. Pardon me if I did something wrong or this has been discussed before. If this is a bug in pyright, feel free to convert this dicussion into an issue. This is a simplification on what I'm trying to do in a bigger project. So I have call decorator that accepts 1 argument to decorate a function with 2 fixed positional-only arguments, followed by additional arbitrary positional or keyword arguments. This is my attempt: # Filename: test_psec.py
import collections.abc
from typing import Protocol
class CallParam[**P](Protocol):
def __call__(
self, context: str, userid: int, /, *args: P.args, **kwds: P.kwargs
) -> collections.abc.Awaitable[str]: ...
def call_decorator(num_type: int, /):
def wrapper0[**P](func: CallParam[P]):
async def wrapper1(context: str, userid: int, /, *args: P.args, **kwargs: P.kwargs) -> str:
return f"calling with num: {num_type}; " + await func(context, userid, *args, **kwargs)
return wrapper1
return wrapper0
@call_decorator(1)
async def call_num_type_1(context: str, userid: int, /):
return f"{context} {userid} type 1"
@call_decorator(2)
async def call_num_type_2(context: str, userid: int, /, mul: int):
return f"{context} {userid} = {userid * mul} type 2" Then I have another Python file that uses # Filename: main.py
import test_pspec
async def main():
result = [
await test_pspec.call_num_type_1("Hello", 12345),
await test_pspec.call_num_type_2("World", 2531011, 214013),
]
print("\n".join(result))
if __name__ == "__main__":
import asyncio
asyncio.run(main()) When I run pyright command-line to
The reason I'm unable to fix this is, if I merge Any pointers on how to do what I need? Or is this is a bug in pyright? Or is it impossible due to limitation in Python typing spec? I appreciate any inputs, thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Thanks for reporting. This is definitely a bug. Converting to a bug report. |
Beta Was this translation helpful? Give feedback.
Thanks for reporting. This is definitely a bug. Converting to a bug report.