2121from pylint import checkers
2222from pylint .checkers import utils
2323from pylint .checkers .utils import node_frame_class
24- from pylint .interfaces import HIGH , INFERENCE
24+ from pylint .interfaces import HIGH , INFERENCE , Confidence
2525
2626if 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
0 commit comments