If I write a standalong script/program, it looks just like a module to
the outside world (as long as I name it correctly), though it might
have a #! line at the top. Otherwise, to a static analyzer, it still
looks like a module, which means anything it defines might be visible
from the outside. Consequently, if I happen not to use module globals
or instance attributes, tools like pylint won't complain.
I think it would be nice if I could tell such tools either:
1. The thing I'm feeding you is a standalone program. Nobody else
might reference this stuff, so complain about unused names.
2. This particular thing I'm defining is either public or private.
So, if I have this block of "constants":
ONE_SECOND = 1000 # ms
ONE_MINUTE = 60 * ONE_SECOND
ONE_HOUR = 60 * ONE_MINUTE
ONE_SECOND_DELTA = datetime.timedelta(seconds=1)
ONE_MINUTE_DELTA = datetime.timedelta(minutes=1)
ONE_DAY_DELTA = datetime.timedelta(days=1)
ONE_YEAR_DELTA = 365 * ONE_DAY_DELTA
EPSILON = 1e-10
it would be nice if I was told if any were unused.
Similarly, if an attribute of a class is defined but not used
elsewhere within the class (or perhaps within the defining module),
I'd like to know if it's unused. At a minimum, if I've defined an
instance method with a leading underscore in its name, it would be
real nice if I was told about lack of use within the class. For
classes where most of the API is public, it might be nice to mark the
class as public then explicitly mark any private attributes:
# <marker> public
self.x = self.y = 0.0
# <marker> private
self.r = self.theta = 0.0
# <marker> private
self.r = math.sqrt(self.x**2 + self.y**2)
self.theta = math.atan2(self.y, self.x)
Do any existing static analysis tools support anything like this? I'm
certainly not married to the notion of annotating attributes. Also, I
should note that I am still using Python 2.7. If there are any
features of Python 3 that facilitate this, I'd love to hear about it.