如何让py薄片忽略一个语句?

时间:2020-12-23 17:26:45

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?

我怎么能让Pyflakes忽略这个呢?

(Normally I'd go read the docs but the link is broken. If nobody has an answer, I'll just read the source.)

(通常我会去看医生,但是链接坏了。如果没有人知道答案,我就看资料。

7 个解决方案

#1


183  

If you can use flake8 instead - which wraps pyflakes as well as the pep8 checker - a line ending with

如果你可以用flake8来代替它——它包裹着皮片和pep8检查器——以一行结束

# NOQA

# 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.

(其中空格是重要的—代码末尾和#之间的两个空格,它和NOQA文本之间的一个空格)将告诉检查器忽略该行上的任何错误。

#2


37  

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.

#3


7  

Yep, unfortunately dimod.org is down together with all goodies.

是的,不幸的是,dimod.org和所有的好东西在一起。

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".

查看pyflakes代码,在我看来,pyflakes被设计成可以很容易地把它作为“嵌入式快速检查器”使用。

For implementing ignore functionality you will need to write your own that calls the pyflakes checker.

为了实现忽略功能,您需要编写自己的称为py薄片检查器的代码。

Here you can find an idea: http://djangosnippets.org/snippets/1762/

在这里你可以找到一个想法: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.

注意,上面的代码片段仅用于注释位于同一行上。为了忽略整个块,您可能需要在块docstring中添加“pyflakes:ignore”,并基于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

我正在使用口袋线进行各种静态代码分析。以下是对忽略pyflakes /code.launchpad.net/~adiroiban/pocket-lint/907742/+merge/102882在pocket-lint中所做的更改

#4


5  

To quote from the github issue ticket:

引用github发行的票据:

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

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

-- deemoowoor

——deemoowoor

#5


5  

Here is a monkey patch for pyflakes that adds a # bypass_pyflakes comment option.

这里是pyflakes的monkey补丁,它添加了# 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()

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

如果将其保存为bypass_pyflakes。然后可以将其作为python bypass_pyflakes调用。py myfile.py。

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

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

#6


2  

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

您还可以导入__import__。它不是毕达哥拉斯式的,但是pyflakes不会再警告你。请参阅文件。

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

#7


0  

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):

我用awk魔法创建了一个shell脚本来帮助我。有了这些输入类型,从输入导入或#$(后面是我在这里使用的一个特殊注释)都被排除($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 ...

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

基本上,它记录行号并动态地创建一个regex。

#1


183  

If you can use flake8 instead - which wraps pyflakes as well as the pep8 checker - a line ending with

如果你可以用flake8来代替它——它包裹着皮片和pep8检查器——以一行结束

# NOQA

# 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.

(其中空格是重要的—代码末尾和#之间的两个空格,它和NOQA文本之间的一个空格)将告诉检查器忽略该行上的任何错误。

#2


37  

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.

#3


7  

Yep, unfortunately dimod.org is down together with all goodies.

是的,不幸的是,dimod.org和所有的好东西在一起。

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".

查看pyflakes代码,在我看来,pyflakes被设计成可以很容易地把它作为“嵌入式快速检查器”使用。

For implementing ignore functionality you will need to write your own that calls the pyflakes checker.

为了实现忽略功能,您需要编写自己的称为py薄片检查器的代码。

Here you can find an idea: http://djangosnippets.org/snippets/1762/

在这里你可以找到一个想法: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.

注意,上面的代码片段仅用于注释位于同一行上。为了忽略整个块,您可能需要在块docstring中添加“pyflakes:ignore”,并基于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

我正在使用口袋线进行各种静态代码分析。以下是对忽略pyflakes /code.launchpad.net/~adiroiban/pocket-lint/907742/+merge/102882在pocket-lint中所做的更改

#4


5  

To quote from the github issue ticket:

引用github发行的票据:

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

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

-- deemoowoor

——deemoowoor

#5


5  

Here is a monkey patch for pyflakes that adds a # bypass_pyflakes comment option.

这里是pyflakes的monkey补丁,它添加了# 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()

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

如果将其保存为bypass_pyflakes。然后可以将其作为python bypass_pyflakes调用。py myfile.py。

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

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

#6


2  

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

您还可以导入__import__。它不是毕达哥拉斯式的,但是pyflakes不会再警告你。请参阅文件。

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

#7


0  

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):

我用awk魔法创建了一个shell脚本来帮助我。有了这些输入类型,从输入导入或#$(后面是我在这里使用的一个特殊注释)都被排除($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 ...

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

基本上,它记录行号并动态地创建一个regex。