From 09c3ee99eebb403ed4f3b8cb0ef20ce02ee34d07 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Mon, 31 Oct 2022 16:56:37 -0300 Subject: [PATCH] use inference confidence when appropriate --- .../refactoring/refactoring_checker.py | 39 ++++++++++++------- .../unnecessary_list_index_lookup.txt | 4 +- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index 5b29b09b87d..82ac8ca697d 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -21,7 +21,7 @@ from pylint import checkers from pylint.checkers import utils from pylint.checkers.utils import node_frame_class -from pylint.interfaces import HIGH, INFERENCE +from pylint.interfaces import HIGH, INFERENCE, Confidence if TYPE_CHECKING: from pylint.lint import PyLinter @@ -2108,9 +2108,10 @@ def _check_unnecessary_list_index_lookup( # destructured, so we can't necessarily use it. return - if self._enumerate_with_start(node): + has_start_arg, confidence = self._enumerate_with_start(node) + if has_start_arg: # enumerate is being called with start arg/kwarg so resulting index lookup - # is not redundant so we should not report an error. + # is not redundant, hence we should not report an error. return iterating_object_name = node.iter.args[0].name @@ -2194,7 +2195,7 @@ def _check_unnecessary_list_index_lookup( "unnecessary-list-index-lookup", node=subscript, args=(node.target.elts[1].name,), - confidence=HIGH, + confidence=confidence, ) for subscript in bad_nodes: @@ -2202,10 +2203,12 @@ def _check_unnecessary_list_index_lookup( "unnecessary-list-index-lookup", node=subscript, args=(node.target.elts[1].name,), - confidence=HIGH, + confidence=confidence, ) - def _enumerate_with_start(self, node: nodes.For | nodes.Comprehension) -> bool: + def _enumerate_with_start( + self, node: nodes.For | nodes.Comprehension + ) -> tuple[bool, Confidence]: """Check presence of `start` kwarg or second argument to enumerate. For example: @@ -2216,31 +2219,37 @@ def _enumerate_with_start(self, node: nodes.For | nodes.Comprehension) -> bool: If `start` is assigned to `0`, the default value, this is equivalent to not calling `enumerate` with start. """ + confidence = HIGH + if len(node.iter.args) > 1: # We assume the second argument to `enumerate` is the `start` int arg. # It's a reasonable assumption for now as it's the only possible argument: # https://docs.python.org/3/library/functions.html#enumerate start_arg = node.iter.args[1] - start_val = self._get_start_value(start_arg) + start_val, confidence = self._get_start_value(start_arg) if start_val is None: - return False - return not start_val == 0 + return False, confidence + return not start_val == 0, confidence for keyword in node.iter.keywords: if keyword.arg == "start": - start_val = self._get_start_value(keyword.value) + start_val, confidence = self._get_start_value(keyword.value) if start_val is None: - return False - return not start_val == 0 + return False, confidence + return not start_val == 0, confidence - return False + return False, confidence + + def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]: + confidence = HIGH - def _get_start_value(self, node: nodes.NodeNG) -> Any: if isinstance(node, nodes.Name): inferred = utils.safe_infer(node) start_val = inferred.value if inferred else None + confidence = INFERENCE elif isinstance(node, nodes.UnaryOp): start_val = node.operand.value else: start_val = node.value - return start_val + + return start_val, confidence diff --git a/tests/functional/u/unnecessary/unnecessary_list_index_lookup.txt b/tests/functional/u/unnecessary/unnecessary_list_index_lookup.txt index 74c6605f305..a38788cc895 100644 --- a/tests/functional/u/unnecessary/unnecessary_list_index_lookup.txt +++ b/tests/functional/u/unnecessary/unnecessary_list_index_lookup.txt @@ -4,5 +4,5 @@ unnecessary-list-index-lookup:46:10:46:22::Unnecessary list index lookup, use 'v unnecessary-list-index-lookup:74:10:74:27::Unnecessary list index lookup, use 'val' instead:HIGH unnecessary-list-index-lookup:112:10:112:21::Unnecessary list index lookup, use 'val' instead:HIGH unnecessary-list-index-lookup:115:10:115:21::Unnecessary list index lookup, use 'val' instead:HIGH -unnecessary-list-index-lookup:119:10:119:21::Unnecessary list index lookup, use 'val' instead:HIGH -unnecessary-list-index-lookup:122:10:122:21::Unnecessary list index lookup, use 'val' instead:HIGH +unnecessary-list-index-lookup:119:10:119:21::Unnecessary list index lookup, use 'val' instead:INFERENCE +unnecessary-list-index-lookup:122:10:122:21::Unnecessary list index lookup, use 'val' instead:INFERENCE