-
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
Autofix EM101
, EM102
, EM103
if possible
#4123
Autofix EM101
, EM102
, EM103
if possible
#4123
Conversation
68b80d3
to
e9adcdd
Compare
EM001
, EM002
, EM003
if possibleEM101
, EM102
, EM103
if possible
e9adcdd
to
d4057fd
Compare
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.
Looks great, just one question below.
Ecosystem checks can be found here: https://github.com/charliermarsh/ruff/actions/runs/4820598417/attempts/1#summary-13071431799 They're just addition of the |
As a stretch, it would be nice to parenthesize expressions in cases like this (imagine the expression is actually multi-line): + msg = "Some very long message"
raise Exception(
- "Some very long "
- "message"
+ msg
) |
} else { | ||
None | ||
} | ||
}); |
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.
@dhruvmanila - I'm not convinced that the change I made here is clearer, but it does mean that we avoid the "invalid" state of fixable = false
with indentation = ""
.
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.
Ah, right. Yes, it would seem that the fixable property is dependent on whether the indentation is present or not. Maybe two variables might help but not the way I did it. 🤔
diff --git a/crates/ruff/src/rules/flake8_errmsg/rules.rs b/crates/ruff/src/rules/flake8_errmsg/rules.rs
index ac30f0d7..e828e416 100644
--- a/crates/ruff/src/rules/flake8_errmsg/rules.rs
+++ b/crates/ruff/src/rules/flake8_errmsg/rules.rs
@@ -229,20 +229,11 @@ pub fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr) {
} => {
if checker.settings.rules.enabled(Rule::RawStringInException) {
if string.len() > checker.settings.flake8_errmsg.max_string_length {
- let indentation = whitespace::indentation(checker.locator, stmt)
- .and_then(|indentation| {
- if checker.ctx.find_binding("msg").is_none() {
- Some(indentation)
- } else {
- None
- }
- });
- let mut diagnostic = Diagnostic::new(
- RawStringInException {
- fixable: indentation.is_some(),
- },
- first.range(),
- );
+ let indentation = whitespace::indentation(checker.locator, stmt);
+ let fixable = indentation
+ .map_or(false, |_| checker.ctx.find_binding("msg").is_none());
+ let mut diagnostic =
+ Diagnostic::new(RawStringInException { fixable }, first.range());
if let Some(indentation) = indentation {
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(generate_fix(
Do you mean to autofix it like so? +msg = (
+ "This is an example exception. "
+ "This message is very long "
+ "and spans multiple lines."
+)
raise Exception(
- "This is an example exception. "
- "This message is very long "
- "and spans multiple lines."
+ msg
) It could be but not with the help of AST. I think slicing the source code would work with |
[![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.263` -> `^0.0.264` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/compatibility-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/confidence-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.264`](https://github.com/charliermarsh/ruff/releases/tag/v0.0.264) [Compare Source](https://github.com/charliermarsh/ruff/compare/v0.0.263...v0.0.264) <!-- Release notes generated using configuration in .github/release.yml at 8cb76f85eba1c970a8c800348fd1e0c874621a57 --> #### What's Changed ##### Rules - Autofix `EM101`, `EM102`, `EM103` if possible by [@​dhruvmanila](https://github.com/dhruvmanila) in [astral-sh/ruff#4123 - Add bugbear immutable functions as allowed in dataclasses by [@​mosauter](https://github.com/mosauter) in [astral-sh/ruff#4122 ##### Settings - Add support for providing command-line arguments via `argfile` by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4087 ##### Bug Fixes - Make D410/D411 autofixes mutually exclusive by [@​evanrittenhouse](https://github.com/evanrittenhouse) in [astral-sh/ruff#4110 - Remove `pyright` comment prefix from PYI033 checks by [@​evanrittenhouse](https://github.com/evanrittenhouse) in [astral-sh/ruff#4152 - Fix F811 false positive with match by [@​JonathanPlasse](https://github.com/JonathanPlasse) in [astral-sh/ruff#4161 - Fix `E713` and `E714` false positives for multiple comparisons by [@​JonathanPlasse](https://github.com/JonathanPlasse) in [astral-sh/ruff#4083 - Fix B023 shadowed variables in nested functions by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#4111 - Preserve star-handling special-casing for force-single-line by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4129 - Respect parent-scoping rules for `NamedExpr` assignments by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4145 - Fix UP032 auto-fix by [@​JonathanPlasse](https://github.com/JonathanPlasse) in [astral-sh/ruff#4165 - Allow boolean parameters for `pytest.param` by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4176 ##### Internal - Replace row/column based `Location` with byte-offsets. by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#3931 - perf(logical-lines): Various small perf improvements by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#4022 - Use `memchr` to speedup newline search on x86 by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#3985 - Remove `ScopeStack` in favor of child-parent `ScopeId` pointers by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4138 **Full Changelog**: astral-sh/ruff@v0.0.263...v0.0.264 </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:eyJjcmVhdGVkSW5WZXIiOiIzNS42Ni4zIiwidXBkYXRlZEluVmVyIjoiMzUuNjYuMyIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==--> Signed-off-by: Renovate Bot <bot@renovateapp.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
[![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.263` -> `==0.0.264` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/compatibility-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/confidence-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.264`](https://github.com/charliermarsh/ruff/releases/tag/v0.0.264) [Compare Source](https://github.com/charliermarsh/ruff/compare/v0.0.263...v0.0.264) <!-- Release notes generated using configuration in .github/release.yml at 8cb76f85eba1c970a8c800348fd1e0c874621a57 --> #### What's Changed ##### Rules - Autofix `EM101`, `EM102`, `EM103` if possible by [@​dhruvmanila](https://github.com/dhruvmanila) in [astral-sh/ruff#4123 - Add bugbear immutable functions as allowed in dataclasses by [@​mosauter](https://github.com/mosauter) in [astral-sh/ruff#4122 ##### Settings - Add support for providing command-line arguments via `argfile` by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4087 ##### Bug Fixes - Make D410/D411 autofixes mutually exclusive by [@​evanrittenhouse](https://github.com/evanrittenhouse) in [astral-sh/ruff#4110 - Remove `pyright` comment prefix from PYI033 checks by [@​evanrittenhouse](https://github.com/evanrittenhouse) in [astral-sh/ruff#4152 - Fix F811 false positive with match by [@​JonathanPlasse](https://github.com/JonathanPlasse) in [astral-sh/ruff#4161 - Fix `E713` and `E714` false positives for multiple comparisons by [@​JonathanPlasse](https://github.com/JonathanPlasse) in [astral-sh/ruff#4083 - Fix B023 shadowed variables in nested functions by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#4111 - Preserve star-handling special-casing for force-single-line by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4129 - Respect parent-scoping rules for `NamedExpr` assignments by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4145 - Fix UP032 auto-fix by [@​JonathanPlasse](https://github.com/JonathanPlasse) in [astral-sh/ruff#4165 - Allow boolean parameters for `pytest.param` by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4176 ##### Internal - Replace row/column based `Location` with byte-offsets. by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#3931 - perf(logical-lines): Various small perf improvements by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#4022 - Use `memchr` to speedup newline search on x86 by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#3985 - Remove `ScopeStack` in favor of child-parent `ScopeId` pointers by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4138 **Full Changelog**: astral-sh/ruff@v0.0.263...v0.0.264 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, 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/allenporter/flux-local). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS42OS4zIiwidXBkYXRlZEluVmVyIjoiMzUuNjkuMyIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
[![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.263` -> `==0.0.264` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/compatibility-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/confidence-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.264`](https://github.com/charliermarsh/ruff/releases/tag/v0.0.264) [Compare Source](https://github.com/charliermarsh/ruff/compare/v0.0.263...v0.0.264) <!-- Release notes generated using configuration in .github/release.yml at 8cb76f85eba1c970a8c800348fd1e0c874621a57 --> #### What's Changed ##### Rules - Autofix `EM101`, `EM102`, `EM103` if possible by [@​dhruvmanila](https://github.com/dhruvmanila) in [astral-sh/ruff#4123 - Add bugbear immutable functions as allowed in dataclasses by [@​mosauter](https://github.com/mosauter) in [astral-sh/ruff#4122 ##### Settings - Add support for providing command-line arguments via `argfile` by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4087 ##### Bug Fixes - Make D410/D411 autofixes mutually exclusive by [@​evanrittenhouse](https://github.com/evanrittenhouse) in [astral-sh/ruff#4110 - Remove `pyright` comment prefix from PYI033 checks by [@​evanrittenhouse](https://github.com/evanrittenhouse) in [astral-sh/ruff#4152 - Fix F811 false positive with match by [@​JonathanPlasse](https://github.com/JonathanPlasse) in [astral-sh/ruff#4161 - Fix `E713` and `E714` false positives for multiple comparisons by [@​JonathanPlasse](https://github.com/JonathanPlasse) in [astral-sh/ruff#4083 - Fix B023 shadowed variables in nested functions by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#4111 - Preserve star-handling special-casing for force-single-line by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4129 - Respect parent-scoping rules for `NamedExpr` assignments by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4145 - Fix UP032 auto-fix by [@​JonathanPlasse](https://github.com/JonathanPlasse) in [astral-sh/ruff#4165 - Allow boolean parameters for `pytest.param` by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4176 ##### Internal - Replace row/column based `Location` with byte-offsets. by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#3931 - perf(logical-lines): Various small perf improvements by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#4022 - Use `memchr` to speedup newline search on x86 by [@​MichaReiser](https://github.com/MichaReiser) in [astral-sh/ruff#3985 - Remove `ScopeStack` in favor of child-parent `ScopeId` pointers by [@​charliermarsh](https://github.com/charliermarsh) in [astral-sh/ruff#4138 **Full Changelog**: astral-sh/ruff@v0.0.263...v0.0.264 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, 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/allenporter/pyrainbird). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS42OS4zIiwidXBkYXRlZEluVmVyIjoiMzUuNjkuMyIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Summary
Generate an autofix for
EM101
,EM102
,EM103
if possible. This will avoid the autofix if themsg
variable is bound in the current scope or the parent scope. Otherwise, it'll produce 2 edits for the fix:raise
statement. The variable name ismsg
.Indentation is taken care using the helper function
ruff_python_ast::whitespace::indentation
Newline is taken from
checker.stylist.line_ending
fixes: #4103