Mypy 认为 @classmethod @property 是可调用的
Mypy 似乎无法理解属性不是类的可调用方法:
from abc import abstractmethod
from typing import Type
class A:
pass
class B:
@abstractmethod
@property
@classmethod
def other_type(cls) -> Type[A]:
pass
@classmethod
def __init_subclass__(cls, **kwargs):
assert issubclass(cls.other_type, A)
在上面的示例上运行 mypy 会导致以下错误:
error: Argument 1 to "issubclass" has incompatible type "Callable[[], Type[A]]"; expected "type" [arg-type]
如何在没有 # type:ignore[arg-type] 的情况下强制执行正确的类型?我正在使用 mypy 版本 0.782 和 python 3.7
(注意:我使用 --check-untyped-defs
标志运行 mypy,因此 mypy 应该扫描 __init_subclass__
方法)。
Mypy seems to have trouble understanding a property is not a callable method of a class:
from abc import abstractmethod
from typing import Type
class A:
pass
class B:
@abstractmethod
@property
@classmethod
def other_type(cls) -> Type[A]:
pass
@classmethod
def __init_subclass__(cls, **kwargs):
assert issubclass(cls.other_type, A)
Running mypy on the above example results in the following error:
error: Argument 1 to "issubclass" has incompatible type "Callable[[], Type[A]]"; expected "type" [arg-type]
How can I enforce the correct type, without # type: ignore[arg-type]
? I'm using mypy version 0.782 and python 3.7
(Note: I'm running mypy with the --check-untyped-defs
flag, so mypy should be scanning the __init_subclass__
method).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论