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

[pylint] - add unnecessary-list-index-lookup (PLR1736) + autofix #7999

Merged
merged 20 commits into from
Nov 30, 2023

Conversation

diceroll123
Copy link
Contributor

@diceroll123 diceroll123 commented Oct 17, 2023

Summary

Add R1736 along with the autofix

See #970

Test Plan

cargo test and manually

@diceroll123 diceroll123 force-pushed the add-R1736 branch 2 times, most recently from 592f4e1 to 7cff2fb Compare October 17, 2023 02:17
@github-actions
Copy link
Contributor

github-actions bot commented Oct 17, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

@diceroll123 diceroll123 force-pushed the add-R1736 branch 2 times, most recently from d5422ed to 551ad16 Compare October 17, 2023 16:55
@diceroll123 diceroll123 changed the title [pylint] add unnecessary-list-index-lookup PLR1736 with autofix [pylint] - add unnecessary-list-index-lookup (PLR1736) + autofix Oct 18, 2023
@diceroll123 diceroll123 force-pushed the add-R1736 branch 5 times, most recently from 5f77a1d to f2d1f1e Compare October 22, 2023 22:11
@Skylion007
Copy link
Contributor

Skylion007 commented Nov 4, 2023

@diceroll123 I tried out your rule and found a bug:

Consider this code snippit

a = list(range(5, 10))
for i, j in enumerate(a):
    a[i] = j + 1
    assert a[i] > j

becomes transformed to:

a = list(range(5, 10))
for i, j in enumerate(a):
    a[i] = j + 1
    assert j > j

which now outputs an Assertion Error. In other words, you have to make sure a[i] is always an alias of j which is not the case here. I suspect your other PR has a similar issue.

You should also test mutating j and changing the alias there.

ie:

j = j+1
assert a[i] != j

@diceroll123
Copy link
Contributor Author

diceroll123 commented Nov 4, 2023

@Skylion007 good catch, thanks! Will update.

I think the safest and sanest way forward is to stop emitting once the visitor finds a subscript assignment to the currently-looped value.

Copy link
Contributor

github-actions bot commented Nov 4, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+3 -0 violations, +0 -0 fixes in 41 projects)

apache/airflow (+3 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview --select ALL

+ dev/breeze/src/airflow_breeze/utils/parallel.py:297:57: PLR1736 [*] Unnecessary lookup of list item by index
+ dev/breeze/src/airflow_breeze/utils/parallel.py:299:40: PLR1736 [*] Unnecessary lookup of list item by index
+ scripts/ci/pre_commit/pre_commit_check_provider_airflow_compatibility.py:51:20: PLR1736 [*] Unnecessary lookup of list item by index

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLR1736 3 3 0 0 0

Copy link
Member

@zanieb zanieb left a comment

Choose a reason for hiding this comment

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

I think @charliermarsh is better suited to review the implementation, but I read through everything as best as I could :)

The ecosystem changes look good.

Comment on lines 216 to 222
// Check that the function is the `enumerate` builtin.
let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() else {
return None;
};
if id != "enumerate" {
return None;
};
Copy link
Member

Choose a reason for hiding this comment

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

Should this use checker.semantic().resolve_call_path(func)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good change, I've also added detection for builtins.enumerate along with it, thanks!

Comment on lines 15 to 19
def dont_fix_these():
# once there is an assignment to the sequence[index], we stop emitting diagnostics
for index, letter in enumerate(letters):
letters[index] = "d" # Ok
assert letters[index] == "d" # Ok
Copy link
Member

Choose a reason for hiding this comment

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

What about a case where index is mutated? e.g. index += 1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding a case for that, as well as the sequence itself being mutated! Thanks!

Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

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

Overall this is pretty good. Thanks for the thorough test cases. I just have a few nits but those shouldn't be a blocker to merge this PR.

@@ -0,0 +1,59 @@
import builtins
Copy link
Member

Choose a reason for hiding this comment

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

These tests are really thorough! Thanks for thinking through them.

@diceroll123
Copy link
Contributor Author

Will address those, all good feedback, thanks @dhruvmanila!

@zanieb zanieb merged commit 70febb1 into astral-sh:main Nov 30, 2023
16 checks passed
@zanieb zanieb added rule Implementing or modifying a lint rule preview Related to preview mode features labels Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants