diff options
author | Sergei Lebedev <185856+superbobry@users.noreply.github.com> | 2018-11-23 12:39:05 +0000 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-11-23 13:39:05 +0100 |
commit | bc25da2681ceef9560c13d1e412aa79c49c67a57 (patch) | |
tree | 6d84e3f692848de8d99614e746106a4bf78f4f15 | |
parent | 6e37ca37a78024dcc79ceb2431b0dfebd526f3ad (diff) | |
download | pylint-git-bc25da2681ceef9560c13d1e412aa79c49c67a57.tar.gz |
Fixed literal-comparison for the case of 0 and 1 (#2601)
Prior to this commit literal-comparison did not fire for the following
comparisons
x is 0
x is 1
due to a subtle bug in the checker implementation. The skip condition
in the ``ComparisonChecker._check_literal_comparison`` was
literal.value in (True, False, None)
which holds for True/False/None _as well as_ 0 and 1, since 0 == False
and 1 == True.
-rw-r--r-- | CONTRIBUTORS.txt | 2 | ||||
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | pylint/checkers/base.py | 2 | ||||
-rw-r--r-- | pylint/test/functional/literal_comparison.py | 13 | ||||
-rw-r--r-- | pylint/test/functional/literal_comparison.txt | 5 |
5 files changed, 21 insertions, 3 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 9f71b8fe6..3e04def95 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -247,3 +247,5 @@ contributors: * Lucas Cimon: contributor * Mike Miller: contributor + +* Sergei Lebedev: contributor @@ -173,6 +173,8 @@ Release date: TBA * Add a new check 'implicit-str-concat-in-sequence' to spot string concatenation inside lists, sets & tuples. + * ``literal-comparison`` is now emitted for 0 and 1 literals. + What's New in Pylint 2.1.1? =========================== diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py index 4a08f9325..beb7c1300 100644 --- a/pylint/checkers/base.py +++ b/pylint/checkers/base.py @@ -2080,7 +2080,7 @@ class ComparisonChecker(_BasicChecker): is_other_literal = isinstance(literal, nodes) is_const = False if isinstance(literal, astroid.Const): - if literal.value in (True, False, None): + if isinstance(literal.value, bool) or literal.value is None: # Not interested in this values. return is_const = isinstance(literal.value, (bytes, str, int, float)) diff --git a/pylint/test/functional/literal_comparison.py b/pylint/test/functional/literal_comparison.py index 531e5ce1e..646e57eec 100644 --- a/pylint/test/functional/literal_comparison.py +++ b/pylint/test/functional/literal_comparison.py @@ -32,8 +32,19 @@ if () is not {1, 2, 3}: # [literal-comparison] pass +CONST = 24 + + +if CONST is 0: # [literal-comparison] + pass + +if CONST is 1: # [literal-comparison] + pass + +if CONST is 42: # [literal-comparison] + pass + # We do not do inference for this check, since the comparing # object might be used as a sentinel. -CONST = 24 if () is CONST: pass diff --git a/pylint/test/functional/literal_comparison.txt b/pylint/test/functional/literal_comparison.txt index ad2b2e52f..5026deceb 100644 --- a/pylint/test/functional/literal_comparison.txt +++ b/pylint/test/functional/literal_comparison.txt @@ -7,4 +7,7 @@ literal-comparison:19::Comparison to literal literal-comparison:22::Comparison to literal literal-comparison:25::Comparison to literal literal-comparison:28::Comparison to literal -literal-comparison:31::Comparison to literal
\ No newline at end of file +literal-comparison:31::Comparison to literal +literal-comparison:38::Comparison to literal +literal-comparison:41::Comparison to literal +literal-comparison:44::Comparison to literal |