diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2023-05-02 21:20:28 +0200 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2023-05-03 22:01:26 +0200 |
commit | c8123d0ac847d79f3f8fcb53b8c5f3578a0bbd9d (patch) | |
tree | 10a2571524aad1533040c37d5425883eaebd1619 | |
parent | ca74a1dbdabc138559d267e3adfef3893a1e795a (diff) | |
download | pylint-git-c8123d0ac847d79f3f8fcb53b8c5f3578a0bbd9d.tar.gz |
Uniformize message and remove useless details.rst
6 files changed, 37 insertions, 36 deletions
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 c5781b739..000000000 --- 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 2f86acf54..000000000 --- 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 0dc180245..000000000 --- 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 ec6d11c2d..eb716d389 100644 --- a/doc/user_guide/checkers/features.rst +++ b/doc/user_guide/checkers/features.rst @@ -894,13 +894,20 @@ Refactoring checker Messages of function or method definition. This statement can safely be removed because Python will implicitly return None :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* - 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. :use-implicit-booleaness-not-comparison (C1803): *"%s" can be simplified to "%s", if it is strictly a sequence, 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 + 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* - 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. :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 eace82f7a..7cabc1ca1 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 d3ef542a1..ad072e3f8 100644 --- a/pylint/checkers/refactoring/implicit_booleaness_checker.py +++ b/pylint/checkers/refactoring/implicit_booleaness_checker.py @@ -66,24 +66,28 @@ 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", 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", 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")], @@ -92,7 +96,11 @@ class ImplicitBooleanessChecker(checkers.BaseChecker): "C1805": ( '"%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")]}, ), } @@ -223,12 +231,6 @@ class ImplicitBooleanessChecker(checkers.BaseChecker): ) 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 @@ -257,7 +259,7 @@ class ImplicitBooleanessChecker(checkers.BaseChecker): if error_detected: suggestion = f"not {node_name}" if op_2 in {"==", "is"} else node_name self.add_message( - "compare-to-empty-string", + "use-implicit-booleaness-not-comparison-to-string", args=(node.as_string(), suggestion), node=node, confidence=HIGH, |