Getting false positive from pylint
Consider this code:
previous = None
for word in ['you', 'cannot', 'be', 'serious']:
if previous is not None:
print(previous[0], word)
previous = word
Here is the output (no surprises here):
$ python3 test.py
y cannot
c be
b serious
Checking it using pylint
gives the following:
$ 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)
As you can see, pylint
has complained about the expression previous[0]
based on the fact that previous
is initialised to None
, even though the statement cannot be reached when it has that value. Additionally, it has complained about the naming style on the basis that previous
is a constant, despite the assignment on the final line.
QUESTION: Is there any suitable configuration of pylint that avoids such false positives without introducing equally blatant false negatives, and if so, why is this not the default?
Attempted solution: disable test (leads to false negative)
I am able for example to suppress the unsubscriptable-object
warning entirely, by creating a $HOME/.pylintrc
containing the output of pylint3 --generate-rcfile
, and editing it to add unsubscriptable-object
to the list of disabled tests (see under disable=
in section [MESSAGES CONTROL]
).
However, if I simply disable the test in this way, and then validate the following code:
"false negative" # silence docstring warning (fair enough)
print(None[0]) # this line will fail
it tells me:
$ 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)
So just disabling the test is not really the answer.
Version and platform info
Version info:
$ 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]
Platform: Ubuntu 18.04.4 LTS