How to avoid Pylint warnings for constructor of inherited class in Python 3?

∥☆過路亽.° 提交于 2019-12-23 08:30:30

问题


In Python 3, I have the following code:

class A:
    def __init__(self):
        pass

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

This yields the Pylint warning:

  • Old-style class defined. (old-style-class)
  • Use of super on an old style class (super-on-old-class)

In my understanding, in Python 3 there does not exist an old-style class anymore and this code is OK.

Even if I explicitly use new-style classes with this code

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

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

I get Pylint warning because of the different syntax to call the parent constructor in Python 3:

  • Missing argument to super() (missing-super-argument)

So, how can I tell Pylint that I want to check Python 3 code to avoid these messages (without disabling the Pylint check)?


回答1:


This is due to a bug in astroid, which wasn't considering all classes as new style w/ python 3 before https://bitbucket.org/logilab/astroid/commits/6869fb2acb9f58f0ba2197c6e9008989d85ca1ca

That should be released shortly.




回答2:


According to this list 'Missing argument to super()' has code E1004: .If you want to disable only one type of warning you can add this line at the beginning of the file:

# pylint: disable=E1004

Or you can try calling super() like this:

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


来源:https://stackoverflow.com/questions/22235021/how-to-avoid-pylint-warnings-for-constructor-of-inherited-class-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!