【发布时间】:2022-01-24 10:46:45
【问题描述】:
定义一个接受Callable[..., int]并返回Callable[..., str]的装饰器似乎无法被mypy理解
def decorator(wrapped: Callable[..., int]) -> Callable[..., str]:
def wrapper() -> str:
return str(wrapped())
return wrapper
@decorator
def foo() -> int:
return 0
def bar():
x = foo() # mypy sees x as int even though the decorator(foo)() returns str
有什么办法可以让foo()的返回类型不是它原来的返回类型,而是装饰器指定的返回类型?
编辑:简化示例
【问题讨论】:
-
您使用哪个版本的 mypy?
mypy 0.790显示没有问题 -
我正在使用
mypy 0.910
标签: python decorator mypy python-typing