禁用给定模块或目录的pylint消息

Tin*_*rus 8 python unit-testing pylint

有没有办法duplicate-code只为测试文件禁用Pylint的消息?我们项目中的所有测试都是DAMP,因此重复的代码是设计的.我知道我们可以在# pylint: disable=duplicate-code整个测试过程中添加,但宁可添加一些规则,说明文件test/夹下的所有文件都会禁用此规则.有没有办法做到这一点?

更具体地说,我正在寻找与"运行两次"解决方案不同的东西(这是我已经重新开始的).

geo*_*xsh 12

它可以用pylint插件和一些hack来实现.

假设我们有以下目录结构:

 pylint_plugin.py
 app
 ??? __init__.py
 ??? mod.py
 test
 ??? __init__.py
 ??? mod.py
Run Code Online (Sandbox Code Playgroud)

mod.py的内容:

def f():
    1/0
Run Code Online (Sandbox Code Playgroud)

pylint_plugin.py的内容:

from astroid import MANAGER
from astroid import scoped_nodes


def register(linter):
    pass


def transform(mod):
    if 'test.' not in mod.name:
        return
    c = mod.stream().read()
    # change to the message-id you need
    c = b'# pylint: disable=pointless-statement\n' + c
    # pylint will read from `.file_bytes` attribute later when tokenization
    mod.file_bytes = c


MANAGER.register_transform(scoped_nodes.Module, transform)
Run Code Online (Sandbox Code Playgroud)

没有插件,pylint会报告:

************* Module tmp.exp_pylint.app.mod
W:  2, 4: Statement seems to have no effect (pointless-statement)
************* Module tmp.exp_pylint.test.mod
W:  2, 4: Statement seems to have no effect (pointless-statement)
Run Code Online (Sandbox Code Playgroud)

加载插件:

PYTHONPATH=. pylint -dC,R --load-plugins pylint_plugin app test
Run Code Online (Sandbox Code Playgroud)

收益率:

************* Module tmp.exp_pylint.app.mod
W:  2, 4: Statement seems to have no effect (pointless-statement)
Run Code Online (Sandbox Code Playgroud)

pylint通过标记源文件读取注释,此插件动态更改文件内容,以便在标记化时欺骗pylint .

注意,为了简化演示,这里我构建了一个"无意义语句"警告,禁用其他类型的消息是微不足道的.

  • 一个非常小的问题是,这会导致其他报告的问题出现逐一错误,因为在“test.”文件的开头添加了一行 (2认同)