【问题标题】:With mypy, how do I type annotate a decorator that decorates a function that accepts a subclass of something?使用 mypy,我如何键入注释一个装饰器,该装饰器装饰一个接受某物子类的函数?
【发布时间】:2019-12-19 07:49:03
【问题描述】:

对于完整的上下文,我希望制作一些对测试进行更好的静态分析的装饰器。在理想的世界中,它会像这样工作:

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 由于逆变的正确行为,但这并不能帮助我解决我的问题。

有没有什么方法可以让装饰器优雅地工作而无需使用Any 显式破解类型?

【问题讨论】:

    标签: python types static-analysis mypy


    【解决方案1】:

    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):
            ...
    

    【讨论】:

      猜你喜欢
      • 2019-11-13
      • 2011-09-03
      • 2023-03-23
      • 1970-01-01
      • 2022-10-13
      • 2022-08-15
      • 1970-01-01
      • 2011-10-04
      • 2017-11-16
      相关资源
      最近更新 更多