[pylint] Avoid false positives in else clause (PLR1733)#25177
Merged
ntBre merged 2 commits intoMay 15, 2026
Conversation
The for-loop's else clause has different control-flow semantics than the loop body: it executes only when the loop completes without break, and iteration variables from nested inner loops may not be in scope or may have stale values. This was causing a false positive where PLR1733 suggested replacing result[res_glob] with res_priority inside a for-else, but res_priority may be unbound if the inner loop never ran, making the suggested fix incorrect and potentially introducing a NameError. Fix by removing the visit_body(&stmt_for.orelse) call in unnecessary_dict_index_lookup(). The else clause of a for loop should not be treated as part of the iteration context. Fixes astral-sh#25150.
|
ntBre
approved these changes
May 15, 2026
Contributor
ntBre
left a comment
There was a problem hiding this comment.
Thanks! I pushed one commit trimming down some of the comments in the tests, but this makes sense to me.
Co-authored-by: Brent Westbrook <[email protected]>
pylint] Avoid false positives in else clause (PLR1733)
3 tasks
thejchap
pushed a commit
to thejchap/ruff
that referenced
this pull request
May 23, 2026
…-sh#25177) ## Summary The `PLR1733` (unnecessary-dict-index-lookup) rule was incorrectly firing on dictionary accesses inside the `else` clause of a `for` loop. The `else` clause has different control-flow semantics — it only executes when the loop completes without `break` — and iteration variables from inner loops may not be in scope or may have stale values. ## Root cause In `unnecessary_dict_index_lookup()`, the `stmt_for.orelse` was being visited along with `stmt_for.body`: ```rust visitor.visit_body(&stmt_for.body); visitor.visit_body(&stmt_for.orelse); // <-- this was the bug ``` The `else` clause of a `for` loop is not part of the iteration context. Variables bound in the outer loop may be unbound if the loop body never ran, or stale from the last iteration if the loop completed. ## Fix Remove the `visitor.visit_body(&stmt_for.orelse)` call so the `else` clause is not checked for unnecessary dict lookups. ## Validation ```bash # False positive case (now correctly passes) cargo run -p ruff -- check test.py --isolated --select PLR1733 --preview # Before: 1 error (PLR1733 false positive) # After: All checks passed # Existing cases still work cargo test -p ruff_linter unnecessary_dict_index # PASS # Zulip pattern is still detected correctly cargo run -p ruff -- check test_zulip.py --isolated --select PLR1733 --preview # Correctly detects: mapped_arrays[mapped_label][i] += value_arrays[label][i] ``` ## Before `ruff check` would report a false positive on `result[res_glob]` inside the `else` clause of the outer `for` loop, and the autofix would suggest `res_priority` which is incorrect (the inner loop variable may be unbound/stale). ## After `ruff check` correctly skips the `else` clause of the `for` loop — no false positive. Fixes astral-sh#25150 --------- Co-authored-by: Aniket Karne <[email protected]> Co-authored-by: Brent Westbrook <[email protected]>
anishgirianish
pushed a commit
to anishgirianish/ruff
that referenced
this pull request
May 28, 2026
…-sh#25177) ## Summary The `PLR1733` (unnecessary-dict-index-lookup) rule was incorrectly firing on dictionary accesses inside the `else` clause of a `for` loop. The `else` clause has different control-flow semantics — it only executes when the loop completes without `break` — and iteration variables from inner loops may not be in scope or may have stale values. ## Root cause In `unnecessary_dict_index_lookup()`, the `stmt_for.orelse` was being visited along with `stmt_for.body`: ```rust visitor.visit_body(&stmt_for.body); visitor.visit_body(&stmt_for.orelse); // <-- this was the bug ``` The `else` clause of a `for` loop is not part of the iteration context. Variables bound in the outer loop may be unbound if the loop body never ran, or stale from the last iteration if the loop completed. ## Fix Remove the `visitor.visit_body(&stmt_for.orelse)` call so the `else` clause is not checked for unnecessary dict lookups. ## Validation ```bash # False positive case (now correctly passes) cargo run -p ruff -- check test.py --isolated --select PLR1733 --preview # Before: 1 error (PLR1733 false positive) # After: All checks passed # Existing cases still work cargo test -p ruff_linter unnecessary_dict_index # PASS # Zulip pattern is still detected correctly cargo run -p ruff -- check test_zulip.py --isolated --select PLR1733 --preview # Correctly detects: mapped_arrays[mapped_label][i] += value_arrays[label][i] ``` ## Before `ruff check` would report a false positive on `result[res_glob]` inside the `else` clause of the outer `for` loop, and the autofix would suggest `res_priority` which is incorrect (the inner loop variable may be unbound/stale). ## After `ruff check` correctly skips the `else` clause of the `for` loop — no false positive. Fixes astral-sh#25150 --------- Co-authored-by: Aniket Karne <[email protected]> Co-authored-by: Brent Westbrook <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
PLR1733(unnecessary-dict-index-lookup) rule was incorrectly firing on dictionary accesses inside theelseclause of aforloop. Theelseclause has different control-flow semantics — it only executes when the loop completes withoutbreak— and iteration variables from inner loops may not be in scope or may have stale values.Root cause
In
unnecessary_dict_index_lookup(), thestmt_for.orelsewas being visited along withstmt_for.body:The
elseclause of aforloop is not part of the iteration context. Variables bound in the outer loop may be unbound if the loop body never ran, or stale from the last iteration if the loop completed.Fix
Remove the
visitor.visit_body(&stmt_for.orelse)call so theelseclause is not checked for unnecessary dict lookups.Validation
Before
ruff checkwould report a false positive onresult[res_glob]inside theelseclause of the outerforloop, and the autofix would suggestres_prioritywhich is incorrect (the inner loop variable may be unbound/stale).After
ruff checkcorrectly skips theelseclause of theforloop — no false positive.Fixes #25150