如何解决装饰器的 Mypy 类型注释
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")
解决方法
这是一个不错的解决方案:
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)
很丑。 - 输入 args 和 kwargs 会很好。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。