如何忽略 Pyflakes 错误“已导入但未使用”在“__init__.py”中文件?
我将测试分散到多个 Python 文件中:
tests
├── __init__.py
├── test_apples.py
└── test_bananas.py.py
我将测试导入到“__init__.py”文件中:
from test_apples import ApplesTest
from test_bananas import BananasTest
但是,在命令行上运行 Pyflakes:
pyflakes .
会输出以下错误:
tests/__init__.py:1: [E] PYFLAKES:'ApplesTest' imported but unused
tests/__init__.py:2: [E] PYFLAKES:'BananasTest' imported but unused
I split my tests across multiple Python files:
tests
├── __init__.py
├── test_apples.py
└── test_bananas.py.py
I import the tests in the ‘__init__.py’ file:
from test_apples import ApplesTest
from test_bananas import BananasTest
However running Pyflakes on the command-line:
pyflakes .
outputs the following errors:
tests/__init__.py:1: [E] PYFLAKES:'ApplesTest' imported but unused
tests/__init__.py:2: [E] PYFLAKES:'BananasTest' imported but unused
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要忽略“__init__.py”文件中的所有错误 F401(“已导入但未使用”),选项 'per-file-ignores' 自 3.7.0 版本以来可用Flake8(更好的 Pyflakes)非常方便。它可以在命令行上使用:
或在配置文件(“.flake8”、“setup.cfg”或“tox.ini”)中使用:
To ignore all errors F401 (‘imported but unused’) in ‘__init__.py’ files, the option ‘per-file-ignores’ which has been available since version 3.7.0 of Flake8 (a better Pyflakes) is very convenient. It can be used on the command-line:
or in a configuration file (‘.flake8’, ‘setup.cfg’ or ‘tox.ini’):
有时你必须跳过一行。
根据当前版本文档(flake8 2.4.1)
包含的文件
将被跳过。这有效,但 #noga, #pyflakes.ignore 不行。
Sometimes you have to skip a line.
According to the current versions docs (flake8 2.4.1)
The files that contain
are skipped. This works, and # noga, # pyflakes.ignore not.
在我的 PyFlakes (0.7.3) 版本中,使用
__all__< /代码> 有效。
此外,要跳过一行,您应该添加
# noqa
。In my version of PyFlakes (0.7.3), using
__all__
works.Additionally, to skip a line, you should add
# noqa
.将
# noqa: F401
添加到行末尾(F401
是此错误的代码)示例:
from django.utils import http # noqa: F401
add
# noqa: F401
to the end of your line (F401
is a code of this error)Example:
from django.utils import http # noqa: F401
导入被解释为未使用,因为您的 linting 工具不了解它的使用方式。
最全面的修复是确保这些名称从您的模块中导出。通过设置 __all__ ,这是一种详尽枚举导出名称的机制,或者通过使用
as
为导入提供显式导出名称。但是,我通常建议使用测试运行程序来扫描测试用例,而不是将它们全部导入,因为这让您更有信心,您肯定正在运行所有测试。
The imports are interpreted as being unused because your linting tool doesn't understand how it's being used.
The most comprehensive fix is to ensure that these names are clearly exported from your module. Either by setting
__all__
, which is a mechanism for exhaustively enumerating the exported names, or by giving the imports explicit exported names usingas
.However, I would generally recommend using a test runner that scans for test cases rather than importing them all, as that gives you more confidence that you're definitely running all of your tests.
当您使用以下命令从 y 模块导入 x 时:
请记住使用以下命令打印 x:
When you import x from y module using:
remember to print the x using:
在您想要忽略的每一行添加
# pyflakes.ignore
注释(在您的情况下为 import 语句)。Add
# pyflakes.ignore
comment on every line you want to ignore (in your case import statements).