Pylint warnings on inherited nested class members

2019-07-19 01:29发布

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条回答
相关推荐>>
2楼-- · 2019-07-19 02:00

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'
查看更多
登录 后发表回答