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

Generalize PIE807 to handle dict literals #8608

Merged
merged 7 commits into from
Nov 13, 2023

Conversation

alanhdu
Copy link
Contributor

@alanhdu alanhdu commented Nov 10, 2023

Summary

PIE807 will rewrite lambda: [] to list -- AFAICT though, the same rationale also applies to dicts, so I've modified the code to also rewrite lambda: {} to dict.

Two things I'm not sure about:

  • Should this go to a new rule? This no longer actually matches the behavior of flake8-pie, and while I think thematically it makes sense to be part of the same rule, we could make it a standalone rule (but if so, where should I put it and what error code should I use)?
  • If we want a single rule, are there backwards compatibility concerns with the rule name change (from reimplemented_list_builtin to reimplemented_container_builtin?

Test Plan

Added snapshot tests of the functionality.

Copy link
Contributor

github-actions bot commented Nov 10, 2023

ruff-ecosystem results

Linter (stable)

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

rotki/rotki (+1 -0 violations, +0 -0 fixes)

+ 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 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

@charliermarsh
Copy link
Member

👍 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.

@charliermarsh
Copy link
Member

You can grep for fn preview_rules for an example of testing preview rules, and to find rules that currently deviate based on the preview flag.

@charliermarsh charliermarsh self-requested a review November 10, 2023 20:51
@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Nov 10, 2023
Comment on lines 39 to 41
pub struct ReimplementedListBuiltin;
pub struct ReimplementedContainerBuiltin;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just ReimplementedBuiltin would suffice?

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

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

Comment on lines 47 to 49
fn message(&self) -> String {
format!("Prefer `list` over useless lambda")
format!("Prefer `list` or `dict` over useless lambda")
}
Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed! Can grep for DeferralKeyword as an example of this pattern.

@Skylion007
Copy link
Contributor

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.

@charliermarsh
Copy link
Member

I think it's #7953, although I think that rule may not catch this case because it's not aware that (e.g.) [] is equivalent to list.

@Skylion007
Copy link
Contributor

Ah, you are right. PIE807 doesn't actually even catch

doc_string_dict = defaultdict(lambda: list())

but unnecessary lambda does.

@alanhdu alanhdu force-pushed the alan/container-builtins branch from a3ba70f to b630dde Compare November 13, 2023 15:45
///
/// [preview]: https://docs.astral.sh/ruff/preview/
#[violation]
pub struct ReimplementedContainerBuiltin(&'static str);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could make this an enum instead, but not sure it's worthwhile compared to just storing the builtin inline.

Copy link
Member

Choose a reason for hiding this comment

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

I decided to make it an enum to follow the pattern we use elsewhere, even though it's really unimportant.

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Thanks!

@charliermarsh charliermarsh enabled auto-merge (squash) November 13, 2023 17:46
@charliermarsh
Copy link
Member

(The Rotki ecosystem change is expected as they use preview mode by default.)

@charliermarsh charliermarsh merged commit 6f23bdb into astral-sh:main Nov 13, 2023
15 of 16 checks passed
@alanhdu alanhdu deleted the alan/container-builtins branch November 16, 2023 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants