is there a way to prevent or alert in case a member is imported from within a module?
Let's say I have the following structure:
my_module/
__init__.py
utilities.py
and __init__.py contains
from .utilities import SomeUtilityFunction
Is there a way to prevent or alert developers when they do
from my_module.utilities import SomeUtilityFunction
instead of
from my_module import SomeUtilityFunction
The problem arose when a few modules started using a function that was imported inside a module in which it wasn't used, while also being available on the module's __init__.py, so after linting the file and removing the unused import my tests started failing.
@fixmycode mypy type checking can report this error in your code:
iox3.py:3: error: Module "iox2" does not explicitly export attribute "y" [attr-defined]
which I think is roughly the problem you are encountering: an attribute in an imported module that wasn't explicitly defined in that module, but instead came from somewhere else.
That's not correct. __all__ is not a whitelist. It is only the list used for
from module import *
If you have a module with submodules foo, bar and baz and __all__ = ["foo", "bar"] it will not prevent you from importing baz manually. It just won't do it automatically.