-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[perflint
] Implement fix for manual-dict-comprehension
(PERF403
)
#16719
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
Conversation
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
PERF403 | 12 | 6 | 6 | 0 | 0 |
RUF043 | 2 | 0 | 2 | 0 | 0 |
Slight nit: had a situation where it didn't simplify an node_to_step: dict[BaseSchedulerNode, int] = dict()
for step, node in enumerate(nodes):
node_to_step[node] = step got transformed to node_to_step: dict[BaseSchedulerNode, int] = dict()
node_to_step.update(...) |
I had forgotten about using a function call to make a dict, since it's not typically something I do in my own code. I've now modified the "is empty dict" check to allow builtin function calls for both PERF403 and PERF401. |
merge with main
Title is wrong, it's PERF403, right? |
Do you know why the tests aren't running? The test jobs in my fork of the repo are timing out while waiting for a runner. |
I think the most recent commits came in while CI was disabled. You can try pushing another (possibly empty) commit, or closing and reopening the PR to retrigger CI. |
@ntBre CI is run now. |
Thanks for the ping, I'll try to review this tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all of this work! It looks good, and most of my inline comments are just stylistic nits.
For tests, I think I'd like to see ~1 test for each of the (very helpful) comments that document some constraint, if that's not too hard. Is that what you had in mind in the PR summary?
crates/ruff_linter/src/rules/perflint/rules/manual_dict_comprehension.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/perflint/rules/manual_dict_comprehension.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/perflint/rules/manual_dict_comprehension.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/perflint/rules/manual_dict_comprehension.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/perflint/rules/manual_dict_comprehension.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/perflint/rules/manual_dict_comprehension.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/perflint/rules/manual_dict_comprehension.rs
Show resolved
Hide resolved
crates/ruff_linter/src/rules/perflint/rules/manual_dict_comprehension.rs
Outdated
Show resolved
Hide resolved
...les/perflint/snapshots/ruff_linter__rules__perflint__tests__preview__PERF403_PERF403.py.snap
Outdated
Show resolved
Hide resolved
@ntBre I think I've addressed all of your feedback, so please let me know if you need anything else changed. Also, do you know why the |
At the very top of the error message it says
I think adding a fix probably changed the docs slightly, and you just need to run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I'd still like to avoid the expect
calls, and I added one more suggestion for Diagnostic::with_fix
but this looks good to me otherwise, once the tests are passing.
crates/ruff_linter/src/rules/perflint/rules/manual_dict_comprehension.rs
Outdated
Show resolved
Hide resolved
Turns out I just needed to merge with main. Did you also want the |
Yes please! |
crates/ruff_linter/src/rules/perflint/rules/manual_dict_comprehension.rs
Outdated
Show resolved
Hide resolved
@ntBre let me know if any other changes are necessary |
perflint
] Implement fix for manual-dict-comprehension
(PERF403
)
list_binding_value.is_some_and(|binding_value| match binding_value { | ||
// `value = []` | ||
Expr::List(list_expr) => list_expr.is_empty(), | ||
// `value = list()` | ||
// This is probably be linted against, but turning it into a list comprehension will also remove it | ||
Expr::Call(call) => { | ||
checker | ||
.semantic() | ||
.resolve_builtin_symbol(&call.func) | ||
.is_some_and(|name| name == "list") | ||
&& call.arguments.is_empty() | ||
} | ||
_ => false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for not noticing this earlier (or for bringing it up again if we already discussed it), but should this be included here? I think these changes (and the corresponding ones on the dict side) are causing the differences in the ecosystem check, and I would kind of like to limit this PR strictly to the autofix.
I would have at least expected some PERF401 snapshots to change with this, since it's obviously affecting ecosystem packages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can split it off in a different PR, but it's an ecosystem bug we noticed when testing.
Thanks, I thought we had to revert the |
@W0ndering Mind opening a new PR for that ecosystem fix to list Calls as well? |
…nsion (`PERF401`) (#17519) This is an implementation of the discussion from #16719. This change will allow list function calls to be replaced with comprehensions: ```python result = list() for i in range(3): result.append(i + 1) # becomes result = [i + 1 for i in range(3)] ``` I added a new test to `PERF401.py` to verify that this fix will now work for `list()`.
Summary
This change adds an auto-fix for manual dict comprehensions. It also copies many of the improvements from #13919 (and associated PRs fixing issues with it), and moves some of the utility functions from
manual_list_comprehension.rs
into a separatehelpers.rs
to be used in both.Test Plan
I added a preview test case to showcase the new fix and added a test case in
PERF403.py
to make sure lines with semicolons function. I didn't yet make similar tests to the ones I added earlier toPERF401.py
, but the logic is the same, so it might be good to add those to make sure they work.