首页 > 试题广场 >

以下使用ParamSpec的代码,my_decorator(

[单选题]
以下使用ParamSpec的代码,my_decorator(foo)(1, "hello")的返回类型被推断为什么?
from typing import ParamSpec, TypeVar, Callable

P = ParamSpec('P')
R = TypeVar('R')

def my_decorator(func: Callable[P, R]) -> Callable[P, R]:
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
        return func(*args, **kwargs)
    return wrapper

@my_decorator
def foo(x: int, y: str) -> bool:
    return len(y) > x
  • bool
  • Any
  • Callable[[int, str], bool]
  • TypeVar('R')
  • int
  • str

这道题你会答吗?花几分钟告诉大家答案吧!