[perflint] Handle tuples in dictionary comprehensions (PERF403)#19934
Merged
Conversation
4ef1ddb to
dce411b
Compare
Contributor
Author
|
@ntBre |
Contributor
|
Thank you! I'll try to get to it this week :) |
Contributor
|
ntBre
reviewed
Aug 19, 2025
Contributor
ntBre
left a comment
There was a problem hiding this comment.
Thank you, this looks great! I just had a few small suggestions.
da2587d to
63f7ca4
Compare
Contributor
Author
|
@ntBre Thanks again! |
perflint] Handle tuples in dictionary comprehensions (PERF403)
second-ed
pushed a commit
to second-ed/ruff
that referenced
this pull request
Sep 9, 2025
…stral-sh#19934) This pull request fixes the bug described in issue [astral-sh#19153](astral-sh#19153). The issue occurred when `PERF403` incorrectly flagged cases involving tuple unpacking in a for loop. For example: ```python def f(): v = {} for (o, p), x in [("op", "x")]: v[x] = o, p ``` This code was wrongly suggested to be rewritten into a dictionary comprehension, which changes the semantics. Changes in this PR: Updated the `PERF403` rule to correctly handle tuple unpacking in loop targets. Added regression tests to ensure this case (and similar ones) are no longer flagged incorrectly. Why: This ensures that `PERF403` only triggers when a dictionary comprehension is semantically equivalent to the original loop, preventing false positives. --------- Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
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.
This pull request fixes the bug described in issue #19153.
The issue occurred when
PERF403incorrectly flagged cases involving tuple unpacking in a for loop. For example:This code was wrongly suggested to be rewritten into a dictionary comprehension, which changes the semantics.
Changes in this PR:
Updated the
PERF403rule to correctly handle tuple unpacking in loop targets.Added regression tests to ensure this case (and similar ones) are no longer flagged incorrectly.
Why:
This ensures that
PERF403only triggers when a dictionary comprehension is semantically equivalent to the original loop, preventing false positives.