mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:18 +00:00
[flake8-simplify
] Improve help message clarity (SIM105
) (#20548)
## Summary Improve the SIM105 rule message to prevent user confusion about how to properly use `contextlib.suppress`. The previous message "Replace with `contextlib.suppress(ValueError)`" was ambiguous and led users to incorrectly use `contextlib.suppress(ValueError)` as a statement inside except blocks instead of replacing the entire try-except-pass block with `with contextlib.suppress(ValueError):`. This change makes the message more explicit: - **Before**: `"Use \`contextlib.suppress({exception})\` instead of \`try\`-\`except\`-\`pass\`"` - **After**: `"Replace \`try\`-\`except\`-\`pass\` block with \`with contextlib.suppress({exception})\`"` The fix title is also updated to be more specific: - **Before**: `"Replace with \`contextlib.suppress({exception})\`"` - **After**: `"Replace \`try\`-\`except\`-\`pass\` with \`with contextlib.suppress({exception})\`"` Fixes #20462 ## Test Plan - ✅ All existing SIM105 tests pass with updated snapshots - ✅ Cargo clippy passes without warnings - ✅ Full test suite passes - ✅ The new messages clearly indicate that the entire try-except-pass block should be replaced with a `with` statement, preventing the misuse described in the issue --------- Co-authored-by: Giovani Moutinho <e@mgiovani.dev>
This commit is contained in:
parent
c256c7943c
commit
beec2f2dbb
6 changed files with 19 additions and 17 deletions
|
@ -58,7 +58,9 @@ impl Violation for SuppressibleException {
|
||||||
|
|
||||||
fn fix_title(&self) -> Option<String> {
|
fn fix_title(&self) -> Option<String> {
|
||||||
let SuppressibleException { exception } = self;
|
let SuppressibleException { exception } = self;
|
||||||
Some(format!("Replace with `contextlib.suppress({exception})`"))
|
Some(format!(
|
||||||
|
"Replace `try`-`except`-`pass` with `with contextlib.suppress({exception}): ...`"
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
|
||||||
9 | | pass
|
9 | | pass
|
||||||
| |________^
|
| |________^
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(ValueError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
|
||||||
1 + import contextlib
|
1 + import contextlib
|
||||||
2 | def foo():
|
2 | def foo():
|
||||||
3 | pass
|
3 | pass
|
||||||
|
@ -40,7 +40,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`exce
|
||||||
17 |
|
17 |
|
||||||
18 | # SIM105
|
18 | # SIM105
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(ValueError, OSError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`
|
||||||
1 + import contextlib
|
1 + import contextlib
|
||||||
2 | def foo():
|
2 | def foo():
|
||||||
3 | pass
|
3 | pass
|
||||||
|
@ -71,7 +71,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`exce
|
||||||
23 |
|
23 |
|
||||||
24 | # SIM105
|
24 | # SIM105
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(ValueError, OSError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`
|
||||||
1 + import contextlib
|
1 + import contextlib
|
||||||
2 | def foo():
|
2 | def foo():
|
||||||
3 | pass
|
3 | pass
|
||||||
|
@ -102,7 +102,7 @@ SIM105 [*] Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`p
|
||||||
29 |
|
29 |
|
||||||
30 | # SIM105
|
30 | # SIM105
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(BaseException)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(BaseException): ...`
|
||||||
1 + import contextlib
|
1 + import contextlib
|
||||||
2 + import builtins
|
2 + import builtins
|
||||||
3 | def foo():
|
3 | def foo():
|
||||||
|
@ -134,7 +134,7 @@ SIM105 [*] Use `contextlib.suppress(a.Error, b.Error)` instead of `try`-`except`
|
||||||
35 |
|
35 |
|
||||||
36 | # OK
|
36 | # OK
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(a.Error, b.Error)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(a.Error, b.Error): ...`
|
||||||
1 + import contextlib
|
1 + import contextlib
|
||||||
2 | def foo():
|
2 | def foo():
|
||||||
3 | pass
|
3 | pass
|
||||||
|
@ -164,7 +164,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
|
||||||
88 | | ...
|
88 | | ...
|
||||||
| |___________^
|
| |___________^
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(ValueError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
|
||||||
1 + import contextlib
|
1 + import contextlib
|
||||||
2 | def foo():
|
2 | def foo():
|
||||||
3 | pass
|
3 | pass
|
||||||
|
@ -195,7 +195,7 @@ SIM105 Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`except`-
|
||||||
104 |
|
104 |
|
||||||
105 | try:
|
105 | try:
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(ValueError, OSError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`
|
||||||
|
|
||||||
SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
|
SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
|
||||||
--> SIM105_0.py:117:5
|
--> SIM105_0.py:117:5
|
||||||
|
@ -210,7 +210,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
|
||||||
121 |
|
121 |
|
||||||
122 | try: os.makedirs(model_dir);
|
122 | try: os.makedirs(model_dir);
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(OSError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
|
||||||
1 + import contextlib
|
1 + import contextlib
|
||||||
2 | def foo():
|
2 | def foo():
|
||||||
3 | pass
|
3 | pass
|
||||||
|
@ -241,7 +241,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
|
||||||
125 |
|
125 |
|
||||||
126 | try: os.makedirs(model_dir);
|
126 | try: os.makedirs(model_dir);
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(OSError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
|
||||||
1 + import contextlib
|
1 + import contextlib
|
||||||
2 | def foo():
|
2 | def foo():
|
||||||
3 | pass
|
3 | pass
|
||||||
|
@ -271,7 +271,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
|
||||||
129 | \
|
129 | \
|
||||||
130 | #
|
130 | #
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(OSError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
|
||||||
1 + import contextlib
|
1 + import contextlib
|
||||||
2 | def foo():
|
2 | def foo():
|
||||||
3 | pass
|
3 | pass
|
||||||
|
@ -299,7 +299,7 @@ SIM105 [*] Use `contextlib.suppress()` instead of `try`-`except`-`pass`
|
||||||
136 | | pass
|
136 | | pass
|
||||||
| |________^
|
| |________^
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress()`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(): ...`
|
||||||
1 + import contextlib
|
1 + import contextlib
|
||||||
2 | def foo():
|
2 | def foo():
|
||||||
3 | pass
|
3 | pass
|
||||||
|
@ -328,7 +328,7 @@ SIM105 [*] Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`p
|
||||||
143 | | pass
|
143 | | pass
|
||||||
| |________^
|
| |________^
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(BaseException)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(BaseException): ...`
|
||||||
1 + import contextlib
|
1 + import contextlib
|
||||||
2 | def foo():
|
2 | def foo():
|
||||||
3 | pass
|
3 | pass
|
||||||
|
|
|
@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
|
||||||
8 | | pass
|
8 | | pass
|
||||||
| |________^
|
| |________^
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(ValueError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
|
||||||
1 | """Case: There's a random import, so it should add `contextlib` after it."""
|
1 | """Case: There's a random import, so it should add `contextlib` after it."""
|
||||||
2 | import math
|
2 | import math
|
||||||
3 + import contextlib
|
3 + import contextlib
|
||||||
|
|
|
@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
|
||||||
13 | | pass
|
13 | | pass
|
||||||
| |________^
|
| |________^
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(ValueError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
|
||||||
7 |
|
7 |
|
||||||
8 |
|
8 |
|
||||||
9 | # SIM105
|
9 | # SIM105
|
||||||
|
|
|
@ -12,4 +12,4 @@ SIM105 Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass`
|
||||||
13 | | pass
|
13 | | pass
|
||||||
| |____________^
|
| |____________^
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(ValueError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
|
||||||
|
|
|
@ -10,7 +10,7 @@ SIM105 [*] Use `contextlib.suppress(ImportError)` instead of `try`-`except`-`pas
|
||||||
4 | | except ImportError: pass
|
4 | | except ImportError: pass
|
||||||
| |___________________________^
|
| |___________________________^
|
||||||
|
|
|
|
||||||
help: Replace with `contextlib.suppress(ImportError)`
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ImportError): ...`
|
||||||
1 | #!/usr/bin/env python
|
1 | #!/usr/bin/env python
|
||||||
- try:
|
- try:
|
||||||
2 + import contextlib
|
2 + import contextlib
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue