Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top