如何忽略 Pyflakes 错误“已导入但未使用”在“__init__.py”中文件?

发布于 2024-12-20 09:48:44 字数 634 浏览 2 评论 0原文

我将测试分散到多个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

伤痕我心 2024-12-27 09:48:44

要忽略“__init__.py”文件中的所有错误 F401(“已导入但未使用”),选项 'per-file-ignores' 自 3.7.0 版本以来可用Flake8(更好的 Pyflakes)非常方便。它可以在命令行上使用:

flake8 --per-file-ignores="__init__.py:F401" .

或在配置文件(“.flake8”、“setup.cfg”或“tox.ini”)中使用:

[flake8]
per-file-ignores = __init__.py:F401

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:

flake8 --per-file-ignores="__init__.py:F401" .

or in a configuration file (‘.flake8’, ‘setup.cfg’ or ‘tox.ini’):

[flake8]
per-file-ignores = __init__.py:F401
赠我空喜 2024-12-27 09:48:44

有时你必须跳过一行。
根据当前版本文档(flake8 2.4.1)
包含的文件

# flake8: noqa

将被跳过。这有效,但 #noga, #pyflakes.ignore 不行。

Sometimes you have to skip a line.
According to the current versions docs (flake8 2.4.1)
The files that contain

# flake8: noqa

are skipped. This works, and # noga, # pyflakes.ignore not.

半边脸i 2024-12-27 09:48:44

在我的 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.

彩扇题诗 2024-12-27 09:48:44

# 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

在梵高的星空下 2024-12-27 09:48:44

导入被解释为未使用,因为您的 linting 工具不了解它的使用方式。

最全面的修复是确保这些名称从您的模块中导出。通过设置 __all__ ,这是一种详尽枚举导出名称的机制,或者通过使用 as 为导入提供显式导出名称。

from test_apples import ApplesTest as ApplesTest

但是,我通常建议使用测试运行程序来扫描测试用例,而不是将它们全部导入,因为这让您更有信心,您肯定正在运行所有测试。

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 using as.

from test_apples import ApplesTest as ApplesTest

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.

萌面超妹 2024-12-27 09:48:44

当您使用以下命令从 y 模块导入 x 时:

from y import x

请记住使用以下命令打印 x:

print(x)

When you import x from y module using:

from y import x

remember to print the x using:

print(x)
只是在用心讲痛 2024-12-27 09:48:44

在您想要忽略的每一行添加 # pyflakes.ignore 注释(在您的情况下为 import 语句)。

Add # pyflakes.ignore comment on every line you want to ignore (in your case import statements).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文