pylint #8961 tell pylint about dynamic attributes [open]

here is a discussion about how we could tell pylint about dynamic attributes:

Duncan:

Presumably something like the following could work.

class MyClass(object):

   # pylint: dynamic-attributes = foo, bar, baz
   def __init__(self, **kwargs):
       for key, val in kwargs.iteritems():
            setattr(self, key, value)

Maarteen update:

I would prefer to have a way to tell pylint that a class has dynamic attributes without explicitly naming the attributes. In the best case explicitly naming the attributes is duplicating information, in the worst case it is not even possible to name the attributes since they might be different for different executions of the program (read from a user-provided data file, for example).

A possible syntax for this would be similar to what you proposed, but with a slightly different comment:

# pylint: dynamic-attributes

or with a wildcard filter:

# pylint: dynamic-attributes = *

or with a regex filter:

# pylint: dynamic-attributes = .*

or similar to existing warning-suppression syntax:

# pylint: disable-msg=E1101

In the last case, this suppression should not suppress warnings from code inside the class, but from code outside the class accessing attributes on the class.

What would be even handier would be to specify the name of a python list or dict where the list members or keys are the attribute names. Of course this would need to be a class attribute, not an instance attribute. For example:

class MyClass(object):
  attrs = ['foo', 'bar', 'baz']
  # pylint: dynamic-attributes = attrs
  def __init__(self):
      for attr in self.__class__.attrs:
          setattr(self, attr, 'some value')

For the case that attributes are defined outside the class and passed in to __init__ I can not think of a way to handle that one.

Paul update:

What would be even handier would be to specify the name of a python list or dict where the list members or keys are the attribute names. Of course this would need to be a class attribute, not an instance attribute. For example:

class MyClass(object):
  attrs = ['foo', 'bar', 'baz']
  # pylint: dynamic-attributes = attrs
  def __init__(self):
      for attr in self.__class__.attrs:
          setattr(self, attr, 'some value')

For the case that attributes are defined outside the class and passed in to __init__ I can not think of a way to handle that one.

prioritynormal
typeenhancement
done in<not specified>
load1.000
load left1.000
closed by<not specified>