Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static code analyzer for Python catching module privacy hints

Say I have a module test.py:

def foo():
   print "foo"

def bar():
   print "bar"

def _baz():
   print "_baz"

__all__ = ['foo']

and a main.py:

from test import foo, bar, _baz

foo()
bar()   # breaks module privacy
_baz()  # breaks module privacy

Is there any (static) code analyzer tool for Python that will catch the imports breaking the privacy (bar, _baz) hinted by __all__?

I have tested Pylint, but it does not catch either.

Another clarification: I am not talking about situation where __all__ would be dynamically modified/filled and/or where importing code is dynamic. Just statically analyzable code situations.

like image 604
oberstet Avatar asked Sep 22 '26 10:09

oberstet


1 Answers

Because __all__'s intention has nothing to do with privacy (it is not a limit on what is exported, it is a tool to delimit wildcard importing) no tool in the Python ecosystem exists that takes that interpretation and tracks usage of names that are not listed in __all__.

In other words; __all__ was never intended as a means to bless only parts of the exported names as public, just as _name leading underscores are just private by convention and not enforced.

like image 162
Martijn Pieters Avatar answered Sep 24 '26 05:09

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!