2014-12-31 19 views
12

考虑这个一段代码pylint的:避免检查INSIDE文档字符串(全局指令/ rcfile)

def test(): 
    """This line is longer than 80 chars, but, for me this is ok inside a DOCSTRING, 
    this one is shorter. 
    """ 

    if 'This is toooooooooooooooooooooooooooooooooooo longggggggggggggggggggggggg': 
     print 'True' 

pylint输出:

C: 5, 0: Line too long (84/80) (line-too-long) 
C: 9, 0: Line too long (83/80) (line-too-long) 

是否有任何指令 avaliable(rcfile)排除ONLYDOCSTRINGSpylint检查器?

一个regexignore-long-lines感谢@fredtantini)似乎被忽略。

周边文档字符串# pylint: dis/enable=line-too-long感谢@Evert)将导致所要求的效果,不过,我正在寻找一个全局指令使

+1

你是否'忽视,长期lines'需要多正则表达式? – fredtantini

+1

我想你可以用'#pylint:dis/enable = line-too-long'评论来包围文档字符串,但这并不好玩,也不漂亮。 – Evert

+1

可能没有更多选项,请参阅'def check_lines(self,lines,i):'format in'format.py'中的源代码。写一个插件来覆盖它可能是可能的。 – simonzack

回答

4

问题是,你问的东西是根本不可配置。此外,根据PEP8

将所有行限制为最多79个字符。

对于流动具有较少结构上的限制 (文档字符串或注释)文本的长块中,行的长度应限制为72个字符

这就是说,让我们来分析一下the source code,看看我们能否想出一个解决方案。负责检查行长度的检查器称为FormatChecker - 它还检查缩进和未授权的代码结构。

的是两个相关的方法,我们感兴趣的是:

正如你可以看到 “行太长” 错误消息在此处添加:

if len(line) > max_chars and not ignore_long_line.search(line): 
    self.add_message('line-too-long', line=i, args=(len(line), max_chars)) 

ignore_long_line这里指的是ignore-long-lines的正则表达式设置,默认情况下,该设置等于^\s*(#)?<?https?://\S+>?$ - 这有助于(doc)字符串内有http链接的情况下 - 不会抛出错误。

换句话说,只有ignore-long-lines设置可以防止pylint应用“太长行”检查。

另外要注意的有趣事实:pylint代码质量检查工具有此误导文档字符串:

def check_lines(self, lines, i): 
    """check lines have less than a maximum number of characters 
    """ 

,同时该方法还检查missing-final-newlinetrailing-whitespace。这是你永远不会动静抓住的东西 - 只有人类才能注意和修复的人为错误。


我不喜欢下面的建议,但在飞行我们可以猴子修补FormatChecker

创建一个名为“checker.py”的脚本并将其放在PYTHONPATH上。这里我们重新定义new_line()方法并添加一个“标记类型”检查 - 如果这是一个注释或一个字符串(docstring),不要让检查器调用check_lines()方法。然后,在register()功能我们覆盖内置FormatCheckernew_line()与我们:

import tokenize 
from pylint.checkers.format import FormatChecker, _last_token_on_line_is, _JUNK_TOKENS 
import new 


class MyFormatChecker(object): 
    def new_line(self, tokens, line_end, line_start): 
     if _last_token_on_line_is(tokens, line_end, ';'): 
      self.add_message('unnecessary-semicolon', line=tokens.start_line(line_end)) 

     line_num = tokens.start_line(line_start) 
     line = tokens.line(line_start) 

     token_type = tokens.type(line_start) 
     if token_type not in _JUNK_TOKENS: 
      self._lines[line_num] = line.split('\n')[0] 

     if token_type not in (tokenize.COMMENT, tokenize.STRING): 
      self.check_lines(line, line_num) 


def register(linter): 
    format_checker = linter._checkers['format'][0] 
    format_checker.new_line = new.instancemethod(MyFormatChecker.new_line.im_func, format_checker, 
               FormatChecker.__class__) 

运行pylint--load-plugins命令行参数:

$ pylint --load-plugins checker script.py 

演示:

    没有插件的
  • $ pylint script.py 
    C: 2, 0: Line too long (85/79) (line-too-long) 
    C: 6, 0: Line too long (83/79) (line-too-long) 
    C: 1, 0: Missing module docstring (missing-docstring) 
    
  • 与插件(不抱怨的文档字符串)

    $ pylint --load-plugins=checker script.py 
    C: 6, 0: Line too long (83/79) (line-too-long) 
    C: 1, 0: Missing module docstring (missing-docstring) 
    

其中script.py包含:

def test(): 
    """This line is longer than 80 chars, but , for me this is ok inside a DOCSTRING, 
    this one is shorter. 
    """ 

    if 'This is toooooooooooooooooooooooooooooooooooo longggggggggggggggggggggggg': 
     print 'True' 

有迹象表明,必须要提到的几点注意事项:

  • 这太复杂,脆弱,不稳定,神奇的将被使用过
  • 看到的第一个音符