Generalize PIE807 to handle dict literals#8608
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| PIE807 | 1 | 1 | 0 | 0 | 0 |
Linter (preview)
ℹ️ ecosystem check detected linter changes. (+6 -0 violations, +0 -0 fixes in 41 projects)
demisto/content (+5 -0 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --preview
+ Packs/RubrikPolaris/Integrations/RubrikPolaris/RubrikPolaris_test.py:61:45: PIE807 [*] Prefer `dict` over useless lambda + Packs/VMware/Integrations/VMware/VMware_test.py:593:48: PIE807 [*] Prefer `dict` over useless lambda + Packs/VMware/Integrations/VMware/VMware_test.py:649:48: PIE807 [*] Prefer `dict` over useless lambda + Packs/VMware/Integrations/VMware/VMware_test.py:677:48: PIE807 [*] Prefer `dict` over useless lambda + Packs/VMware/Integrations/VMware/VMware_test.py:721:48: PIE807 [*] Prefer `dict` over useless lambda
rotki/rotki (+1 -0 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --preview
+ rotkehlchen/tests/api/test_balances.py:953:135: PIE807 [*] Prefer `dict` over useless lambda
Changes by rule (1 rules affected)
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| PIE807 | 6 | 6 | 0 | 0 | 0 |
|
👍 I think it's fine to use a single rule, and even to rename it (I believe our versioning policy allows this), but we should probably gate the change in behavior to preview-only. |
|
You can grep for |
| pub struct ReimplementedListBuiltin; | ||
| pub struct ReimplementedContainerBuiltin; |
There was a problem hiding this comment.
Maybe just ReimplementedBuiltin would suffice?
There was a problem hiding this comment.
I think we have some other rules that also detect re-implemented builtins though -- like the simplify rules that detect re-implemented any and all loops.
There was a problem hiding this comment.
Hm yes ReimplementedBuiltin is used for SIM110 and SIM111 — that's such a generic name for those though...
I think ReimplementedContainerBuiltin is fine but separately we should consider changing that other one to be more specific :D
| fn message(&self) -> String { | ||
| format!("Prefer `list` over useless lambda") | ||
| format!("Prefer `list` or `dict` over useless lambda") | ||
| } |
There was a problem hiding this comment.
You could store the type on the Violation then say "Prefer list over lambda" or "Prefer dict over lambda" depending on the case. It seems confusing to present both to the user when we know which they meant.
There was a problem hiding this comment.
Agreed! Can grep for DeferralKeyword as an example of this pattern.
|
I remember reviewing a PR for a more general rule about any lambda that didn't pass any args to a function call. Would that be a better rule to enable? I think it was a pylint one. |
|
I think it's #7953, although I think that rule may not catch this case because it's not aware that (e.g.) |
|
Ah, you are right. PIE807 doesn't actually even catch but unnecessary lambda does. |
a3ba70f to
b630dde
Compare
| /// | ||
| /// [preview]: https://docs.astral.sh/ruff/preview/ | ||
| #[violation] | ||
| pub struct ReimplementedContainerBuiltin(&'static str); |
There was a problem hiding this comment.
I could make this an enum instead, but not sure it's worthwhile compared to just storing the builtin inline.
There was a problem hiding this comment.
I decided to make it an enum to follow the pattern we use elsewhere, even though it's really unimportant.
|
(The Rotki ecosystem change is expected as they use preview mode by default.) |
Summary
PIE807 will rewrite
lambda: []tolist-- AFAICT though, the same rationale also applies to dicts, so I've modified the code to also rewritelambda: {}todict.Two things I'm not sure about:
reimplemented_list_builtintoreimplemented_container_builtin?Test Plan
Added snapshot tests of the functionality.