如何让 Pyflakes 忽略语句?

发布于 2024-10-17 22:40:54 字数 428 浏览 2 评论 0原文

我们的很多模块都以:

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 技术交流群。

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

发布评论

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

评论(9

谁与争疯 2024-10-24 22:40:54

如果您可以使用 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 the NOQA text) will tell the checker to ignore any errors on that line.

顾铮苏瑾 2024-10-24 22:40:54

我知道这个问题不久前就被质疑过并且已经得到了回答。

但我想补充一下我常用的:

try:
    import json
    assert json  # silence pyflakes
except ImportError:
    from django.utils import simplejson as json  # Python 2.4 fallback.

I know this was questioned some time ago and is already answered.

But I wanted to add what I usually use:

try:
    import json
    assert json  # silence pyflakes
except ImportError:
    from django.utils import simplejson as json  # Python 2.4 fallback.
别靠近我心 2024-10-24 22:40:54

是的,不幸的是,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

醉酒的小男人 2024-10-24 22:40:54

引用 github 问题单

虽然修复仍在进行中,但如果您想知道的话,可以通过以下方式解决:

<前><代码>尝试:
从unittest.runner导入_WritelnDecorator
_WriteInDecorator; # pyflakes 问题 #13 的解决方法
除了导入错误:
从单元测试导入_WritelnDecorator

用您需要的实体(模块、函数、类)替换 _unittest 和 _WritelnDecorator

-- deemoowoor

To quote from the github issue ticket:

While the fix is still coming, this is how it can be worked around, if you're wondering:

try:
    from unittest.runner import _WritelnDecorator
    _WritelnDecorator; # workaround for pyflakes issue #13
except ImportError:
    from unittest import _WritelnDecorator

Substitude _unittest and _WritelnDecorator with the entities (modules, functions, classes) you need

-- deemoowoor

无名指的心愿 2024-10-24 22:40:54

这是 pyflakes 的猴子补丁,添加了 #bypass_pyflakes 注释选项。

bypass_pyflakes.py

#!/usr/bin/env python

from pyflakes.scripts import pyflakes
from pyflakes.checker import Checker


def report_with_bypass(self, messageClass, *args, **kwargs):
    text_lineno = args[0] - 1
    with open(self.filename, 'r') as code:
        if code.readlines()[text_lineno].find('bypass_pyflakes') >= 0:
            return
    self.messages.append(messageClass(self.filename, *args, **kwargs))

# monkey patch checker to support bypass
Checker.report = report_with_bypass

pyflakes.main()

如果将其保存为 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

#!/usr/bin/env python

from pyflakes.scripts import pyflakes
from pyflakes.checker import Checker


def report_with_bypass(self, messageClass, *args, **kwargs):
    text_lineno = args[0] - 1
    with open(self.filename, 'r') as code:
        if code.readlines()[text_lineno].find('bypass_pyflakes') >= 0:
            return
    self.messages.append(messageClass(self.filename, *args, **kwargs))

# monkey patch checker to support bypass
Checker.report = report_with_bypass

pyflakes.main()

If you save this as bypass_pyflakes.py, then you can invoke it as python bypass_pyflakes.py myfile.py.

http://chase-seibert.github.com/blog/2013/01/11/bypass_pyflakes.html

梦途 2024-10-24 22:40:54

Flake 为您提供一些忽略违规的选项

我最喜欢的一个是使其明确并忽略特定的内联违规:

my invalid code # noqa: WS03

并且您还有其他已经引用的选项。

  1. 忽略行中的所有验证:
my invalid code # NOQA
  1. 忽略文件中的所有验证。放入其第一行:
# flake8: noqa: E121, E131, E241, F403, F405

或配置为忽略它作为 flake8 配置中的参数。

Flake gives you some options to ignore violations.

My favorite one is to make it explicit and ignore the specific violation inline:

my invalid code # noqa: WS03

And you have the others already cited options.

  1. Ignore all validations in the line:
my invalid code # NOQA
  1. Ignore all validations in the file. Put in its first line:
# flake8: noqa: E121, E131, E241, F403, F405

Or configure to ignore it as a parameter in you flake8 configuration.

蓝天 2024-10-24 22:40:54

您还可以使用 __import__ 导入。它不是 pythonic,但 pyflakes 不再警告你。请参阅 __import__ 的文档。

try:
    import json
except ImportError:
    __import__('django.utils', globals(), locals(), ['json'], -1)

You can also import with __import__. It's not pythonic, but pyflakes does not warn you anymore. See documentation for __import__ .

try:
    import json
except ImportError:
    __import__('django.utils', globals(), locals(), ['json'], -1)
自演自醉 2024-10-24 22:40:54

我创建了一个带有 awk 魔法的小 shell 脚本来帮助我。这样,所有带有 importtypingfromtypingimport#$(后者是我在这里使用的特殊注释)的行都被排除在外(< code>$1 是Python脚本的文件名):

result=$(pyflakes -- "$1" 2>&1)

# check whether there is any output
if [ "$result" ]; then

    # lines to exclude
    excl=$(awk 'BEGIN { ORS="" } /(#\$)|(import +typing)|(from +typing +import )/ { print sep NR; sep="|" }' "$1")

    # exclude lines if there are any (otherwise we get invalid regex)
    [ "$excl" ] &&
        result=$(awk "! /^[^:]+:(${excl}):/" <<< "$result")

fi

# now echo "$result" or such ...

基本上它会记录行号并动态地创建一个正则表达式。

I created a little shell script with some awk magic to help me. With this all lines with import 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):

result=$(pyflakes -- "$1" 2>&1)

# check whether there is any output
if [ "$result" ]; then

    # lines to exclude
    excl=$(awk 'BEGIN { ORS="" } /(#\$)|(import +typing)|(from +typing +import )/ { print sep NR; sep="|" }' "$1")

    # exclude lines if there are any (otherwise we get invalid regex)
    [ "$excl" ] &&
        result=$(awk "! /^[^:]+:(${excl}):/" <<< "$result")

fi

# now echo "$result" or such ...

Basically it notes the line numbers and dynamically creates a regex out it.

悍妇囚夫 2024-10-24 22:40:54

对于flake8,这是推荐的替代方案(比较 flake8 与 pyflakes 此处

添加第一行,例如:

# flake8: noqa: E121, E131, E241, F403, F405

一般来说:

# flake8: noqa: <code>[, <code> ...]

这样,您可以在一行中静默整个文件并为许多执行此操作code>立即忽略语句,这是常见的情况。

For flake8, which is recommended alternative (compare flake8 vs pyflakes here)

Add the first line like:

# flake8: noqa: E121, E131, E241, F403, F405

in general:

# flake8: noqa: <code>[, <code> ...]

This way in one line you can silent the entire file and do it for many ignore statements at once, which is often a case.

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