Mypy 认为 @classmethod @property 是可调用的

发布于 2025-01-21 01:44:48 字数 728 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文