简体   繁体   English

如何让 Pyflakes 忽略语句?

[英]How do I get Pyflakes to ignore a statement?

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: ...这是整个文件中唯一的 Pyflakes 警告:

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.) (通常我会去阅读文档,但链接已损坏。如果没有人有答案,我会阅读源代码。)

If you can use flake8 instead - which wraps pyflakes as well as the pep8 checker - a line ending with如果您可以改用flake8 - 它包装了 pyflakes 以及 pep8 检查器 - 以

# 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. (其中空格很重要 - 代码末尾和#之间有 2 个空格,它和NOQA文本之间有一个NOQA )将告诉检查器忽略该行上的任何错误。

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.

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.为了实现忽略功能,您需要编写自己的调用 pyflakes 检查器。

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.为了忽略整个块,您可能希望在块文档字符串中添加“pyflakes:ignore”并基于 node.doc 进行过滤。

Good luck!祝你好运!


I am using pocket-lint for all kind of static code analysis.我使用pocket-lint 进行各种静态代码分析。 Here are the changes made in pocket-lint for ignoring pyflakes: https://code.launchpad.net/~adiroiban/pocket-lint/907742/+merge/102882以下是在 pocket-lint 中为忽略 pyflakes 所做的更改: https ://code.launchpad.net/~adiroiban/pocket-lint/907742/+merge/102882

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 Substitude _unittest 和 _WritelnDecorator 与您需要的实体(模块、函数、类)

-- deemoowoor --deemoowoor

Here is a monkey patch for pyflakes that adds a # bypass_pyflakes comment option.这是 pyflakes 的一个猴子补丁,它添加了# bypass_pyflakes注释选项。

bypass_pyflakes.py绕过_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.py ,那么您可以将其调用为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

You can also import with __import__ .您也可以使用__import__导入。 It's not pythonic, but pyflakes does not warn you anymore.它不是 pythonic,但 pyflakes 不再警告你。 See documentation for __import__ .请参阅__import__文档

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

I created a little shell script with some awk magic to help me.我创建了一个带有awk魔法的小 shell 脚本来帮助我。 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):有了这个,所有带有import typingfrom typing import#$ (后者是我在这里使用的特殊注释)的行都被排除在外( $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.基本上它会记录行号并动态创建一个正则表达式。

For flake8 , which is recommended alternative (compare flake8 vs pyflakes here )对于flake8 ,这是推荐的替代品( 在此处比较 flake8 与 pyflakes )

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.这样,在同一行,你可以沉默entire file ,并做了many忽略声明at once ,这是经常的情况。

Flake gives you some options to ignore violations . Flake 为您提供了一些忽略违规的选项

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.或者配置为忽略它作为 flake8 配置中的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM