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

Add flake8-pie PIE796: prefer-unique-enum #1923

Merged
merged 4 commits into from
Jan 17, 2023
Merged

Add flake8-pie PIE796: prefer-unique-enum #1923

merged 4 commits into from
Jan 17, 2023

Conversation

ljesparis
Copy link
Contributor

@ljesparis ljesparis commented Jan 16, 2023

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

@charliermarsh
Copy link
Member

Awesome, thank you -- I'll review this tonight!

...but is it necessary that this one is also fixed automatically ?

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.


let mut seen_targets: FxHashSet<String> = FxHashSet::default();
for stmt in body {
let target = match &stmt.node {
Copy link
Member

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.

Copy link
Contributor Author

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.

);
impl Violation for PreferUniqueEnums {
fn message(&self) -> String {
"Consider using removing dupe values.".to_string()
Copy link
Member

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!

Copy link
Contributor Author

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

@charliermarsh charliermarsh merged commit 6e88c60 into astral-sh:main Jan 17, 2023

if !bases.iter().any(|expr| {
checker
.resolve_call_path(expr)
Copy link
Member

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,
}
Copy link
Member

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

@charliermarsh
Copy link
Member

Thank you! Made a few small changes prior to merging (left notes).

renovate bot referenced this pull request in ixm-one/pytest-cmake-presets Jan 17, 2023
[![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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;colin99d](https://togithub.com/colin99d) in
[https://github.com/charliermarsh/ruff/pull/1905](https://togithub.com/charliermarsh/ruff/pull/1905)

#### New Contributors

- [@&#8203;pmbarrett314](https://togithub.com/pmbarrett314) made their
first contribution in
[https://github.com/charliermarsh/ruff/pull/1910](https://togithub.com/charliermarsh/ruff/pull/1910)
- [@&#8203;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 <bot@renovateapp.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants