对于完整的上下文,我希望制作一些装饰器,为测试做更好的静态分析。在一个理想的世界里,它的工作原理是这样的:
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
对类型进行黑客攻击?你知道吗
你说得对,mypy检查失败是因为
Callable
是反变的。 它可以通过使用类型变量来修复。你知道吗相关问题 更多 >
编程相关推荐