禁用给定模块或目录的 pylint 消息
Disable pylint message for a given module or directory
有没有办法仅针对测试文件禁用 Pylint 的 duplicate-code
消息?我们项目中的所有测试都是 DAMP,因此重复代码是设计使然。我知道我们可以在整个测试过程中添加 # pylint: disable=duplicate-code
,但更愿意添加某种规则,说明 test/
文件夹下的所有文件都将禁用此规则。有没有办法做到这一点?
更具体地说,我正在寻找与 'run it twice' 解决方案不同的东西(这是我已经求助的)。
有一个 --disable
or -d
message control flag 可用于在调用时有选择地禁用消息。因此,您可以通过 运行ning pylint 对项目文件夹中的这些文件禁用测试文件夹下所有文件的此消息:
pylint -d duplicate-code test/
我能够验证我可以从目录中的所有文件中删除特定消息,尽管我没有遇到重复代码错误,因此无法检查该消息。
您也可以将其放入项目主目录中 运行 的脚本中。类似于:
#!/bin/bash
pylint src/
pylint -d duplicate-code test/
或者,您可以将 # pylint: disable=duplicate-code
添加到要排除这些消息的每个文件的顶部。就文件方面的选择性排除标志而言,这似乎是关于 pylint 的。
可以通过 pylint 插件和一些 hack 来实现。
假设我们有以下目录结构:
pylint_plugin.py
app
├── __init__.py
└── mod.py
test
├── __init__.py
└── mod.py
mod.py的内容:
def f():
1/0
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)
没有插件,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)
已加载插件:
PYTHONPATH=. pylint -dC,R --load-plugins pylint_plugin app test
产量:
************* Module tmp.exp_pylint.app.mod
W: 2, 4: Statement seems to have no effect (pointless-statement)
pylint 通过标记化源文件读取评论,此插件 change file content on the fly, to cheat pylint when tokenization.
注意,为了简化演示,我这里构造了一个"pointless-statement"警告,禁用其他类型的消息是微不足道的。
有没有办法仅针对测试文件禁用 Pylint 的 duplicate-code
消息?我们项目中的所有测试都是 DAMP,因此重复代码是设计使然。我知道我们可以在整个测试过程中添加 # pylint: disable=duplicate-code
,但更愿意添加某种规则,说明 test/
文件夹下的所有文件都将禁用此规则。有没有办法做到这一点?
更具体地说,我正在寻找与 'run it twice' 解决方案不同的东西(这是我已经求助的)。
有一个 --disable
or -d
message control flag 可用于在调用时有选择地禁用消息。因此,您可以通过 运行ning pylint 对项目文件夹中的这些文件禁用测试文件夹下所有文件的此消息:
pylint -d duplicate-code test/
我能够验证我可以从目录中的所有文件中删除特定消息,尽管我没有遇到重复代码错误,因此无法检查该消息。
您也可以将其放入项目主目录中 运行 的脚本中。类似于:
#!/bin/bash
pylint src/
pylint -d duplicate-code test/
或者,您可以将 # pylint: disable=duplicate-code
添加到要排除这些消息的每个文件的顶部。就文件方面的选择性排除标志而言,这似乎是关于 pylint 的。
可以通过 pylint 插件和一些 hack 来实现。
假设我们有以下目录结构:
pylint_plugin.py
app
├── __init__.py
└── mod.py
test
├── __init__.py
└── mod.py
mod.py的内容:
def f():
1/0
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)
没有插件,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)
已加载插件:
PYTHONPATH=. pylint -dC,R --load-plugins pylint_plugin app test
产量:
************* Module tmp.exp_pylint.app.mod
W: 2, 4: Statement seems to have no effect (pointless-statement)
pylint 通过标记化源文件读取评论,此插件 change file content on the fly, to cheat pylint when tokenization.
注意,为了简化演示,我这里构造了一个"pointless-statement"警告,禁用其他类型的消息是微不足道的。