-
Notifications
You must be signed in to change notification settings - Fork 1k
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-simplify SIM300 check for Yoda Conditions #1539
Conversation
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.
This looks great! Thank you for putting it together!
Couple small things -- only behavior change would be catching the constant-constant case, if you agree.
# Bad | ||
|
||
'yoda' == compare # SIM300 | ||
42 == age # SIM300 |
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.
One condition that flake8-simplify
seems to get wrong:
"yoda" == "yoda"
flake8-simplify
gives me:
resources/test/fixtures/flake8_simplify/SIM300.py:10:1: SIM300 Use '"yoda" == "yoda"' instead of '"yoda" == "yoda"' (Yoda-conditions)
But we should probably avoid raising this in the event that both sides are constant?
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.
Oh good catch, I'll add another check. Might be worth creating an alert at some point when comparing constants with each other too. Can't think of any good reason to do it on purpose.
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.
Haha yeah the absurdity of that didn't even occur to me... I have my correctness hat on.
@@ -0,0 +1,9 @@ | |||
# Bad | |||
|
|||
'yoda' == compare # SIM300 |
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.
Couple other good test cases:
x == y and 'yoda' == compare
(should raise)'yoda' == compare == 1
(shouldn't raise)
use crate::checkers::ast::Checker; | ||
use crate::checks::{Check, CheckKind}; | ||
|
||
pub fn yoda_conditions( |
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.
Mind just adding /// SIM300
above this function? Helps a lot with grepping for rule implementations, since otherwise there's nothing tying this file to the check code.
CONTRIBUTING.md
Outdated
#[test_case(CheckCode::A001, Path::new("A001.py"); "A001")] | ||
... | ||
``` | ||
Run Ruff locally with (e.g.) `cargo run resources/test/fixtures/E402.py --no-cache --select E402`. |
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 think this line might be repeated between here and the previous paragraph.
Awesome, thank you! |
If you were interested in enabling autofix for this rule, you could take a look at the |
great next step. thanks for the hint i'll have a look! |
[![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.206` -> `^0.0.207` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.207/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.207/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.207/compatibility-slim/0.0.206)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.207/confidence-slim/0.0.206)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.207`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.207) [Compare Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.206...v0.0.207) #### What's Changed - Implement list-to-tuple comprehension unpacking by [@​colin99d](https://togithub.com/colin99d) in [astral-sh/ruff#1534 - Avoid triggering PD errors on method calls by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#1537 - Avoid PD false positives on some non-DataFrame expressions by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#1538 - Correct UP027 message to “generator expression” by [@​andersk](https://togithub.com/andersk) in [astral-sh/ruff#1540 - Add flake8-simplify SIM300 check for Yoda Conditions by [@​PedramNavid](https://togithub.com/PedramNavid) in [astral-sh/ruff#1539 - Print warning when running debug builds without --no-cache by [@​not-my-profile](https://togithub.com/not-my-profile) in [astral-sh/ruff#1549 - Fix typing::match_annotated_subscript matching ExprKind::Call by [@​not-my-profile](https://togithub.com/not-my-profile) in [astral-sh/ruff#1554 - Add clarification by [@​VictorGob](https://togithub.com/VictorGob) in [astral-sh/ruff#1557 - Add explicit new-rule recommendation in CONTRIBUTING.md by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#1558 - Detect unpacking assignments in eradicate by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#1559 - Fix `__init__.py` being private by [@​not-my-profile](https://togithub.com/not-my-profile) in [astral-sh/ruff#1556 #### New Contributors - [@​PedramNavid](https://togithub.com/PedramNavid) made their first contribution in [astral-sh/ruff#1539 - [@​VictorGob](https://togithub.com/VictorGob) made their first contribution in [astral-sh/ruff#1557 **Full Changelog**: astral-sh/ruff@v0.0.206...v0.0.207 </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:eyJjcmVhdGVkSW5WZXIiOiIzNC43NC4yIiwidXBkYXRlZEluVmVyIjoiMzQuNzQuMiJ9--> Signed-off-by: Renovate Bot <bot@renovateapp.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
I was just writing an issue because I thought this check wanted me to use Yoda conditions, and thus it all didn't make sense to me. But now I think this is more of a communication thing, and I hope it's okay to address that here. First, I'm not sure if the description Second, Maybe the message could be more along the line of: |
Yeah this message was taken directly from |
(Fixed.) |
Thank you for your quick response! I'm very impressed by this tool and am looking forward to integrating it into our project(s) more and more! |
Adds a SIM300 Check for Yoda Conditions
Refs: #998