Python pep8 class in init imported but not used

I am doing PEP8 checks in python using the python flake8 library. I have an import statement in the __init__.py file in one of my submodules that looks like this:

 from .my_class import MyClass 

The reason I have this line in the initialization file is because I can import MyClass from a submodule as from somemodule import MyClass instead of writing from somemodule.my_class import MyClass .

I would like to know if it is possible to maintain this functionality while fixing a PEP8 violation?

+7
python pep8 flake8
source share
2 answers

This is not a violation of PEP8. I just do this:

 from .my_class import MyClass # noqa 

Edit: Another possibility is to use __all__ . In this case, flake8 understands what happens:

 from .my_class import MyClass __all__ = ['MyClass',] 
+20
source share

According to PEP 8 , you should include MyClass in __all__ , which will also fix the import problem, but is not used:

To better support introspection, modules should explicitly declare names in their public API using the __all__ attribute.

0
source share

All Articles