-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add flake8-pie PIE796: prefer-unique-enum #1923
Conversation
Awesome, thank you -- I'll review this tonight!
I think in this case we probably shouldn't autofix it (even though we could), since there's not a clearly-correct way to resolve the error. That is, it's not clear which enum member should be removed, so we should probably just leave it. |
src/rules/flake8_pie/rules.rs
Outdated
|
||
let mut seen_targets: FxHashSet<String> = FxHashSet::default(); | ||
for stmt in body { | ||
let target = match &stmt.node { |
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.
Could we instead use...
let comparable_value: ComparableExpr = (&value).into();
if !seen_targets.insert(comparable_value) {
...
}
Not exact code, just pseudo code. But this would detect any duplicate values, not just constants.
If we do want to leave it as constants, can we change the ExprKind::Constant
match to use ExprKind::Constant { value: Constant::Str(value), .. }
? And remove the value.to_string()
? That way, we'll only match on strings, which I think better matches the desired semantics.
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.
good one, i didn't know about ComparableExpr 😅. It makes everything a bit more simple.
src/violations.rs
Outdated
); | ||
impl Violation for PreferUniqueEnums { | ||
fn message(&self) -> String { | ||
"Consider using removing dupe values.".to_string() |
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.
I know this is copied over from flake8-pie
but I think we can do something more descriptive...
Can we change pub struct PreferUniqueEnums
to:
pub struct PreferUniqueEnums {
pub value: String
}
Then, change this to, like: format!("Enum contains duplicate value:
{value}")
.
In prefer_unique_enums
, you can pass violations::PreferUniqueEnums(unparse_expr(&values[i], checker.stylist))
to get the formatted value.
Does that seem ok? Let me know if you have any questions!
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.
it seems more explicit, which is a good thing. I've uploaded the review changes 👍🏿
|
||
if !bases.iter().any(|expr| { | ||
checker | ||
.resolve_call_path(expr) |
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.
I tweaked this logic to use resolve_call_path
, which validates that the expression is bound to enum.Enum
(i.e., it tracks imports, follows aliases, and so on).
define_violation!( | ||
pub struct PreferUniqueEnums { | ||
pub value: String, | ||
} |
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.
(Tweaked this to use named fields which we're trying to prefer going forwards.)
Thank you! Made a few small changes prior to merging (left notes). |
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [ruff](https://togithub.com/charliermarsh/ruff) | `^0.0.223` -> `^0.0.224` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.224/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.224/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.224/compatibility-slim/0.0.223)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.224/confidence-slim/0.0.223)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.224`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.224) [Compare Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.223...v0.0.224) #### What's Changed - Re-run benchmark and update documentation by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1907](https://togithub.com/charliermarsh/ruff/pull/1907) - Derive Hash instead of implementing it by hand by [@​not-my-profile](https://togithub.com/not-my-profile) in [https://github.com/charliermarsh/ruff/pull/1890](https://togithub.com/charliermarsh/ruff/pull/1890) - Add backticks to B904's message by [@​harupy](https://togithub.com/harupy) in [https://github.com/charliermarsh/ruff/pull/1914](https://togithub.com/charliermarsh/ruff/pull/1914) - Refactor `flake8_tidy_imports` by [@​not-my-profile](https://togithub.com/not-my-profile) in [https://github.com/charliermarsh/ruff/pull/1909](https://togithub.com/charliermarsh/ruff/pull/1909) - Trigger update to pre-commit mirror after pypi publish by [@​pmbarrett314](https://togithub.com/pmbarrett314) in [https://github.com/charliermarsh/ruff/pull/1910](https://togithub.com/charliermarsh/ruff/pull/1910) - Rewrite `lru_cache` to `cache` on Python 3.9+ by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1918](https://togithub.com/charliermarsh/ruff/pull/1918) - Avoid syntax errors when fixing parenthesized unused variables by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1919](https://togithub.com/charliermarsh/ruff/pull/1919) - Add some new testimonials by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1921](https://togithub.com/charliermarsh/ruff/pull/1921) - Avoid removing statements that contain side-effects by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1920](https://togithub.com/charliermarsh/ruff/pull/1920) - Add benchmark scripts for no-IO by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1925](https://togithub.com/charliermarsh/ruff/pull/1925) - Add flake8-pie PIE796: prefer-unique-enum by [@​ljesparis](https://togithub.com/ljesparis) in [https://github.com/charliermarsh/ruff/pull/1923](https://togithub.com/charliermarsh/ruff/pull/1923) - \[pyupgrade] Automatically rewrite format-strings to f-strings by [@​colin99d](https://togithub.com/colin99d) in [https://github.com/charliermarsh/ruff/pull/1905](https://togithub.com/charliermarsh/ruff/pull/1905) #### New Contributors - [@​pmbarrett314](https://togithub.com/pmbarrett314) made their first contribution in [https://github.com/charliermarsh/ruff/pull/1910](https://togithub.com/charliermarsh/ruff/pull/1910) - [@​ljesparis](https://togithub.com/ljesparis) made their first contribution in [https://github.com/charliermarsh/ruff/pull/1923](https://togithub.com/charliermarsh/ruff/pull/1923) **Full Changelog**: astral-sh/ruff@v0.0.223...v0.0.224 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/ixm-one/pytest-cmake-presets). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMDIuNyIsInVwZGF0ZWRJblZlciI6IjM0LjEwMi43In0=--> Signed-off-by: Renovate Bot <[email protected]> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
I accept any suggestion. By the way, I have a doubt, I have checked and all flake8-pie plugins can be fixed by ruff, but is it necessary that this one is also fixed automatically ?
rel #1543