Skip to content

Commit 09c3ee9

Browse files
clavedelunaPierre-Sassoulas
authored andcommitted
use inference confidence when appropriate
1 parent 43243cc commit 09c3ee9

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pylint import checkers
2222
from pylint.checkers import utils
2323
from pylint.checkers.utils import node_frame_class
24-
from pylint.interfaces import HIGH, INFERENCE
24+
from pylint.interfaces import HIGH, INFERENCE, Confidence
2525

2626
if TYPE_CHECKING:
2727
from pylint.lint import PyLinter
@@ -2108,9 +2108,10 @@ def _check_unnecessary_list_index_lookup(
21082108
# destructured, so we can't necessarily use it.
21092109
return
21102110

2111-
if self._enumerate_with_start(node):
2111+
has_start_arg, confidence = self._enumerate_with_start(node)
2112+
if has_start_arg:
21122113
# enumerate is being called with start arg/kwarg so resulting index lookup
2113-
# is not redundant so we should not report an error.
2114+
# is not redundant, hence we should not report an error.
21142115
return
21152116

21162117
iterating_object_name = node.iter.args[0].name
@@ -2194,18 +2195,20 @@ def _check_unnecessary_list_index_lookup(
21942195
"unnecessary-list-index-lookup",
21952196
node=subscript,
21962197
args=(node.target.elts[1].name,),
2197-
confidence=HIGH,
2198+
confidence=confidence,
21982199
)
21992200

22002201
for subscript in bad_nodes:
22012202
self.add_message(
22022203
"unnecessary-list-index-lookup",
22032204
node=subscript,
22042205
args=(node.target.elts[1].name,),
2205-
confidence=HIGH,
2206+
confidence=confidence,
22062207
)
22072208

2208-
def _enumerate_with_start(self, node: nodes.For | nodes.Comprehension) -> bool:
2209+
def _enumerate_with_start(
2210+
self, node: nodes.For | nodes.Comprehension
2211+
) -> tuple[bool, Confidence]:
22092212
"""Check presence of `start` kwarg or second argument to enumerate.
22102213
22112214
For example:
@@ -2216,31 +2219,37 @@ def _enumerate_with_start(self, node: nodes.For | nodes.Comprehension) -> bool:
22162219
If `start` is assigned to `0`, the default value, this is equivalent to
22172220
not calling `enumerate` with start.
22182221
"""
2222+
confidence = HIGH
2223+
22192224
if len(node.iter.args) > 1:
22202225
# We assume the second argument to `enumerate` is the `start` int arg.
22212226
# It's a reasonable assumption for now as it's the only possible argument:
22222227
# https://docs.python.org/3/library/functions.html#enumerate
22232228
start_arg = node.iter.args[1]
2224-
start_val = self._get_start_value(start_arg)
2229+
start_val, confidence = self._get_start_value(start_arg)
22252230
if start_val is None:
2226-
return False
2227-
return not start_val == 0
2231+
return False, confidence
2232+
return not start_val == 0, confidence
22282233

22292234
for keyword in node.iter.keywords:
22302235
if keyword.arg == "start":
2231-
start_val = self._get_start_value(keyword.value)
2236+
start_val, confidence = self._get_start_value(keyword.value)
22322237
if start_val is None:
2233-
return False
2234-
return not start_val == 0
2238+
return False, confidence
2239+
return not start_val == 0, confidence
22352240

2236-
return False
2241+
return False, confidence
2242+
2243+
def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]:
2244+
confidence = HIGH
22372245

2238-
def _get_start_value(self, node: nodes.NodeNG) -> Any:
22392246
if isinstance(node, nodes.Name):
22402247
inferred = utils.safe_infer(node)
22412248
start_val = inferred.value if inferred else None
2249+
confidence = INFERENCE
22422250
elif isinstance(node, nodes.UnaryOp):
22432251
start_val = node.operand.value
22442252
else:
22452253
start_val = node.value
2246-
return start_val
2254+
2255+
return start_val, confidence

tests/functional/u/unnecessary/unnecessary_list_index_lookup.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ unnecessary-list-index-lookup:46:10:46:22::Unnecessary list index lookup, use 'v
44
unnecessary-list-index-lookup:74:10:74:27::Unnecessary list index lookup, use 'val' instead:HIGH
55
unnecessary-list-index-lookup:112:10:112:21::Unnecessary list index lookup, use 'val' instead:HIGH
66
unnecessary-list-index-lookup:115:10:115:21::Unnecessary list index lookup, use 'val' instead:HIGH
7-
unnecessary-list-index-lookup:119:10:119:21::Unnecessary list index lookup, use 'val' instead:HIGH
8-
unnecessary-list-index-lookup:122:10:122:21::Unnecessary list index lookup, use 'val' instead:HIGH
7+
unnecessary-list-index-lookup:119:10:119:21::Unnecessary list index lookup, use 'val' instead:INFERENCE
8+
unnecessary-list-index-lookup:122:10:122:21::Unnecessary list index lookup, use 'val' instead:INFERENCE

0 commit comments

Comments
 (0)