在为函数分配新属性时,如何抑制 mypy 中的“没有属性”错误?

问题描述 投票:0回答:1

我经常使用以下习惯用法进行静态初始化:

def compute_answer() -> int:
    if compute_answer.ret is None:
        # Do stuff that only happens the first time
        compute_answer.ret = 42
    return compute_answer.ret

compute_answer.ret = None

但是,使用 mypy 进行类型检查会出现以下错误:

compute.py:2: error: "Callable[[], int]" has no attribute "ret"
compute.py:4: error: "Callable[[], int]" has no attribute "ret"
compute.py:5: error: "Callable[[], int]" has no attribute "ret"
compute.py:7: error: "Callable[[], int]" has no attribute "ret"

如何抑制这些错误,尤其是本地错误(例如,仅此函数/属性)?

python python-typing mypy
1个回答
3
投票

您可以使用为函数对象返回自定义

Protocol
的装饰器。像这样:

from typing import Any, Protocol, Optional


class ComputeAnswerProto(Protocol):
    ret: Optional[int]

    def __call__(self) -> int: ...


def compute_decorator(func: Any) -> ComputeAnswerProto:
    return func


@compute_decorator
def compute_answer() -> int:
    if compute_answer.ret is None:
        # Do stuff that only happens the first time
        compute_answer.ret = 42
    return compute_answer.ret


compute_answer.ret = None
© www.soinside.com 2019 - 2024. All rights reserved.