Skip to content

unnecessary-list-index-lookup false-positive when iterable is modified in loop #6896

Closed
@The-Compiler

Description

@The-Compiler

Bug description

With some code like this:

# pylint: disable=missing-module-docstring

parts = ["a", "b", "c", "d"]
for i, part in enumerate(parts):
    if i == 3:  # more complex condition actually
        parts.insert(i, "X")
    print(part, parts[i])

(real-world example), pylint claims that parts[i] could be replaced by part - but as running the file shows, that isn't actually the case:

a a
b b
c c
d X
d d

You might think that this is a bit unorthodox (and I might agree, should probably rewrite this...), but if I'm reading the Python docs right, the behavior is well-defined (emphasis mine):

There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop.

cc @timmartin who added the checker in #5834.

Configuration

No response

Command used

pylint x.py

Pylint output

************* Module x
x.py:7:16: R1736: Unnecessary list index lookup, use 'part' instead (unnecessary-list-index-lookup)

Expected behavior

No error

Pylint version

pylint 2.14.1
astroid 2.11.5
Python 3.10.5 (main, Jun  6 2022, 18:49:26) [GCC 12.1.0]

OS / Environment

Archlinux

Additional dependencies

No response

Metadata

Metadata

Assignees

Labels

False Positive 🦟A message is emitted but nothing is wrong with the code

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions