如何让 Pyflakes 忽略语句?
我们的很多模块都以:
try:
import json
except ImportError:
from django.utils import simplejson as json # Python 2.4 fallback.
...这是整个文件中唯一的 Pyflakes 警告:
foo/bar.py:14: redefinition of unused 'json' from line 12
如何让 Pyflakes 忽略这个?
(通常我会去阅读文档,但链接已损坏。如果没有人有回答,我只会阅读源代码。)
A lot of our modules start with:
try:
import json
except ImportError:
from django.utils import simplejson as json # Python 2.4 fallback.
...and it's the only Pyflakes warning in the entire file:
foo/bar.py:14: redefinition of unused 'json' from line 12
How can I get Pyflakes to ignore this?
(Normally I'd go read the docs but the link is broken. If nobody has an answer, I'll just read the source.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您可以使用 flake8 代替 - 它包装 pyflakes 以及 pep8 检查器 - 一行以结尾
# NOQA
(其中空格很重要 - 代码末尾和
#
之间有 2 个空格,与NOQA
文本之间有 1 个空格) 将告诉检查器忽略该行上的任何错误。If you can use flake8 instead - which wraps pyflakes as well as the pep8 checker - a line ending with
# NOQA
(in which the space is significant - 2 spaces between the end of the code and the
#
, one between it and theNOQA
text) will tell the checker to ignore any errors on that line.我知道这个问题不久前就被质疑过并且已经得到了回答。
但我想补充一下我常用的:
I know this was questioned some time ago and is already answered.
But I wanted to add what I usually use:
是的,不幸的是,dimod.org 和所有好东西一起关闭了。
查看 pyflakes 代码,在我看来,pyflakes 的设计目的是为了可以轻松地将其用作“嵌入式快速检查器”。
为了实现忽略功能,您需要编写自己的调用 pyflakes 检查器的函数。
在这里你可以找到一个想法: http://djangosnippets.org/snippets/1762/
请注意上面的代码片段仅适用于同一行上的注释。
为了忽略整个块,您可能需要在块文档字符串中添加“pyflakes:ignore”并根据node.doc进行过滤。
祝你好运!
我正在使用 pocket-lint 进行各种静态代码分析。以下是 pocket-lint 中为忽略 pyflakes 所做的更改:https: //code.launchpad.net/~adiroiban/pocket-lint/907742/+merge/102882
Yep, unfortunately dimod.org is down together with all goodies.
Looking at the pyflakes code, it seems to me that pyflakes is designed so that it will be easy to use it as an "embedded fast checker".
For implementing ignore functionality you will need to write your own that calls the pyflakes checker.
Here you can find an idea: http://djangosnippets.org/snippets/1762/
Note that the above snippet only for for comments places on the same line.
For ignoring a whole block you might want to add 'pyflakes:ignore' in the block docstring and filter based on node.doc.
Good luck!
I am using pocket-lint for all kind of static code analysis. Here are the changes made in pocket-lint for ignoring pyflakes: https://code.launchpad.net/~adiroiban/pocket-lint/907742/+merge/102882
引用 github 问题单:
To quote from the github issue ticket:
这是 pyflakes 的猴子补丁,添加了 #bypass_pyflakes 注释选项。
bypass_pyflakes.py
如果将其保存为
bypass_pyflakes.py
,那么您可以将其调用为pythonbypass_pyflakes.py myfile.py
。http://chase-seibert.github.com/blog/ 2013/01/11/bypass_pyflakes.html
Here is a monkey patch for pyflakes that adds a
# bypass_pyflakes
comment option.bypass_pyflakes.py
If you save this as
bypass_pyflakes.py
, then you can invoke it aspython bypass_pyflakes.py myfile.py
.http://chase-seibert.github.com/blog/2013/01/11/bypass_pyflakes.html
Flake 为您提供一些忽略违规的选项。
我最喜欢的一个是使其明确并忽略特定的内联违规:
并且您还有其他已经引用的选项。
或配置为忽略它作为 flake8 配置中的参数。
Flake gives you some options to ignore violations.
My favorite one is to make it explicit and ignore the specific violation inline:
And you have the others already cited options.
Or configure to ignore it as a parameter in you flake8 configuration.
您还可以使用
__import__
导入。它不是 pythonic,但 pyflakes 不再警告你。请参阅__import__
的文档。You can also import with
__import__
. It's not pythonic, but pyflakes does not warn you anymore. See documentation for__import__
.我创建了一个带有 awk 魔法的小 shell 脚本来帮助我。这样,所有带有
importtyping
、fromtypingimport
或#$
(后者是我在这里使用的特殊注释)的行都被排除在外(< code>$1 是Python脚本的文件名):基本上它会记录行号并动态地创建一个正则表达式。
I created a little shell script with some
awk
magic to help me. With this all lines withimport typing
,from typing import
or#$
(latter is a special comment I am using here) are excluded ($1
is the file name of the Python script):Basically it notes the line numbers and dynamically creates a regex out it.
对于
flake8
,这是推荐的替代方案(比较 flake8 与 pyflakes 此处)添加第一行,例如:
一般来说:
这样,您可以在一行中静默
整个文件
并为许多
执行此操作code>立即忽略语句
,这是常见的情况。For
flake8
, which is recommended alternative (compare flake8 vs pyflakes here)Add the first line like:
in general:
This way in one line you can silent the
entire file
and do it formany
ignore statementsat once
, which is often a case.