How do I get Pyflakes to ignore a statement?

前端 未结 7 769
南方客
南方客 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:26

    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

    0 讨论(0)
提交回复
热议问题