Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
The-Compiler opened this issue Jun 8, 2022 · 2 comments · Fixed by #7334
Closed
Assignees
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code

Comments

@The-Compiler
Copy link
Contributor

The-Compiler commented Jun 8, 2022

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

@The-Compiler The-Compiler added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jun 8, 2022
@The-Compiler
Copy link
Contributor Author

Also see #6818, though this seems subtly different. Note that #6845 doesn't seem to fix this.

@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jun 9, 2022
@Pierre-Sassoulas
Copy link
Member

Thank you for opening the issue and providing the relevant doc I think this is indeed a false positive (even if I personally would not modify iterable while iterating them to make the code as unsurprising as possible).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants