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 is falsely being reported when there is a start index #7682

Closed
AviSarmani opened this issue Oct 27, 2022 · 3 comments · Fixed by #7685
Closed
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@AviSarmani
Copy link

AviSarmani commented Oct 27, 2022

Bug description

The code is:

series = [1, 2, 3, 4, 5]
output_list = [
    (item, series[index])
    for index, item in enumerate(series, start=1)
    if index < len(series)
]
print(output_list)

Which produces the output:
[(1, 2), (2, 3), (3, 4), (4, 5)]

When there is a start parameter on the enumeration, "item" and "series[index]" are not the same object
but pylint suggests:
R1736: Unnecessary list index lookup, use 'item' instead (unnecessary-list-index-lookup)
If we follow the advice we would get a different result set
[(1, 1), (2, 2), (3, 3), (4, 4)]

I think the error should be suppressed when there's a start index on the enumeration

Configuration

No response

Command used

pylint .\pylint.py

Pylint output

************* Module pylint
pylint.py:4:11: R1736: Unnecessary list index lookup, use 'item' instead (unnecessary-list-index-lookup)

Expected behavior

No error at all

Pylint version

pylint 2.14.1
astroid 2.11.5
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)]

OS / Environment

No response

Additional dependencies

No response

@AviSarmani AviSarmani added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Oct 27, 2022
@Pierre-Sassoulas
Copy link
Member

Thank you for opening the issue, I can reproduce that on the latest main.

@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Oct 27, 2022
@clavedeluna
Copy link
Collaborator

I can work on a PR to handle this edge case. Probably adding an if statement to _check_unnecessary_list_index_lookup to determine if the list comp's iterating function is enumerate call with a second arg or a keyword arg start=

@clavedeluna
Copy link
Collaborator

Also really similar to https://github.com/PyCQA/pylint/pull/7334/files so looks like this check is often getting false positives

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 Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants