Skip to content

Commit 73787bf

Browse files
[compare to zero] Fix false positive with 'x is False'
1 parent b0191fe commit 73787bf

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

pylint/extensions/comparetozero.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121

2222

2323
def _is_constant_zero(node: str | nodes.NodeNG) -> bool:
24-
return isinstance(node, astroid.Const) and node.value == 0
24+
# We have to check that node.value is not False because node.value == 0 is True
25+
# when node.value is False
26+
return (
27+
isinstance(node, astroid.Const) and node.value == 0 and node.value is not False
28+
)
2529

2630

2731
class CompareToZeroChecker(checkers.BaseChecker):

tests/functional/ext/comparetozero/compare_to_zero.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
if X is 0: # [compare-to-zero]
77
pass
88

9+
if X is False:
10+
pass
11+
912
if Y is not 0: # [compare-to-zero]
1013
pass
1114

15+
if Y is not False:
16+
pass
17+
1218
if X == 0: # [compare-to-zero]
1319
pass
1420

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
compare-to-zero:6:3:6:9::"""X is 0"" can be simplified to ""not X"" as 0 is falsey":HIGH
2-
compare-to-zero:9:3:9:13::"""Y is not 0"" can be simplified to ""Y"" as 0 is falsey":HIGH
3-
compare-to-zero:12:3:12:9::"""X == 0"" can be simplified to ""not X"" as 0 is falsey":HIGH
4-
compare-to-zero:15:3:15:9::"""0 == Y"" can be simplified to ""not Y"" as 0 is falsey":HIGH
5-
compare-to-zero:18:3:18:9::"""Y != 0"" can be simplified to ""Y"" as 0 is falsey":HIGH
6-
compare-to-zero:21:3:21:9::"""0 != X"" can be simplified to ""X"" as 0 is falsey":HIGH
2+
compare-to-zero:12:3:12:13::"""Y is not 0"" can be simplified to ""Y"" as 0 is falsey":HIGH
3+
compare-to-zero:18:3:18:9::"""X == 0"" can be simplified to ""not X"" as 0 is falsey":HIGH
4+
compare-to-zero:21:3:21:9::"""0 == Y"" can be simplified to ""not Y"" as 0 is falsey":HIGH
5+
compare-to-zero:24:3:24:9::"""Y != 0"" can be simplified to ""Y"" as 0 is falsey":HIGH
6+
compare-to-zero:27:3:27:9::"""0 != X"" can be simplified to ""X"" as 0 is falsey":HIGH

0 commit comments

Comments
 (0)