Skip to content

Conversation

@MeGaGiGaGon
Copy link
Contributor

@MeGaGiGaGon MeGaGiGaGon commented Jul 3, 2025

Summary

Part of #18972

This PR makes if-else-block-instead-of-dict-lookup (SIM116)'s example error out-of-the-box

Old example

if x == 1:
    return "Hello"
elif x == 2:
    return "Goodbye"
else:
    return "Goodnight"

New example

def find_phrase(x):
    if x == 1:
        return "Hello"
    elif x == 2:
        return "Goodbye"
    elif x == 3:
        return "Good morning"
    else:
        return "Goodnight"

The "Use instead" section was also updated to reflect the new case. I also changed it to use an intermediary variable since I find the return <long dict>.get very ugly and hard to read.

Test Plan

N/A, no functionality/tests affected

@MeGaGiGaGon MeGaGiGaGon changed the title [flake8-simplify] Make example error out-of-the-box (SIM116) [flake8-simplify] Make example error out-of-the-box (SIM116) Jul 3, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Jul 3, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre added the documentation Improvements or additions to documentation label Jul 3, 2025
/// elif x == 3:
/// return "Good morning"
/// else:
/// return "Goodnight"
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we wrap these in functions? Otherwise the returns should be syntax errors. I was alarmed that we weren't flagging the syntax error, but that semantic error is still associated with the F706 lint rule.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would probably be a good idea. I ran my tool again looking for that, and I got only this rule and UP028 as having examples that raised one of F701-F706 (Aside from those rule's examples themselves).

Should the tests also be updated? Since they have the same problem as the example. I also did a ruff run on the tests itself, and found a few other rules that run into these in their tests:

crates\ruff_linter\resources\test\fixtures\flake8_bandit\S112.py: F702 `continue` not properly in loop
crates\ruff_linter\resources\test\fixtures\flake8_bugbear\B031.py: F706 `return` statement outside of a function/method
crates\ruff_linter\resources\test\fixtures\flake8_pie\PIE804.py: F704 `yield` statement outside of a function
crates\ruff_linter\resources\test\fixtures\flake8_simplify\SIM115.py: F704 `await` statement outside of a function
crates\ruff_linter\resources\test\fixtures\flake8_simplify\SIM116.py: F706 `return` statement outside of a function/method
crates\ruff_linter\resources\test\fixtures\pycodestyle\E27.py: F704 `yield` statement outside of a function

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 got a couple more hits adding the "Use instead" sections to the search, SIM110 and PLW1501

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, that seems like a good idea! I think the plan is still to make those rules into true SyntaxErrors eventually, so these snapshots would change then anyway.

dylwil3 pushed a commit that referenced this pull request Jul 4, 2025
…028`) (#19127)

## Summary

From me and @ntBre's discussion in #19111.

This PR makes these two examples into valid code, since they previously
had `F701`-`F707` syntax errors. `SIM110` was already fixed in a
different PR, I just forgot to pull.
dylwil3 pushed a commit that referenced this pull request Jul 4, 2025
## Summary

Per @ntBre in #19111, it would be
a good idea to make the tests no longer have these syntax errors, so
this PR updates the tests and snapshots.

`B031` gave me a lot of trouble since the ending test of declaring a
function named `groupby` makes it so that inside other functions, it's
unclear which `groupby` is referred to since it depends on when the
function is called. To fix it I made each function have it's own `from
itertools import groupby` so there's no more ambiguity.
@MichaReiser MichaReiser requested a review from ntBre July 7, 2025 13:37
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks!

@ntBre ntBre merged commit 4dd2c03 into astral-sh:main Jul 7, 2025
35 checks passed
@MeGaGiGaGon MeGaGiGaGon deleted the patch-8 branch July 7, 2025 21:30
UnboundVariable pushed a commit to UnboundVariable/ruff that referenced this pull request Jul 7, 2025
…c_tokens

* 'main' of https://github.com/astral-sh/ruff: (27 commits)
  [ty] First cut at semantic token provider (astral-sh#19108)
  [`flake8-simplify`] Make example error out-of-the-box (`SIM116`) (astral-sh#19111)
  [`flake8-use-pathlib`] Make example error out-of-the-box (`PTH210`) (astral-sh#19189)
  [`flake8-use-pathlib`] Add autofixes for `PTH203`, `PTH204`, `PTH205` (astral-sh#18922)
  [`flake8-type-checking`] Fix syntax error introduced by fix (`TC008`) (astral-sh#19150)
  [`flake8-pyi`] Make example error out-of-the-box (`PYI007`, `PYI008`) (astral-sh#19103)
  Update Rust crate indicatif to 0.18.0 (astral-sh#19165)
  [ty] Add separate CI job for memory usage stats (astral-sh#19134)
  [ty] Add documentation for server traits (astral-sh#19137)
  Rename to `SessionSnapshot`, move unwind assertion closer (astral-sh#19177)
  [`flake8-type-checking`] Make example error out-of-the-box (`TC001`) (astral-sh#19151)
  [ty] Bare `ClassVar` annotations (astral-sh#15768)
  [ty] Re-enable multithreaded pydantic benchmark (astral-sh#19176)
  [ty] Implement equivalence for protocols with method members (astral-sh#18659)
  [ty] Use RHS inferred type for bare `Final` symbols (astral-sh#19142)
  [ty] Support declaration-only attributes (astral-sh#19048)
  [ty] Sync vendored typeshed stubs (astral-sh#19174)
  Update dependency pyodide to ^0.28.0 (astral-sh#19164)
  Update NPM Development dependencies (astral-sh#19170)
  Update taiki-e/install-action action to v2.56.7 (astral-sh#19169)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants