Skip to content

Commit

Permalink
use inference confidence when appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna authored and Pierre-Sassoulas committed Oct 31, 2022
1 parent 43243cc commit 09c3ee9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
39 changes: 24 additions & 15 deletions pylint/checkers/refactoring/refactoring_checker.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2194,18 +2195,20 @@ 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:
self.add_message(
"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:
Expand All @@ -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
Expand Up @@ -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

0 comments on commit 09c3ee9

Please sign in to comment.