Skip to content

Conversation

@timmartin
Copy link
Contributor

Type of Changes

Type
πŸ› Bug fix
βœ“ ✨ New feature
πŸ”¨ Refactoring
πŸ“œ Docs

Description

Added a new checker for catching the case of iterating over a list using enumerate():

for index, value in enumerate(mylist):
    print(mylist[index]) # Bad, should use value instead

This will now report

Unnecessary list index lookup, use 'value' instead

Closes #4525

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.14.0 milestone Feb 25, 2022
@timmartin timmartin force-pushed the issue-4525-unnecessary-list-lookup branch from 8fc9d8c to 7e626dd Compare February 27, 2022 08:48
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening the pull request, this look goods already. I think we'll be able to add this to 2.14, but we need to release 2.13 at some point and we're lagging behind all the contributions πŸ˜„. Could you move the changelog to 2.14 ? I know the "what's new" page for 2.14 is not created yet, sorry.

@timmartin timmartin force-pushed the issue-4525-unnecessary-list-lookup branch 4 times, most recently from 9502b59 to c7c0c51 Compare March 2, 2022 18:28
@coveralls
Copy link

coveralls commented Mar 2, 2022

Pull Request Test Coverage Report for Build 2042126251

  • 27 of 27 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.009%) to 94.184%

Totals Coverage Status
Change from base Build 2040724854: 0.009%
Covered Lines: 15337
Relevant Lines: 16284

πŸ’› - Coveralls

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very good already!

I left some comments, let me know if you have any questions about them πŸ˜„

@timmartin timmartin force-pushed the issue-4525-unnecessary-list-lookup branch 2 times, most recently from dd8f358 to 7076fe0 Compare March 4, 2022 22:42
# enumerate() result is being assigned without destructuring
return

iterating_object_name = node.iter.args[0].name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if there are edge cases to consider with len(args) > 1, but I think in those cases using the index is so unnecessarily complicated that most people will have moved to using value at that point. So I think we can leave those edge cases unconsidered here.

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @timmartin, this looks good to me!

@Pierre-Sassoulas Depending on your schedule and free time, we might actually get this reviewed and merged before we release 2.11. No rush though!

@Pierre-Sassoulas Pierre-Sassoulas added Checkers Related to a checker Enhancement ✨ Improvement to a component labels Mar 6, 2022
@DanielNoord
Copy link
Collaborator

Hi @timmartin,

I know I already approved this PR, but...
We have been working on the documentation of the messages that pylint emits. As this PR adds a new message could I ask you to rebase on main and add two examples for bad and good code like explained in #5953? That way we immediately help users understand what this new message will do πŸ˜„

@timmartin timmartin force-pushed the issue-4525-unnecessary-list-lookup branch from 43dce52 to 0f794ae Compare March 25, 2022 20:30
@timmartin
Copy link
Contributor Author

I've rebased and added documentation now.

@timmartin timmartin force-pushed the issue-4525-unnecessary-list-lookup branch from 4fdb3ba to 207060d Compare March 25, 2022 20:32
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the two examples, looks great !

@Pierre-Sassoulas Pierre-Sassoulas merged commit 8d21ab2 into pylint-dev:main Mar 25, 2022
@timmartin timmartin deleted the issue-4525-unnecessary-list-lookup branch April 27, 2022 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Checkers Related to a checker Enhancement ✨ Improvement to a component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New checker unnecessary-list-index-lookup

4 participants