Pylint:避免检查INSIDE DOCSTRINGS(全局指令/ rcfile)

时间:2014-12-31 11:51:29

标签: python pylint static-code-analysis

考虑这段代码

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)

是否有指令可用(rcfile)从DOCSTRINGS检查程序中排除 pylint

regex多行 ignore-long-lines谢谢 @fredtantini )似乎被忽略了。

围绕 docstrings # pylint: dis/enable=line-too-long谢谢 @Evert )会产生所需的效果,但是,我正在寻找一个全局指令来制作技巧

1 个答案:

答案 0 :(得分: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应用&#34;太长的行&#34;检查。

还要注意一个有趣的事实:pylint代码质量检查实用程序有这个误导性的文档字符串:

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

虽然该方法还会检查missing-final-newlinetrailing-whitespace。这是你永远不会静态捕获的东西 - 人类错误,只能被人注意和修复。


我不喜欢以下提案,但我们可以猴子修补FormatChecker动态

创建一个名为&#34; checker.py&#34;的脚本。并把它放在PYTHONPATH上。在这里,我们重新定义new_line()方法并添加&#34;令牌类型&#34; check - 如果这是注释或字符串(docstring),则不允许检查器调用check_lines()方法。然后,在register()函数中,我们将覆盖内置的FormatChecker new_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' 包含:

{{1}}

有几点需要注意:

  • 这是太复杂,脆弱,不稳定和神奇的使用
  • 见第一个注释
相关问题