Skip to content

Commit f18d4ee

Browse files
Jason LauPierre-Sassoulas
authored andcommitted
Add applicability notes for compare-to-empty-string/zero (#8592)
The extension `compare-to-empty-string` is only applicable when the expression being compared is strictly a `str`. The extension `compare-to-zero` is only applicable when the expression being compared is strictly an `int`.
1 parent d285874 commit f18d4ee

File tree

5 files changed

+51
-51
lines changed

5 files changed

+51
-51
lines changed

doc/user_guide/checkers/features.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -893,14 +893,14 @@ Refactoring checker Messages
893893
Emitted when a single "return" or "return None" statement is found at the end
894894
of function or method definition. This statement can safely be removed
895895
because Python will implicitly return None
896-
:use-implicit-booleaness-not-comparison-to-zero (C1805): *"%s" can be simplified to "%s" as 0 is falsey*
897-
Used when Pylint detects comparison to a 0 constant.
898-
:use-implicit-booleaness-not-comparison-to-string (C1804): *"%s" can be simplified to "%s" as an empty string is falsey*
896+
:use-implicit-booleaness-not-comparison-to-string (C1804): *"%s" can be simplified to "%s", if it is striclty a string, as an empty string is falsey*
899897
Used when Pylint detects comparison to an empty string constant.
900-
:use-implicit-booleaness-not-comparison (C1803): *'%s' can be simplified to '%s' as an empty %s is falsey*
898+
:use-implicit-booleaness-not-comparison (C1803): *"%s" can be simplified to "%s", if it is strictly a sequence, as an empty %s is falsey*
901899
Used when Pylint detects that collection literal comparison is being used to
902900
check for emptiness; Use implicit booleaness instead of a collection classes;
903901
empty collections are considered as false
902+
:use-implicit-booleaness-not-comparison-to-zero (C1805): *"%s" can be simplified to "%s", if it is strictly an int, as 0 is falsey*
903+
Used when Pylint detects comparison to a 0 constant.
904904
:unneeded-not (C0113): *Consider changing "%s" to "%s"*
905905
Used when a boolean expression contains an unneeded negation.
906906
:consider-iterating-dictionary (C0201): *Consider iterating the dictionary directly instead of calling .keys()*

pylint/checkers/refactoring/implicit_booleaness_checker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ class ImplicitBooleanessChecker(checkers.BaseChecker):
7474
{"old_names": [("C1801", "len-as-condition")]},
7575
),
7676
"C1803": (
77-
'"%s" can be simplified to "%s" as an empty %s is falsey',
77+
'"%s" can be simplified to "%s", if it is strictly a sequence, as an empty %s is falsey',
7878
"use-implicit-booleaness-not-comparison",
7979
"Used when Pylint detects that collection literal comparison is being "
8080
"used to check for emptiness; Use implicit booleaness instead "
8181
"of a collection classes; empty collections are considered as false",
8282
),
8383
"C1804": (
84-
'"%s" can be simplified to "%s" as an empty string is falsey',
84+
'"%s" can be simplified to "%s", if it is striclty a string, as an empty string is falsey',
8585
"use-implicit-booleaness-not-comparison-to-string",
8686
"Used when Pylint detects comparison to an empty string constant.",
8787
{
@@ -90,7 +90,7 @@ class ImplicitBooleanessChecker(checkers.BaseChecker):
9090
},
9191
),
9292
"C1805": (
93-
'"%s" can be simplified to "%s" as 0 is falsey',
93+
'"%s" can be simplified to "%s", if it is strictly an int, as 0 is falsey',
9494
"use-implicit-booleaness-not-comparison-to-zero",
9595
"Used when Pylint detects comparison to a 0 constant.",
9696
{"default_enabled": False, "old_names": [("C2001", "compare-to-zero")]},
Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
use-implicit-booleaness-not-comparison:14:7:14:21:github_issue_4774:"""bad_list == []"" can be simplified to ""not bad_list"" as an empty list is falsey":HIGH
2-
use-implicit-booleaness-not-comparison:22:3:22:20::"""empty_tuple == ()"" can be simplified to ""not empty_tuple"" as an empty tuple is falsey":HIGH
3-
use-implicit-booleaness-not-comparison:25:3:25:19::"""empty_list == []"" can be simplified to ""not empty_list"" as an empty list is falsey":HIGH
4-
use-implicit-booleaness-not-comparison:28:3:28:19::"""empty_dict == {}"" can be simplified to ""not empty_dict"" as an empty dict is falsey":HIGH
5-
use-implicit-booleaness-not-comparison:31:3:31:20::"""empty_tuple == ()"" can be simplified to ""not empty_tuple"" as an empty tuple is falsey":HIGH
6-
use-implicit-booleaness-not-comparison:34:3:34:19::"""empty_list == []"" can be simplified to ""not empty_list"" as an empty list is falsey":HIGH
7-
use-implicit-booleaness-not-comparison:37:3:37:19::"""empty_dict == {}"" can be simplified to ""not empty_dict"" as an empty dict is falsey":HIGH
8-
use-implicit-booleaness-not-comparison:42:11:42:18:bad_tuple_return:"""t == ()"" can be simplified to ""not t"" as an empty tuple is falsey":HIGH
9-
use-implicit-booleaness-not-comparison:46:11:46:18:bad_list_return:"""b == []"" can be simplified to ""not b"" as an empty list is falsey":HIGH
10-
use-implicit-booleaness-not-comparison:50:11:50:18:bad_dict_return:"""c == {}"" can be simplified to ""not c"" as an empty dict is falsey":HIGH
11-
use-implicit-booleaness-not-comparison:52:7:52:24::"""empty_tuple == ()"" can be simplified to ""not empty_tuple"" as an empty tuple is falsey":HIGH
12-
use-implicit-booleaness-not-comparison:53:7:53:23::"""empty_list == []"" can be simplified to ""not empty_list"" as an empty list is falsey":HIGH
13-
use-implicit-booleaness-not-comparison:54:7:54:23::"""empty_dict != {}"" can be simplified to ""empty_dict"" as an empty dict is falsey":HIGH
14-
use-implicit-booleaness-not-comparison:55:7:55:23::"""empty_tuple < ()"" can be simplified to ""not empty_tuple"" as an empty tuple is falsey":HIGH
15-
use-implicit-booleaness-not-comparison:56:7:56:23::"""empty_list <= []"" can be simplified to ""not empty_list"" as an empty list is falsey":HIGH
16-
use-implicit-booleaness-not-comparison:57:7:57:23::"""empty_tuple > ()"" can be simplified to ""not empty_tuple"" as an empty tuple is falsey":HIGH
17-
use-implicit-booleaness-not-comparison:58:7:58:23::"""empty_list >= []"" can be simplified to ""not empty_list"" as an empty list is falsey":HIGH
18-
use-implicit-booleaness-not-comparison:83:3:83:10::"""a == []"" can be simplified to ""not a"" as an empty list is falsey":HIGH
19-
use-implicit-booleaness-not-comparison:95:3:95:10::"""e == []"" can be simplified to ""not e"" as an empty list is falsey":HIGH
20-
use-implicit-booleaness-not-comparison:95:15:95:22::"""f == {}"" can be simplified to ""not f"" as an empty dict is falsey":HIGH
21-
use-implicit-booleaness-not-comparison:133:3:133:14::"""A.lst == []"" can be simplified to ""not A.lst"" as an empty list is falsey":HIGH
22-
use-implicit-booleaness-not-comparison:137:3:137:14::"""A.lst == []"" can be simplified to ""not A.lst"" as an empty list is falsey":HIGH
23-
use-implicit-booleaness-not-comparison:141:3:141:20::"""A.test(...) == []"" can be simplified to ""not A.test(...)"" as an empty list is falsey":HIGH
24-
use-implicit-booleaness-not-comparison:149:3:149:24::"""test_function(...) == []"" can be simplified to ""not test_function(...)"" as an empty list is falsey":HIGH
25-
use-implicit-booleaness-not-comparison:156:3:156:20::"""numpy_array == []"" can be simplified to ""not numpy_array"" as an empty list is falsey":HIGH
26-
use-implicit-booleaness-not-comparison:158:3:158:20::"""numpy_array != []"" can be simplified to ""numpy_array"" as an empty list is falsey":HIGH
27-
use-implicit-booleaness-not-comparison:160:3:160:20::"""numpy_array >= ()"" can be simplified to ""not numpy_array"" as an empty tuple is falsey":HIGH
28-
use-implicit-booleaness-not-comparison:185:3:185:13::"""data == {}"" can be simplified to ""not data"" as an empty dict is falsey":HIGH
29-
use-implicit-booleaness-not-comparison:187:3:187:13::"""data != {}"" can be simplified to ""data"" as an empty dict is falsey":HIGH
30-
use-implicit-booleaness-not-comparison:195:3:195:26::"""long_test == {}"" can be simplified to ""not long_test"" as an empty dict is falsey":HIGH
31-
use-implicit-booleaness-not-comparison:233:11:233:41:test_func:"""my_class.parent_function == {}"" can be simplified to ""not my_class.parent_function"" as an empty dict is falsey":HIGH
32-
use-implicit-booleaness-not-comparison:234:11:234:37:test_func:"""my_class.my_property == {}"" can be simplified to ""not my_class.my_property"" as an empty dict is falsey":HIGH
1+
use-implicit-booleaness-not-comparison:14:7:14:21:github_issue_4774:"""bad_list == []"" can be simplified to ""not bad_list"", if it is strictly a sequence, as an empty list is falsey":HIGH
2+
use-implicit-booleaness-not-comparison:22:3:22:20::"""empty_tuple == ()"" can be simplified to ""not empty_tuple"", if it is strictly a sequence, as an empty tuple is falsey":HIGH
3+
use-implicit-booleaness-not-comparison:25:3:25:19::"""empty_list == []"" can be simplified to ""not empty_list"", if it is strictly a sequence, as an empty list is falsey":HIGH
4+
use-implicit-booleaness-not-comparison:28:3:28:19::"""empty_dict == {}"" can be simplified to ""not empty_dict"", if it is strictly a sequence, as an empty dict is falsey":HIGH
5+
use-implicit-booleaness-not-comparison:31:3:31:20::"""empty_tuple == ()"" can be simplified to ""not empty_tuple"", if it is strictly a sequence, as an empty tuple is falsey":HIGH
6+
use-implicit-booleaness-not-comparison:34:3:34:19::"""empty_list == []"" can be simplified to ""not empty_list"", if it is strictly a sequence, as an empty list is falsey":HIGH
7+
use-implicit-booleaness-not-comparison:37:3:37:19::"""empty_dict == {}"" can be simplified to ""not empty_dict"", if it is strictly a sequence, as an empty dict is falsey":HIGH
8+
use-implicit-booleaness-not-comparison:42:11:42:18:bad_tuple_return:"""t == ()"" can be simplified to ""not t"", if it is strictly a sequence, as an empty tuple is falsey":HIGH
9+
use-implicit-booleaness-not-comparison:46:11:46:18:bad_list_return:"""b == []"" can be simplified to ""not b"", if it is strictly a sequence, as an empty list is falsey":HIGH
10+
use-implicit-booleaness-not-comparison:50:11:50:18:bad_dict_return:"""c == {}"" can be simplified to ""not c"", if it is strictly a sequence, as an empty dict is falsey":HIGH
11+
use-implicit-booleaness-not-comparison:52:7:52:24::"""empty_tuple == ()"" can be simplified to ""not empty_tuple"", if it is strictly a sequence, as an empty tuple is falsey":HIGH
12+
use-implicit-booleaness-not-comparison:53:7:53:23::"""empty_list == []"" can be simplified to ""not empty_list"", if it is strictly a sequence, as an empty list is falsey":HIGH
13+
use-implicit-booleaness-not-comparison:54:7:54:23::"""empty_dict != {}"" can be simplified to ""empty_dict"", if it is strictly a sequence, as an empty dict is falsey":HIGH
14+
use-implicit-booleaness-not-comparison:55:7:55:23::"""empty_tuple < ()"" can be simplified to ""not empty_tuple"", if it is strictly a sequence, as an empty tuple is falsey":HIGH
15+
use-implicit-booleaness-not-comparison:56:7:56:23::"""empty_list <= []"" can be simplified to ""not empty_list"", if it is strictly a sequence, as an empty list is falsey":HIGH
16+
use-implicit-booleaness-not-comparison:57:7:57:23::"""empty_tuple > ()"" can be simplified to ""not empty_tuple"", if it is strictly a sequence, as an empty tuple is falsey":HIGH
17+
use-implicit-booleaness-not-comparison:58:7:58:23::"""empty_list >= []"" can be simplified to ""not empty_list"", if it is strictly a sequence, as an empty list is falsey":HIGH
18+
use-implicit-booleaness-not-comparison:83:3:83:10::"""a == []"" can be simplified to ""not a"", if it is strictly a sequence, as an empty list is falsey":HIGH
19+
use-implicit-booleaness-not-comparison:95:3:95:10::"""e == []"" can be simplified to ""not e"", if it is strictly a sequence, as an empty list is falsey":HIGH
20+
use-implicit-booleaness-not-comparison:95:15:95:22::"""f == {}"" can be simplified to ""not f"", if it is strictly a sequence, as an empty dict is falsey":HIGH
21+
use-implicit-booleaness-not-comparison:133:3:133:14::"""A.lst == []"" can be simplified to ""not A.lst"", if it is strictly a sequence, as an empty list is falsey":HIGH
22+
use-implicit-booleaness-not-comparison:137:3:137:14::"""A.lst == []"" can be simplified to ""not A.lst"", if it is strictly a sequence, as an empty list is falsey":HIGH
23+
use-implicit-booleaness-not-comparison:141:3:141:20::"""A.test(...) == []"" can be simplified to ""not A.test(...)"", if it is strictly a sequence, as an empty list is falsey":HIGH
24+
use-implicit-booleaness-not-comparison:149:3:149:24::"""test_function(...) == []"" can be simplified to ""not test_function(...)"", if it is strictly a sequence, as an empty list is falsey":HIGH
25+
use-implicit-booleaness-not-comparison:156:3:156:20::"""numpy_array == []"" can be simplified to ""not numpy_array"", if it is strictly a sequence, as an empty list is falsey":HIGH
26+
use-implicit-booleaness-not-comparison:158:3:158:20::"""numpy_array != []"" can be simplified to ""numpy_array"", if it is strictly a sequence, as an empty list is falsey":HIGH
27+
use-implicit-booleaness-not-comparison:160:3:160:20::"""numpy_array >= ()"" can be simplified to ""not numpy_array"", if it is strictly a sequence, as an empty tuple is falsey":HIGH
28+
use-implicit-booleaness-not-comparison:185:3:185:13::"""data == {}"" can be simplified to ""not data"", if it is strictly a sequence, as an empty dict is falsey":HIGH
29+
use-implicit-booleaness-not-comparison:187:3:187:13::"""data != {}"" can be simplified to ""data"", if it is strictly a sequence, as an empty dict is falsey":HIGH
30+
use-implicit-booleaness-not-comparison:195:3:195:26::"""long_test == {}"" can be simplified to ""not long_test"", if it is strictly a sequence, as an empty dict is falsey":HIGH
31+
use-implicit-booleaness-not-comparison:233:11:233:41:test_func:"""my_class.parent_function == {}"" can be simplified to ""not my_class.parent_function"", if it is strictly a sequence, as an empty dict is falsey":HIGH
32+
use-implicit-booleaness-not-comparison:234:11:234:37:test_func:"""my_class.my_property == {}"" can be simplified to ""not my_class.my_property"", if it is strictly a sequence, as an empty dict is falsey":HIGH
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use-implicit-booleaness-not-comparison-to-string:6:3:6:10::"""X is ''"" can be simplified to ""not X"" as an empty string is falsey":HIGH
2-
use-implicit-booleaness-not-comparison-to-string:9:3:9:14::"""Y is not ''"" can be simplified to ""Y"" as an empty string is falsey":HIGH
3-
use-implicit-booleaness-not-comparison-to-string:12:3:12:10::"""X == ''"" can be simplified to ""not X"" as an empty string is falsey":HIGH
4-
use-implicit-booleaness-not-comparison-to-string:15:3:15:10::"""Y != ''"" can be simplified to ""Y"" as an empty string is falsey":HIGH
5-
use-implicit-booleaness-not-comparison-to-string:18:3:18:10::"""'' == Y"" can be simplified to ""not Y"" as an empty string is falsey":HIGH
6-
use-implicit-booleaness-not-comparison-to-string:21:3:21:10::"""'' != X"" can be simplified to ""X"" as an empty string is falsey":HIGH
1+
use-implicit-booleaness-not-comparison-to-string:6:3:6:10::"""X is ''"" can be simplified to ""not X"", if it is striclty a string, as an empty string is falsey":HIGH
2+
use-implicit-booleaness-not-comparison-to-string:9:3:9:14::"""Y is not ''"" can be simplified to ""Y"", if it is striclty a string, as an empty string is falsey":HIGH
3+
use-implicit-booleaness-not-comparison-to-string:12:3:12:10::"""X == ''"" can be simplified to ""not X"", if it is striclty a string, as an empty string is falsey":HIGH
4+
use-implicit-booleaness-not-comparison-to-string:15:3:15:10::"""Y != ''"" can be simplified to ""Y"", if it is striclty a string, as an empty string is falsey":HIGH
5+
use-implicit-booleaness-not-comparison-to-string:18:3:18:10::"""'' == Y"" can be simplified to ""not Y"", if it is striclty a string, as an empty string is falsey":HIGH
6+
use-implicit-booleaness-not-comparison-to-string:21:3:21:10::"""'' != X"" can be simplified to ""X"", if it is striclty a string, as an empty string is falsey":HIGH
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use-implicit-booleaness-not-comparison-to-zero:6:3:6:9::"""X is 0"" can be simplified to ""not X"" as 0 is falsey":HIGH
2-
use-implicit-booleaness-not-comparison-to-zero:12:3:12:13::"""Y is not 0"" can be simplified to ""Y"" as 0 is falsey":HIGH
3-
use-implicit-booleaness-not-comparison-to-zero:18:3:18:9::"""X == 0"" can be simplified to ""not X"" as 0 is falsey":HIGH
4-
use-implicit-booleaness-not-comparison-to-zero:24:3:24:9::"""0 == Y"" can be simplified to ""not Y"" as 0 is falsey":HIGH
5-
use-implicit-booleaness-not-comparison-to-zero:27:3:27:9::"""Y != 0"" can be simplified to ""Y"" as 0 is falsey":HIGH
6-
use-implicit-booleaness-not-comparison-to-zero:30:3:30:9::"""0 != X"" can be simplified to ""X"" as 0 is falsey":HIGH
1+
use-implicit-booleaness-not-comparison-to-zero:6:3:6:9::"""X is 0"" can be simplified to ""not X"", if it is strictly an int, as 0 is falsey":HIGH
2+
use-implicit-booleaness-not-comparison-to-zero:12:3:12:13::"""Y is not 0"" can be simplified to ""Y"", if it is strictly an int, as 0 is falsey":HIGH
3+
use-implicit-booleaness-not-comparison-to-zero:18:3:18:9::"""X == 0"" can be simplified to ""not X"", if it is strictly an int, as 0 is falsey":HIGH
4+
use-implicit-booleaness-not-comparison-to-zero:24:3:24:9::"""0 == Y"" can be simplified to ""not Y"", if it is strictly an int, as 0 is falsey":HIGH
5+
use-implicit-booleaness-not-comparison-to-zero:27:3:27:9::"""Y != 0"" can be simplified to ""Y"", if it is strictly an int, as 0 is falsey":HIGH
6+
use-implicit-booleaness-not-comparison-to-zero:30:3:30:9::"""0 != X"" can be simplified to ""X"", if it is strictly an int, as 0 is falsey":HIGH

0 commit comments

Comments
 (0)