如何编写一个接受任何功能并传递mypy --disallow-any-decorated的装饰器?

问题描述

我最后一次尝试编写一个装饰器,该装饰器接受任何可能的python函数并通过带有--disallow-any-decorated标志的mypy检查,如下所示:

from typing import Any,Callable,TypeVar


T = TypeVar('T')


def decorator(func: Callable[...,T]) -> Callable[...,T]:
    def decorated(*args: Any,**kwargs: Any) -> Any:
        print('decorated')
        return func(*args,**kwargs)

    return decorated


@decorator
def foo() -> int:
    print('foo')
    return 42


print(foo())

但是它仍然以Type of decorated function contains type "Any" ("Callable[...,int]")

失败

我在做什么错?我还尝试使用VarArg中的KwArgmypy_extensions而不是...,但这无济于事。

解决方法

--disallow-any-decorated

在装饰器转换后不允许签名中带有Any的函数。

...中的

Callable[...,T]是一种表示可调用对象接受零个或多个Any自变量的方式。因此,即使声明本身不包含Any,它仍然可以解析为包含Any的声明。

,

使用TypeVar吞下整个Callable,而不仅仅是返回类型。然后,您需要向MyPy阐明修饰函数的类型与原始函数相同,从而不必担心“哦,但是这里的返回类型几乎可以是任何东西。”

可能有必要在最终返回线上使用强制转换。

FuncT = TypeVar(FuncT,bound=Callable)

def decorator(func: FuncT) -> FuncT:
   ...
   return cast(FuncT,decorated)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...