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

flake8_simplify : SIM401 #1778

Merged
merged 10 commits into from
Jan 12, 2023
Merged

Conversation

chammika-become
Copy link
Contributor

@chammika-become chammika-become commented Jan 11, 2023

Ref #998

  • Implements SIM401 with fix
  • Added tests

Notes:

  • only recognize simple ExprKind::Name variables in expr patterns for now
  • bug-fix from reference implementation: check 3-conditions (dict-key, target-variable, dict-name) to be equal, flake8_simplify only test first two (only first in second pattern)

@charliermarsh I would like to handle cases marked nice-to-have (they are handled in flake8_simplify by flattening the expr into strings). Appreciate, any pointers in improving helper match_name_expr for more complex exprs.

src/ast/helpers.rs Outdated Show resolved Hide resolved
@chammika-become chammika-become marked this pull request as ready for review January 11, 2023 07:28
@chammika-become
Copy link
Contributor Author

@charliermarsh please review.
I am not entirely happy the way a new match_comp_expr turn out to be. I could get rid of this function, if I could directly convert &Located<ExprKind> => &ComparableExpr . For example,

// test_key : &Box<Located<ExprKind>>
 let test_key_comp: &ComparableExpr = (&test_key).into(); // <= failed

Hope you can improve on that.

@charliermarsh charliermarsh merged commit 4523885 into astral-sh:main Jan 12, 2023
@charliermarsh
Copy link
Member

Thank you!

I wasn't able to remove that method entirely, but I was able to simplify it a bit.

bruxisma referenced this pull request in ixm-one/pytest-cmake-presets Jan 12, 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://github.com/charliermarsh/ruff) | `^0.0.218` ->
`^0.0.219` |
[![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.219/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.219/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.219/compatibility-slim/0.0.218)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.219/confidence-slim/0.0.218)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>charliermarsh/ruff</summary>

###
[`v0.0.219`](https://github.com/charliermarsh/ruff/releases/tag/v0.0.219)

[Compare
Source](https://github.com/charliermarsh/ruff/compare/v0.0.218...v0.0.219)

#### What's Changed

- Disable update check by default by
[@&#8203;charliermarsh](https://github.com/charliermarsh) in
[https://github.com/charliermarsh/ruff/pull/1786](https://github.com/charliermarsh/ruff/pull/1786)
- Improve PIE794 autofix behavior by
[@&#8203;charliermarsh](https://github.com/charliermarsh) in
[https://github.com/charliermarsh/ruff/pull/1794](https://github.com/charliermarsh/ruff/pull/1794)
- Convert flake8-comprehensions checks to Checker style by
[@&#8203;charliermarsh](https://github.com/charliermarsh) in
[https://github.com/charliermarsh/ruff/pull/1795](https://github.com/charliermarsh/ruff/pull/1795)
- Refactor flake8-comprehensions rules to take fewer arguments by
[@&#8203;charliermarsh](https://github.com/charliermarsh) in
[https://github.com/charliermarsh/ruff/pull/1797](https://github.com/charliermarsh/ruff/pull/1797)
- Avoid rewriting flake8-comprehensions expressions for builtin
overrides by [@&#8203;charliermarsh](https://github.com/charliermarsh)
in
[https://github.com/charliermarsh/ruff/pull/1799](https://github.com/charliermarsh/ruff/pull/1799)
- Update readme to reflect
[#&#8203;1763](https://github.com/charliermarsh/ruff/issues/1763) by
[@&#8203;Czaki](https://github.com/Czaki) in
[https://github.com/charliermarsh/ruff/pull/1780](https://github.com/charliermarsh/ruff/pull/1780)
- Avoid flagging builtins for OSError rewrites by
[@&#8203;charliermarsh](https://github.com/charliermarsh) in
[https://github.com/charliermarsh/ruff/pull/1800](https://github.com/charliermarsh/ruff/pull/1800)
- Skip unused argument checks for magic methods by
[@&#8203;charliermarsh](https://github.com/charliermarsh) in
[https://github.com/charliermarsh/ruff/pull/1801](https://github.com/charliermarsh/ruff/pull/1801)
- Skip SIM108 violations for complex if-statements by
[@&#8203;charliermarsh](https://github.com/charliermarsh) in
[https://github.com/charliermarsh/ruff/pull/1802](https://github.com/charliermarsh/ruff/pull/1802)
- \[`flake8-simplify`] Add Rule for `SIM115` (Use context handler for
opening files) by [@&#8203;saadmk11](https://github.com/saadmk11) in
[https://github.com/charliermarsh/ruff/pull/1782](https://github.com/charliermarsh/ruff/pull/1782)
- flake8\_simplify : SIM401 by
[@&#8203;chammika-become](https://github.com/chammika-become) in
[https://github.com/charliermarsh/ruff/pull/1778](https://github.com/charliermarsh/ruff/pull/1778)
- Avoid erroneous Q002 error message for single-quote docstrings by
[@&#8203;colin99d](https://github.com/colin99d) in
[https://github.com/charliermarsh/ruff/pull/1777](https://github.com/charliermarsh/ruff/pull/1777)
- Implement doc line length enforcement by
[@&#8203;charliermarsh](https://github.com/charliermarsh) in
[https://github.com/charliermarsh/ruff/pull/1804](https://github.com/charliermarsh/ruff/pull/1804)
- Move top level `ruff` into `python` folder by
[@&#8203;messense](https://github.com/messense) in
[https://github.com/charliermarsh/ruff/pull/1806](https://github.com/charliermarsh/ruff/pull/1806)
- Improve globset documentation and help message by
[@&#8203;charliermarsh](https://github.com/charliermarsh) in
[https://github.com/charliermarsh/ruff/pull/1808](https://github.com/charliermarsh/ruff/pull/1808)

#### New Contributors

- [@&#8203;Czaki](https://github.com/Czaki) made their first
contribution in
[https://github.com/charliermarsh/ruff/pull/1780](https://github.com/charliermarsh/ruff/pull/1780)

**Full Changelog**:
astral-sh/ruff@v0.0.218...v0.0.219

</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:eyJjcmVhdGVkSW5WZXIiOiIzNC45OC4xIiwidXBkYXRlZEluVmVyIjoiMzQuOTguMSJ9-->

Signed-off-by: Renovate Bot <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@chammika-become
Copy link
Contributor Author

@charliermarsh some afterthought... we are not going to flag this at all if there are comments in the stmt ? If that's the case we can move has_comments() check all the way up to the top.

@charliermarsh
Copy link
Member

@chammika-become - Yeah, it's really hard to preserve comments correctly since there are so many cases to handle (above the statement, below the statement, inline, etc.), so for now, I'm just skipping commented blocks when collapsing multi- to single-line statements. We do the same for the ternary-rewrite rule.

I prefer keeping has_comments lower as its more computationally expensive (we have to re-tokenize the block).

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