在mypy中,如何为修饰接受某个函数的子类的函数的修饰器输入注释?

2024-04-19 20:46:30 发布

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

对于完整的上下文,我希望制作一些装饰器,为测试做更好的静态分析。在一个理想的世界里,它的工作原理是这样的:

class SomeTest(unittest.TestCase):
    @login_decorate
    def test_login(self):
        reveal_type(self.user)  # type: User

    @anonymous_decorate
    def test_anonymous(self):
        reveal_type(self.user)  # type: None

刚开始的时候,我正试着创建一个类似这样的装饰器:

def login_decorate(func: Callable[[unittest.TestCase], None]):
    def decorated_function(self: unittest.TestCase):
        self.user = User()
        return func(self)
    return decorated_function

但是当我运行mypy时,我得到了一个错误:

error: Argument 1 to "login_decorate" has incompatible type "Callable[[SomeTest], None]";
expected "Callable[[TestCase], None]"

经过一点思考,我同意这是正确的行为为mypy由于逆变,但这并不能帮助我解决我的问题。你知道吗

有没有办法让decorator优雅地工作,而不必显式地用Any对类型进行黑客攻击?你知道吗


Tags: testselfnonedeftypelogin装饰unittest
1条回答
网友
1楼 · 发布于 2024-04-19 20:46:30

你说得对,mypy检查失败是因为Callable是反变的。 它可以通过使用类型变量来修复。你知道吗

import unittest
from typing import Callable, TypeVar

T = TypeVar('T', bound=unittest.TestCase)


def login_decorate(func: Callable[[T], None]):
    def decorated_function(self: T):
        ...
        return func(self)
    return decorated_function


class SomeTest(unittest.TestCase):
    @login_decorate
    def test_login(self):
        ...

相关问题 更多 >