python pep8 class in init imported but not used

ぃ、小莉子 提交于 2020-02-17 08:26:13

问题


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?


回答1:


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',]



回答2:


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.



来源:https://stackoverflow.com/questions/31079047/python-pep8-class-in-init-imported-but-not-used

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!