【问题标题】:How to avoid Pylint warnings for constructor of inherited class in Python 3?如何避免 Python 3 中继承类的构造函数的 Pylint 警告?
【发布时间】:2014-03-06 20:17:53
【问题描述】:

在 Python 3 中,我有以下代码:

class A:
    def __init__(self):
        pass

class B(A):
    def __init__(self):
        super().__init__()

这会产生 Pylint 警告:

  • 定义了旧式类。 (旧式)
  • 在旧样式类上使用 super(super-on-old-class)

据我了解,在 Python 3 中不再存在老式类,这段代码是可以的。

即使我在这段代码中明确使用新式类

class A(object):
    def __init__(self):
        pass

class B(A):
    def __init__(self):
        super().__init__()

由于在 Python 3 中调用父构造函数的语法不同,我收到 Pylint 警告:

  • super() 缺少参数(缺少超级参数)

那么,我如何告诉 Pylint 我要检查 Python 3 代码以避免这些消息(不禁用 Pylint 检查)?

【问题讨论】:

    标签: python inheritance python-3.x constructor pylint


    【解决方案1】:

    根据this list 'Missing argument to super()' 的代码为 E1004:。如果您只想禁用一种类型的警告,您可以在文件开头添加此行:

    # pylint: disable=E1004
    

    或者您可以尝试像这样拨打super()

    class B(A):
      def __init__(self):
        super(B, self).__init__()
    

    【讨论】:

    • 谢谢,您甚至可以禁用# pylint: disable=missing-super-argument 的消息。但是,我的 Python 代码有问题还是 Pylint 不兼容 Python 3?
    • 我不确定我在 python 2.7 上运行。 pylint 1.1 版也应该支持 python 3。
    【解决方案2】:

    这是由于 astroid 中的一个错误,在 https://bitbucket.org/logilab/astroid/commits/6869fb2acb9f58f0ba2197c6e9008989d85ca1ca 之前没有将所有类都视为带有 python 3 的新样式

    应该很快就会发布。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-05
    • 2013-02-16
    • 1970-01-01
    • 2014-10-04
    • 2013-11-01
    • 1970-01-01
    • 2013-04-25
    相关资源
    最近更新 更多