Pylint warnings on inherited nested class members

痞子三分冷 提交于 2019-12-11 03:38:37

问题


We have some particular functionality implemented as a Python class, in order to be easily extended by our developers inheriting it. Each class has a an inner Config class with a list of items. The base class has an empty Config class, and each inheriting class defines some items into it. Then pylint complains each time the item of Config subclass is used.

For example, this code:

class A(object):

  class Config(object):

    def __init__(self):
      self.item1 = 1
      self.item2 = 2

  def __init__(self):
    self._config = self.Config()


class B(A):

  class Config(A.Config):

    def __init__(self):
      super(B.Config, self).__init__()
      self.item3 = 3
      self.item4 = 4

  def do_something(self):
    if self._config.item3 == 3:
      print 'hello'
    if self._config.item1 == 5:
      print 'bye'

Then you can use it as:

>>> B().do_something()
hello

Our program works nicely with the same idea behind. Thing is pylint continues complaining each time we use the items. For example, in this case it would say:

E1101(no-member) Instance of 'Config' has no 'item3' member

So my question is ¿how can I avoid these pylint warnings, without disabling them? ¿is there a better way to achieve what I want? Note that in the real program, config values change according to user data, and it is not a bunch of constants.

Thanks a lot.


回答1:


This seems over complicated IMO. Using a dict for config will do nicely. e,g:

class A(object):

  def __init__(self):
    self._config = {
        'item1': 1,
        'item2': 2,
    }


class B(A):

  def __init__(self):
      super(B, self).__init__()
      self._config.update({
          'item3': 3,
          'item4': 4,
      })

  def do_something(self):
    if self._config['item3'] == 3:
      print 'hello'
    if self._config['item1'] == 5:
      print 'bye'


来源:https://stackoverflow.com/questions/25245414/pylint-warnings-on-inherited-nested-class-members

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