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. | |
priority | normal |
---|---|
type | enhancement |
done in | <not specified> |
load | 1.000 |
load left | 1.000 |
closed by | <not specified> |
- logilab-astng #9515 strange message for non-class "Class baz has no egg member"
- pylint #9188 Avoidable W0631
- pylint #2463 W0631 false positive when we are sure to do at least one iteration
- pylint #4024 W0631 false positive if a "else" clause is defining the loop variable
- pylint #37750 provide a way of getting evaluation without all reports
Comments
-
2009/04/10 17:22, written by anon
-
2009/09/12 11:57, written by jmsalli
-
2010/06/03 04:32, written by philadams
add commentWhenever setattr or __setattr__ is present in __init__, a warning could be thrown instead of an error if an unknown attribute is referred to afterwards.
In my customized version of PyLint 0.18.1, I use a plug-in system to handle various classes that have dynamic attributes. In essence, just before issuing W0201, E0203, E1101, or E1103, .hasattr() method of each attribute definer plug-in is called to determine if a class instance has the attribute after all.
As invasive measures as necessary are used to determine names of the attributes. For instance, in one attribute definer I have code reading widget names from wxPython XRC-files to determine attribute names for objects which have their attributes dynamically loaded based on those widget names in said XRC-files. I imagine similar plug-in could be relatively easily made for e.g. Django ORM.
If you are interested in a patch, just let me know.
A fix here would be great! Note that pylint currently chokes in this way for attributes of stdlib classes like collections.namedtuple too. Many thanks for a great tool!