绑定方法的 MyPy 类型注释?

Sor*_*tad 6 python-3.x mypy

我正在使用pytest. 我想为库公开的每个函数尝试多个测试用例,因此我发现将类中每个方法的测试分组很方便。我要测试的所有函数都具有相同的签名并返回相似的结果,因此我想使用超类中定义的辅助方法对结果进行一些断言。简化版本会像这样运行:

class MyTestCase:
    function_under_test: Optional[Callable[[str], Any]] = None

    def assert_something(self, input_str: str, expected_result: Any) -> None:
        if self.function_under_test is None:
            raise AssertionError(
                "To use this helper method, you must set the function_under_test"
                "class variable within your test class to the function to be called.")

        result = self.function_under_test.__func__(input_str)
        assert result == expected_result
        # various other assertions on result...


class FunctionATest(MyTestCase):
    function_under_test = mymodule.myfunction

    def test_whatever(self):
        self.assert_something("foo bar baz")
Run Code Online (Sandbox Code Playgroud)

在 中assert_something,有必要调用__func__()该函数,因为将函数分配给类属性使其成为该类的绑定方法——否则self将作为第一个参数传递给外部库函数,在那里它没有任何意义.

此代码按预期工作。但是,它会产生 MyPy 错误:

"Callable[[str], Any]" has no attribute "__func__"
Run Code Online (Sandbox Code Playgroud)

根据我的注释,这不是安全操作是正确的:任意 Callable 可能没有__func__属性。但是,我找不到任何类型注释表明该function_under_test变量引用了一个方法,因此将始终具有__func__. 我是否忽略了一个,或者是否有另一种方法来调整我的注释或访问以使其与类型检查一起工作?

当然,还有很多其他方法可以解决这个问题,其中一些甚至可能更简洁(使用Any类型,跳过类型检查,使用私有方法返回被测函数而不是将其设为类变量,使辅助方法、函数等)。我更感兴趣的是是否有注释或其他 mypy 技巧可以使此代码正常工作。

Ces*_*esc 0

Callable仅确保您的对象具有该__call__方法。

你的问题是你的电话self.function_under_test.__func__(input_str)你应该只调用你的函数self.function_under_test(input_str)

请参阅下面的示例,没有 mypy 投诉(v0.910)

from typing import Any, Callable, Optional


class MyTestCase:
    function_under_test: Optional[Callable] = None

    def myfunction_wrap(self, *args, **kwargs):
        raise NotImplementedError

    def assert_something(self, input_str: str, expected_result: Any) -> None:
        if self.function_under_test is None:
            raise AssertionError(
                "To use this helper method, you must set the function_under_test"
                "class variable within your test class to the function to be called.")

        result = self.myfunction_wrap(input_str)
        assert result == expected_result
        # various other assertions on result...


def myfunction(a: str) -> None:
    ...


class FunctionATest(MyTestCase):
    def myfunction_wrap(self, *args, **kwargs):
        myfunction(*args, **kwargs)

    def test_whatever(self):
        self.assert_something("foo bar baz")

Run Code Online (Sandbox Code Playgroud)

Edit1:错过了问题的要点,将函数移动到包装函数内