From beec2f2dbb9c8c79b772594230b1b2921f37b60f Mon Sep 17 00:00:00 2001 From: Giovani Moutinho Date: Thu, 25 Sep 2025 12:19:26 -0300 Subject: [PATCH] [`flake8-simplify`] Improve help message clarity (`SIM105`) (#20548) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 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 --- .../rules/suppressible_exception.rs | 4 +++- ...8_simplify__tests__SIM105_SIM105_0.py.snap | 24 +++++++++---------- ...8_simplify__tests__SIM105_SIM105_1.py.snap | 2 +- ...8_simplify__tests__SIM105_SIM105_2.py.snap | 2 +- ...8_simplify__tests__SIM105_SIM105_3.py.snap | 2 +- ...8_simplify__tests__SIM105_SIM105_4.py.snap | 2 +- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs index 34c4915f3b..d3b93e3dbb 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs @@ -58,7 +58,9 @@ impl Violation for SuppressibleException { fn fix_title(&self) -> Option { let SuppressibleException { exception } = self; - Some(format!("Replace with `contextlib.suppress({exception})`")) + Some(format!( + "Replace `try`-`except`-`pass` with `with contextlib.suppress({exception}): ...`" + )) } } diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_0.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_0.py.snap index 0a05a9ca89..26a37405fa 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_0.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_0.py.snap @@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass 9 | | pass | |________^ | -help: Replace with `contextlib.suppress(ValueError)` +help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...` 1 + import contextlib 2 | def foo(): 3 | pass @@ -40,7 +40,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`exce 17 | 18 | # SIM105 | -help: Replace with `contextlib.suppress(ValueError, OSError)` +help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...` 1 + import contextlib 2 | def foo(): 3 | pass @@ -71,7 +71,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`exce 23 | 24 | # SIM105 | -help: Replace with `contextlib.suppress(ValueError, OSError)` +help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...` 1 + import contextlib 2 | def foo(): 3 | pass @@ -102,7 +102,7 @@ SIM105 [*] Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`p 29 | 30 | # SIM105 | -help: Replace with `contextlib.suppress(BaseException)` +help: Replace `try`-`except`-`pass` with `with contextlib.suppress(BaseException): ...` 1 + import contextlib 2 + import builtins 3 | def foo(): @@ -134,7 +134,7 @@ SIM105 [*] Use `contextlib.suppress(a.Error, b.Error)` instead of `try`-`except` 35 | 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 2 | def foo(): 3 | pass @@ -164,7 +164,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass 88 | | ... | |___________^ | -help: Replace with `contextlib.suppress(ValueError)` +help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...` 1 + import contextlib 2 | def foo(): 3 | pass @@ -195,7 +195,7 @@ SIM105 Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`except`- 104 | 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_0.py:117:5 @@ -210,7 +210,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass` 121 | 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 2 | def foo(): 3 | pass @@ -241,7 +241,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass` 125 | 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 2 | def foo(): 3 | pass @@ -271,7 +271,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass` 129 | \ 130 | # | -help: Replace with `contextlib.suppress(OSError)` +help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...` 1 + import contextlib 2 | def foo(): 3 | pass @@ -299,7 +299,7 @@ SIM105 [*] Use `contextlib.suppress()` instead of `try`-`except`-`pass` 136 | | pass | |________^ | -help: Replace with `contextlib.suppress()` +help: Replace `try`-`except`-`pass` with `with contextlib.suppress(): ...` 1 + import contextlib 2 | def foo(): 3 | pass @@ -328,7 +328,7 @@ SIM105 [*] Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`p 143 | | pass | |________^ | -help: Replace with `contextlib.suppress(BaseException)` +help: Replace `try`-`except`-`pass` with `with contextlib.suppress(BaseException): ...` 1 + import contextlib 2 | def foo(): 3 | pass diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_1.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_1.py.snap index 79aab24239..9ad5a3c17e 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_1.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_1.py.snap @@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`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.""" 2 | import math 3 + import contextlib diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_2.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_2.py.snap index 88621f5d35..2f73d90197 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_2.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_2.py.snap @@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass 13 | | pass | |________^ | -help: Replace with `contextlib.suppress(ValueError)` +help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...` 7 | 8 | 9 | # SIM105 diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_3.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_3.py.snap index 385aabd5de..3f4f3a5e14 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_3.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_3.py.snap @@ -12,4 +12,4 @@ SIM105 Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass` 13 | | pass | |____________^ | -help: Replace with `contextlib.suppress(ValueError)` +help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...` diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_4.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_4.py.snap index 78413d7d1f..da7e0842f7 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_4.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_4.py.snap @@ -10,7 +10,7 @@ SIM105 [*] Use `contextlib.suppress(ImportError)` instead of `try`-`except`-`pas 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 - try: 2 + import contextlib