可以使用pylint插件和一些技巧来实现。
假设我们具有以下目录结构:
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 MANAGERfrom astroid import scoped_nodesdef register(linter): passdef 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-statementn' + c # pylint will read from `.file_bytes` attribute later when tokenization mod.file_bytes = cMANAGER.register_transform(scoped_nodes.Module, transform)
没有插件,pylint将报告:
************* Module tmp.exp_pylint.app.modW: 2, 4: Statement seems to have no effect (pointless-statement)************* Module tmp.exp_pylint.test.modW: 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.modW: 2, 4: Statement seems to have no effect (pointless-statement)
pylint通过对源文件进行标记化来读取注释,此插件可在运行时更改文件内容,以在标记化时欺骗pylint
。
注意,为简化演示,我在这里构造了一个“无意义声明”警告,禁用其他类型的消息是微不足道的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)