How do I get Pyflakes to ignore a statement?

前端 未结 7 768
南方客
南方客 2020-11-29 20:33

A lot of our modules start with:

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


        
相关标签:
7条回答
  • 2020-11-29 21:05

    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.
    
    0 讨论(0)
  • 2020-11-29 21:10

    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

    0 讨论(0)
  • 2020-11-29 21:11

    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

    0 讨论(0)
  • 2020-11-29 21:11

    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.

    0 讨论(0)
  • 2020-11-29 21:14

    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.

    0 讨论(0)
  • 2020-11-29 21:22

    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)
    
    0 讨论(0)
提交回复
热议问题