装饰器的Mypy类型注释

2024-04-19 09:27:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个decorator类,但在添加类型注释时遇到了问题

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")

Tags: importselfreturnenvironmentdefargsanykwargs
1条回答
网友
1楼 · 发布于 2024-04-19 09:27:00

这是一个不错的解决方案:

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就好了

相关问题 更多 >