diff --git a/doc/data/messages/u/use-implicit-booleaness-not-comparison-to-string/details.rst b/doc/data/messages/u/use-implicit-booleaness-not-comparison-to-string/details.rst deleted file mode 100644 index c5781b739f..0000000000 --- a/doc/data/messages/u/use-implicit-booleaness-not-comparison-to-string/details.rst +++ /dev/null @@ -1,3 +0,0 @@ -Following this check blindly in weakly typed code base can create hard to debug issues. If the value -can be something else that is falsey but not a string (for example ``None``, an empty sequence, or ``0``), -the code will not be equivalent. diff --git a/doc/data/messages/u/use-implicit-booleaness-not-comparison-to-zero/details.rst b/doc/data/messages/u/use-implicit-booleaness-not-comparison-to-zero/details.rst deleted file mode 100644 index 2f86acf546..0000000000 --- a/doc/data/messages/u/use-implicit-booleaness-not-comparison-to-zero/details.rst +++ /dev/null @@ -1,3 +0,0 @@ -Following this check blindly in weakly typed code base can create hard to debug issues. If the value -can be something else that is falsey but not an ``int`` (for example ``None``, an empty sequence, -or an empty string), the code will not be equivalent. diff --git a/doc/data/messages/u/use-implicit-booleaness-not-comparison/details.rst b/doc/data/messages/u/use-implicit-booleaness-not-comparison/details.rst deleted file mode 100644 index 0dc1802459..0000000000 --- a/doc/data/messages/u/use-implicit-booleaness-not-comparison/details.rst +++ /dev/null @@ -1,3 +0,0 @@ -Following this check blindly in weakly typed code base can create hard to debug issues. If the value -can be something else that is falsey but not a sequence (for example ``None``, an empty string, or ``0``), -the code will not be equivalent. diff --git a/doc/user_guide/checkers/features.rst b/doc/user_guide/checkers/features.rst index 789d59b8b6..eb716d3897 100644 --- a/doc/user_guide/checkers/features.rst +++ b/doc/user_guide/checkers/features.rst @@ -893,14 +893,21 @@ Refactoring checker Messages Emitted when a single "return" or "return None" statement is found at the end of function or method definition. This statement can safely be removed because Python will implicitly return None -:use-implicit-booleaness-not-comparison-to-zero (C1805): *"%s" can be simplified to "%s" as 0 is falsey* - Used when Pylint detects comparison to a 0 constant. -:use-implicit-booleaness-not-comparison-to-string (C1804): *"%s" can be simplified to "%s" as an empty string is falsey* - Used when Pylint detects comparison to an empty string constant. -:use-implicit-booleaness-not-comparison (C1803): *'%s' can be simplified to '%s' as an empty %s is falsey* - Used when Pylint detects that collection literal comparison is being used to - check for emptiness; Use implicit booleaness instead of a collection classes; - empty collections are considered as false +: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* + Empty string are considered false in a boolean context. Following this check + blindly in weakly typed code base can create hard to debug issues. If the + value can be something else that is falsey but not a string (for example + ``None``, an empty sequence, or ``0``) the code will not be equivalent. +:use-implicit-booleaness-not-comparison (C1803): *"%s" can be simplified to "%s", if it is strictly a sequence, as an empty %s is falsey* + Empty sequences are considered false in a boolean context. Following this + check blindly in weakly typed code base can create hard to debug issues. If + the value can be something else that is falsey but not a sequence (for + example ``None``, an empty string, or ``0``) the code will not be equivalent. +:use-implicit-booleaness-not-comparison-to-zero (C1805): *"%s" can be simplified to "%s", if it is strictly an int, as 0 is falsey* + 0 is considered false in a boolean context. Following this check blindly in + weakly typed code base can create hard to debug issues. If the value can be + something else that is falsey but not an int (for example ``None``, an empty + string, or an empty sequence) the code will not be equivalent. :unneeded-not (C0113): *Consider changing "%s" to "%s"* Used when a boolean expression contains an unneeded negation. :consider-iterating-dictionary (C0201): *Consider iterating the dictionary directly instead of calling .keys()* @@ -916,10 +923,9 @@ Refactoring checker Messages Emitted when code that iterates with range and len is encountered. Such code can be simplified by using the enumerate builtin. :use-implicit-booleaness-not-len (C1802): *Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty* - Used when Pylint detects that len(sequence) is being used without explicit - comparison inside a condition to determine if a sequence is empty. Instead of - coercing the length to a boolean, either rely on the fact that empty - sequences are false or compare the length against a scalar. + Empty sequences are considered false in a boolean context. You can either + remove the call to 'len' (``if not x``) or compare the length against ascalar + (``if len(x) > 1``). :consider-using-f-string (C0209): *Formatting a regular string which could be an f-string* Used when we detect a string that is being formatted with format() or % which could potentially be an f-string. The use of f-strings is preferred. Requires diff --git a/doc/whatsnew/fragments/6871.user_action b/doc/whatsnew/fragments/6871.user_action index eace82f7a5..7cabc1ca16 100644 --- a/doc/whatsnew/fragments/6871.user_action +++ b/doc/whatsnew/fragments/6871.user_action @@ -9,7 +9,9 @@ and they now need to be enabled explicitly. The `pylint.extensions.emptystring`` and ``pylint.extensions.compare-to-zero`` extensions no longer exists and needs to be removed from the ``load-plugins`` option. +Messages related to implicit booleaness were made more explicit and actionable. + This permits to make their likeness explicit and will provide better performance as they share most of their conditions to be raised. -Refs #6871 +Closes #6871 diff --git a/pylint/checkers/refactoring/implicit_booleaness_checker.py b/pylint/checkers/refactoring/implicit_booleaness_checker.py index 93ac44269b..5b91990b03 100644 --- a/pylint/checkers/refactoring/implicit_booleaness_checker.py +++ b/pylint/checkers/refactoring/implicit_booleaness_checker.py @@ -66,38 +66,47 @@ class ImplicitBooleanessChecker(checkers.BaseChecker): "C1802": ( "Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty", "use-implicit-booleaness-not-len", - "Used when Pylint detects that len(sequence) is being used " - "without explicit comparison inside a condition to determine if a sequence is empty. " - "Instead of coercing the length to a boolean, either " - "rely on the fact that empty sequences are false or " - "compare the length against a scalar.", + "Empty sequences are considered false in a boolean context. You can either" + " remove the call to 'len' (``if not x``) or compare the length against a" + "scalar (``if len(x) > 1``).", {"old_names": [("C1801", "len-as-condition")]}, ), "C1803": ( - "'%s' can be simplified to '%s' as an empty %s is falsey", + '"%s" can be simplified to "%s", if it is strictly a sequence, as an empty %s is falsey', "use-implicit-booleaness-not-comparison", - "Used when Pylint detects that collection literal comparison is being " - "used to check for emptiness; Use implicit booleaness instead " - "of a collection classes; empty collections are considered as false", + "Empty sequences are considered false in a boolean context. Following this" + " check blindly in weakly typed code base can create hard to debug issues." + " If the value can be something else that is falsey but not a sequence (for" + " example ``None``, an empty string, or ``0``) the code will not be " + "equivalent.", ), "C1804": ( - '"%s" can be simplified to "%s" as an empty string is falsey', + '"%s" can be simplified to "%s", if it is striclty a string, as an empty string is falsey', "use-implicit-booleaness-not-comparison-to-string", - "Used when Pylint detects comparison to an empty string constant.", + "Empty string are considered false in a boolean context. Following this" + " check blindly in weakly typed code base can create hard to debug issues." + " If the value can be something else that is falsey but not a string (for" + " example ``None``, an empty sequence, or ``0``) the code will not be " + "equivalent.", { "default_enabled": False, "old_names": [("C1901", "compare-to-empty-string")], }, ), "C1805": ( - '"%s" can be simplified to "%s" as 0 is falsey', + '"%s" can be simplified to "%s", if it is strictly an int, as 0 is falsey', "use-implicit-booleaness-not-comparison-to-zero", - "Used when Pylint detects comparison to a 0 constant.", + "0 is considered false in a boolean context. Following this" + " check blindly in weakly typed code base can create hard to debug issues." + " If the value can be something else that is falsey but not an int (for" + " example ``None``, an empty string, or an empty sequence) the code will not be " + "equivalent.", {"default_enabled": False, "old_names": [("C2001", "compare-to-zero")]}, ), } options = () + _operators = {"!=", "==", "is not", "is"} @utils.only_required_for_messages("use-implicit-booleaness-not-len") def visit_call(self, node: nodes.Call) -> None: @@ -177,91 +186,77 @@ def visit_unaryop(self, node: nodes.UnaryOp) -> None: "use-implicit-booleaness-not-comparison-to-zero", ) def visit_compare(self, node: nodes.Compare) -> None: - self._check_use_implicit_booleaness_not_comparison(node) - self._check_compare_to_zero(node) - self._check_compare_to_string(node) + if self.linter.is_message_enabled("use-implicit-booleaness-not-comparison"): + self._check_use_implicit_booleaness_not_comparison(node) + if self.linter.is_message_enabled( + "use-implicit-booleaness-not-comparison-to-zero" + ) or self.linter.is_message_enabled( + "use-implicit-booleaness-not-comparison-to-str" + ): + self._check_compare_to_str_or_zero(node) - def _check_compare_to_zero(self, node: nodes.Compare) -> None: - # pylint: disable=duplicate-code - _operators = ["!=", "==", "is not", "is"] + def _check_compare_to_str_or_zero(self, node: nodes.Compare) -> None: # note: astroid.Compare has the left most operand in node.left # while the rest are a list of tuples in node.ops # the format of the tuple is ('compare operator sign', node) # here we squash everything into `ops` to make it easier for processing later - ops: list[tuple[str, nodes.NodeNG]] = [("", node.left)] - ops.extend(node.ops) + ops: list[tuple[str, nodes.NodeNG]] = [("", node.left), *node.ops] iter_ops = iter(ops) all_ops = list(itertools.chain(*iter_ops)) - for ops_idx in range(len(all_ops) - 2): - op_1 = all_ops[ops_idx] op_2 = all_ops[ops_idx + 1] + if op_2 not in self._operators: + continue + op_1 = all_ops[ops_idx] op_3 = all_ops[ops_idx + 2] error_detected = False - - # 0 ?? X - if _is_constant_zero(op_1) and op_2 in _operators: - error_detected = True - op = op_3 - # X ?? 0 - elif op_2 in _operators and _is_constant_zero(op_3): - error_detected = True - op = op_1 - - if error_detected: - original = f"{op_1.as_string()} {op_2} {op_3.as_string()}" - suggestion = ( - op.as_string() - if op_2 in {"!=", "is not"} - else f"not {op.as_string()}" - ) - self.add_message( - "compare-to-zero", - args=(original, suggestion), - node=node, - confidence=HIGH, - ) - - def _check_compare_to_string(self, node: nodes.Compare) -> None: - """Checks for comparisons to empty string. - - Most of the time you should use the fact that empty strings are false. - An exception to this rule is when an empty string value is allowed in the program - and has a different meaning than None! - """ - _operators = {"!=", "==", "is not", "is"} - # note: astroid.Compare has the left most operand in node.left while the rest - # are a list of tuples in node.ops the format of the tuple is - # ('compare operator sign', node) here we squash everything into `ops` - # to make it easier for processing later - ops: list[tuple[str, nodes.NodeNG | None]] = [("", node.left)] - ops.extend(node.ops) - iter_ops = iter(ops) - ops = list(itertools.chain(*iter_ops)) # type: ignore[arg-type] - for ops_idx in range(len(ops) - 2): - op_1: nodes.NodeNG | None = ops[ops_idx] - op_2: str = ops[ops_idx + 1] # type: ignore[assignment] - op_3: nodes.NodeNG | None = ops[ops_idx + 2] - error_detected = False - if op_1 is None or op_3 is None or op_2 not in _operators: - continue - node_name = "" - # x ?? "" - if utils.is_empty_str_literal(op_1): - error_detected = True - node_name = op_3.as_string() - # '' ?? X - elif utils.is_empty_str_literal(op_3): - error_detected = True - node_name = op_1.as_string() - if error_detected: - suggestion = f"not {node_name}" if op_2 in {"==", "is"} else node_name - self.add_message( - "compare-to-empty-string", - args=(node.as_string(), suggestion), - node=node, - confidence=HIGH, - ) + if self.linter.is_message_enabled( + "use-implicit-booleaness-not-comparison-to-zero" + ): + # 0 ?? X + if _is_constant_zero(op_1): + error_detected = True + op = op_3 + # X ?? 0 + elif _is_constant_zero(op_3): + error_detected = True + op = op_1 + if error_detected: + original = f"{op_1.as_string()} {op_2} {op_3.as_string()}" + suggestion = ( + op.as_string() + if op_2 in {"!=", "is not"} + else f"not {op.as_string()}" + ) + self.add_message( + "use-implicit-booleaness-not-comparison-to-zero", + args=(original, suggestion), + node=node, + confidence=HIGH, + ) + error_detected = False + if self.linter.is_message_enabled( + "use-implicit-booleaness-not-comparison-to-str" + ): + node_name = "" + # x ?? "" + if utils.is_empty_str_literal(op_1): + error_detected = True + node_name = op_3.as_string() + # '' ?? X + elif utils.is_empty_str_literal(op_3): + error_detected = True + node_name = op_1.as_string() + if error_detected: + suggestion = ( + f"not {node_name}" if op_2 in {"==", "is"} else node_name + ) + self.add_message( + "use-implicit-booleaness-not-comparison-to-string", + args=(node.as_string(), suggestion), + node=node, + confidence=HIGH, + ) def _check_use_implicit_booleaness_not_comparison( self, node: nodes.Compare diff --git a/tests/functional/u/use/use_implicit_booleaness_not_comparison.txt b/tests/functional/u/use/use_implicit_booleaness_not_comparison.txt index 2ace15d7e2..f4cf777472 100644 --- a/tests/functional/u/use/use_implicit_booleaness_not_comparison.txt +++ b/tests/functional/u/use/use_implicit_booleaness_not_comparison.txt @@ -1,32 +1,32 @@ -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 -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 -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 -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 -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 -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 -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 -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 -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 -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 -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 -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 -use-implicit-booleaness-not-comparison:54:7:54:23::'empty_dict != {}' can be simplified to 'empty_dict' as an empty dict is falsey:HIGH -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 -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 -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 -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 -use-implicit-booleaness-not-comparison:83:3:83:10::'a == []' can be simplified to 'not a' as an empty list is falsey:HIGH -use-implicit-booleaness-not-comparison:95:3:95:10::'e == []' can be simplified to 'not e' as an empty list is falsey:HIGH -use-implicit-booleaness-not-comparison:95:15:95:22::'f == {}' can be simplified to 'not f' as an empty dict is falsey:HIGH -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 -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 -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 -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 -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 -use-implicit-booleaness-not-comparison:158:3:158:20::'numpy_array != []' can be simplified to 'numpy_array' as an empty list is falsey:HIGH -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 -use-implicit-booleaness-not-comparison:185:3:185:13::'data == {}' can be simplified to 'not data' as an empty dict is falsey:HIGH -use-implicit-booleaness-not-comparison:187:3:187:13::'data != {}' can be simplified to 'data' as an empty dict is falsey:HIGH -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 -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 -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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 diff --git a/tests/functional/u/use/use_implicit_booleaness_not_comparison_to_string.txt b/tests/functional/u/use/use_implicit_booleaness_not_comparison_to_string.txt index c1f31f27ff..5f07a683c6 100644 --- a/tests/functional/u/use/use_implicit_booleaness_not_comparison_to_string.txt +++ b/tests/functional/u/use/use_implicit_booleaness_not_comparison_to_string.txt @@ -1,6 +1,6 @@ -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 -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 -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 -use-implicit-booleaness-not-comparison-to-string:15:3:15:10::"""Y != ''"" can be simplified to ""Y"" as an empty string is falsey":HIGH -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 -use-implicit-booleaness-not-comparison-to-string:21:3:21:10::"""'' != X"" can be simplified to ""X"" as an empty string is falsey":HIGH +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 +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 +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 +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 +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 +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 diff --git a/tests/functional/u/use/use_implicit_booleaness_not_comparison_to_zero.txt b/tests/functional/u/use/use_implicit_booleaness_not_comparison_to_zero.txt index 25702682a2..cb7d57699e 100644 --- a/tests/functional/u/use/use_implicit_booleaness_not_comparison_to_zero.txt +++ b/tests/functional/u/use/use_implicit_booleaness_not_comparison_to_zero.txt @@ -1,6 +1,6 @@ -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 -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 -use-implicit-booleaness-not-comparison-to-zero:18:3:18:9::"""X == 0"" can be simplified to ""not X"" as 0 is falsey":HIGH -use-implicit-booleaness-not-comparison-to-zero:24:3:24:9::"""0 == Y"" can be simplified to ""not Y"" as 0 is falsey":HIGH -use-implicit-booleaness-not-comparison-to-zero:27:3:27:9::"""Y != 0"" can be simplified to ""Y"" as 0 is falsey":HIGH -use-implicit-booleaness-not-comparison-to-zero:30:3:30:9::"""0 != X"" can be simplified to ""X"" as 0 is falsey":HIGH +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 +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 +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 +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 +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 +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 diff --git a/tests/functional/u/use/use_implicit_booleaness_not_len.py b/tests/functional/u/use/use_implicit_booleaness_not_len.py index eb0679918b..79547d99e1 100644 --- a/tests/functional/u/use/use_implicit_booleaness_not_len.py +++ b/tests/functional/u/use/use_implicit_booleaness_not_len.py @@ -158,23 +158,23 @@ def function_returning_int(r): return 1 return 2 - # def function_returning_generator(r): - # for i in [r, 1, 2, 3]: - # yield i + def function_returning_generator(r): + for i in [r, 1, 2, 3]: + yield i - # def function_returning_comprehension(r): - # return [x+1 for x in [r, 1, 2, 3]] + def function_returning_comprehension(r): + return [x+1 for x in [r, 1, 2, 3]] - # def function_returning_function(r): - # return function_returning_generator(r) + def function_returning_function(r): + return function_returning_generator(r) assert len(function_returning_list(z)) # [use-implicit-booleaness-not-len] assert len(function_returning_int(z)) # This should raise a use-implicit-booleaness-not-len once astroid can infer it # See https://github.com/pylint-dev/pylint/pull/3821#issuecomment-743771514 - # assert len(function_returning_generator(z)) - # assert len(function_returning_comprehension(z)) - # assert len(function_returning_function(z)) + assert len(function_returning_generator(z)) + assert len(function_returning_comprehension(z)) + assert len(function_returning_function(z)) def github_issue_4215():