是什么让 pylint 认为我的类是抽象的?

发布于 2024-08-16 13:44:33 字数 1340 浏览 1 评论 0原文

据我了解,Python(2.5.2)并不真正支持抽象类。为什么 pylint 抱怨这个类是“抽象类而不是引用?”它会对任何抛出 NotImplementedError 的类执行此操作吗?

我将每个类都放在自己的文件中,所以如果是这种情况,我想我别无选择,只能抑制此消息,但我希望也许有另一种方法可以解决它。

"""Package Repository interface."""


class PackageRepository(object):
    """Package Repository interface."""

    def __init__(self):
        self.hello = "world"

    def get_package(self, package_id):
        """
        Get a package by ID.
        """
        raise NotImplementedError( \
                "get_package() method has not been implemented")

    def get_packages(self):
        """
        Get all packages.
        """
        raise NotImplementedError( \
                "get_packages() method has not been implemented")

    def commit(self):
        """
        Commit all changes.
        """
        raise NotImplementedError( \
                "commit() method has not been implemented")

    def do_something(self):
        """
        Doing something.
        """
        return self.hello

编辑

也许我应该澄清一下。我意识到这是一个抽象类,我很想使用abstract关键字,但据我了解,这在Python中都不重要(至少在我当前使用的版本中),所以我没有费心做任何有趣的抽象技巧(就像在这里找到的)一样,只是将其省略。

我很惊讶地发现 pylint 意识到这本身就是一个抽象类。是什么让 pylint 确定这是一个抽象类?它只是寻找在某个地方抛出的 NotImplementedError 吗?

As I understand it, Python (2.5.2) does not have real support for abstract classes. Why is pylint complaining about this class being an "Abstract class not reference?" Will it do this for any class that has NotImplementedError thrown?

I have each class in its own file so if this is the case I guess I have no choice but to suppress this message but I am hoping there is maybe another way around it.

"""Package Repository interface."""


class PackageRepository(object):
    """Package Repository interface."""

    def __init__(self):
        self.hello = "world"

    def get_package(self, package_id):
        """
        Get a package by ID.
        """
        raise NotImplementedError( \
                "get_package() method has not been implemented")

    def get_packages(self):
        """
        Get all packages.
        """
        raise NotImplementedError( \
                "get_packages() method has not been implemented")

    def commit(self):
        """
        Commit all changes.
        """
        raise NotImplementedError( \
                "commit() method has not been implemented")

    def do_something(self):
        """
        Doing something.
        """
        return self.hello

EDIT

Perhaps I should clarify. I realize this is an abstract class and I would love to use the abstract keyword but as I understand it none of that matters in Python (at least in the version I am currently using) so I didn't bother doing any funny abstract tricks (like those found here) and simply left it out.

I was surprised to see that pylint picks up on the fact that this is an abstract class on its own. What makes pylint determine this is an abstract class? Is it simply looking for NotImplementedError being thrown somewhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年少掌心 2024-08-23 13:44:33

FWIW,引发 NotImplementedError 足以使 pylint 认为这是一个抽象类(这是绝对正确的)。来自 logilab.org/card/pylintfeatures:W0223:方法 %r 在类 %r 中是抽象的,但未被重写 当抽象方法(即 raise NotImplementedError)在具体类中未被重写时使用。 – 托比斯克 2 小时前

FWIW, raising NotImplementedError is enough to make pylint think this is an abstract class (which is absolutely correct). from logilab.org/card/pylintfeatures: W0223: Method %r is abstract in class %r but is not overridden Used when an abstract method (ie raise NotImplementedError) is not overridden in concrete class. – Tobiesque 2 hours ago

故笙诉离歌 2024-08-23 13:44:33

根据我的经验,pylint 有点过于热心,并且在您关闭一些警告之前没有用处。

In my experience, pylint is a bit over-zealous, and isn't useful until you've turned off a number of the warnings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文