[code-quality] Two attribute usage checkers I'd like to see

Skip Montanaro skip at pobox.com
Thu Jun 6 22:49:01 CEST 2013


These features may already be possible with some tools.  If so,
pointers appreciated.

When a class evolves over a long period of time, it's quite possible
for methods or data attributes to fall into disuse.  While I realize
that attributes which don't start with an underscore are implicitly
part of the class's API, most attributes are used internally.  I'd
like to be able to have my checker(s) warn me if I define an attribute
but don't use it within the class:

    class Foo:
        def __init__(self):
            self.x = 0

        def y(self):
            pass
    ....

If I never refer to self.x or self.y within the class's methods it
would be nice to be alerted.  It might catch misspellings
(think_long_amd_meaningful_attribute_names) or catch what is
effectively dead code.  Obviously, this checker should be something
you can turn on and off selectively.  There are plenty of cases where
attributes aren't used within the class implementation but are used by
its clients.  This is not a hard-and-fast rule.

Here's the other problem I'd like to catch:

    class X(object):
        ...
        def get_foo(self):
            return self._foo
        foo = property(get_foo)

The problem here is that I have violated this Zen of Python dictum:

    There should be one-- and preferably only one --obvious way to do it.

I will admit that I sometimes go back and forth on property objects.
On the one hand, as a Python programmer, I like them.  I didn't always
feel that way though, and some of the people I have worked with over
the years have been more C++-centric, and tended to spell their
setters and getters without leading underscores.

Skip


More information about the code-quality mailing list