我有一个装饰器类,但无法为其添加类型注释。
import functools
class LogInfo:
def __init__(self, environment: str):
self.environment = environment
def __call__(self, func):
@functools.wraps(func)
def decorated(*args, **kwargs):
# My Stuff goes here...
return func(*args, **kwargs)
return decorated
我能得到的最接近的是:
import functools
from collections import Callable
from typing import TypeVar, Any
GenericReturn = TypeVar("GenericReturn")
GenericCallable = TypeVar("GenericCallable", bound=Callable[..., GenericReturn])
class LogInfo:
def __init__(self, environment: str) -> None:
self.environment = environment
def __call__(self, func: GenericCallable) -> GenericCallable:
@functools.wraps(func)
def decorated(*args: Any, **kwargs: Any) -> GenericReturn:
# My Stuff goes here...
return func(*args, **kwargs)
return decorated # LINE 29
但我仍然收到此错误:
29: error: Incompatible return value type (got "Callable[..., Any]", expected "GenericCallable")
删除 @functools.wraps(func)
会将错误更改为:
29: error: Incompatible return value type (got "Callable[[VarArg(Any), KwArg(Any)], GenericReturn]", expected "GenericCallable")
答案 0 :(得分:0)
这是一个不错的解决方案:
import functools
from collections import Callable
from typing import TypeVar, cast, Any
T = TypeVar("T", bound=Callable[..., Any])
class LogInfo:
def __init__(self, environment: str):
self.environment = environment
def __call__(self, func: T) -> T:
@functools.wraps(func)
def decorated(*args, **kwargs): # type: ignore
# My Stuff goes here...
return func(*args, **kwargs)
return cast(T, decorated)
我们可以使用以下代码进行测试:
@LogInfo(environment="HI")
def foo(input: str) -> str:
return f"{input}{input}"
# NOTE: Intentional error here to trigger mypy
def bar() -> int:
return foo("jake")
正如预期的那样,我们收到此 mypy
错误:
error: Incompatible return value type (got "str", expected "int")
还有待改进的地方:
return cast(T, decorated)
很丑。