python pep8 class in init imported but not used

PythonPep8Flake8

Python Problem Overview


I'm doing PEP8 checks in python using the python flake8 library. I have an import statement in an __init__.py file in one of my sub-modules which looks like this:

from .my_class import MyClass

The reason I have this line in the init file is so that I can import MyClass from the sub-module as from somemodule import MyClass instead of having to write from somemodule.my_class import MyClass.

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

Python Solutions


Solution 1 - Python

This is not actually a PEP8 violation. I simply do this:

from .my_class import MyClass  # noqa

Edit: Another possibility is to use __all__. In that case, flake8 understands what is going on:

from .my_class import MyClass

__all__ = ['MyClass',]

Solution 2 - Python

According to PEP 8, you should include MyClass in __all__, which will also fix the imported-but-not-used issue:

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

Solution 3 - Python

According to flake8's documention, you can in-line ignore this specific warning with:

from .my_class import MyClass  # noqa: F401

For reference, here are flake8's error codes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSalviusView Question on Stackoverflow
Solution 1 - PythonRené FleschenbergView Answer on Stackoverflow
Solution 2 - PythonMihai CapotăView Answer on Stackoverflow
Solution 3 - PythonMike TView Answer on Stackoverflow