从 pylint 获得误报
考虑这段代码:
previous = None
for word in ['you', 'cannot', 'be', 'serious']:
if previous is not None:
print(previous[0], word)
previous = word
这是输出(这里没有惊喜):
$ python3 test.py
y cannot
c be
b serious
检查它使用pylint
给出以下内容:
$ pylint3 test.py
No config file found, using default configuration
************* Module test
C: 1, 0: Missing module docstring (missing-docstring)
C: 1, 0: Constant name "previous" doesn't conform to UPPER_CASE naming style (invalid-name)
E: 4,14: Value 'previous' is unsubscriptable (unsubscriptable-object)
--------------------------------------------------------------------
Your code has been rated at -4.00/10 (previous run: -4.00/10, +0.00)
如您所见,基于初始化为的事实pylint
抱怨该表达式,即使当它具有该值时无法到达该语句。此外,尽管在最后一行进行了赋值,但它仍抱怨基于常量的命名样式。previous[0]
previous
None
previous
问题:是否有任何合适的 pylint 配置可以避免此类误报,而不会引入同样明显的误报,如果是这样,为什么这不是默认配置?
尝试的解决方案:禁用测试(导致假阴性)
例如,我可以unsubscriptable-object
完全抑制警告,方法是创建一个$HOME/.pylintrc
包含 的输出pylint3 --generate-rcfile
,并对其进行编辑以添加unsubscriptable-object
到禁用测试列表中(请参阅下面disable=
的部分[MESSAGES CONTROL]
)。
但是,如果我只是以这种方式禁用测试,然后验证以下代码:
"false negative" # silence docstring warning (fair enough)
print(None[0]) # this line will fail
它告诉我:
$ pylint3 test2.py
Using config file /home/<myuser>/.pylintrc
--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)
所以仅仅禁用测试并不是真正的答案。
版本和平台信息
版本信息:
$ pylint3 --version
Using config file /home/<myuser>/.pylintrc
pylint3 1.8.3,
astroid 1.6.0
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0]
平台:Ubuntu 18.04.4 LTS