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

Bea*_*sen 5 python pylint

据我了解,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
Run Code Online (Sandbox Code Playgroud)

编辑

也许我应该澄清一下.我意识到这是一个抽象类,我很乐意使用抽象关键字,但据我所知,它在Python中并不重要(至少在我目前使用的版本中)所以我没有做任何有趣的抽象技巧(像那里发现的那些)并简单地把它留下来.

我很惊讶地发现pylint已经认识到这是一个抽象类本身.是什么让pylint确定这是一个抽象类?它只是在寻找NotImplementedError被扔到某个地方吗?

S.L*_*ott 12

FWIW,提出NotImplementedError就足以让pylint认为这是一个抽象类(绝对正确).来自logilab.org/card/pylintfeatures:W0223:方法%r在类%r中是抽象的但未被覆盖在具体类中未覆盖抽象方法(即引发NotImplementedError)时使用. - Tobiesque 2小时前


Ned*_*der 1

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