Skip to content

Commit 24580e2

Browse files
authored
flake8-simplify: avoid unnecessary builtins import for SIM105 (#22358)
1 parent 3d3af6f commit 24580e2

2 files changed

Lines changed: 15 additions & 19 deletions

File tree

crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,11 @@ pub(crate) fn suppressible_exception(
157157
let mut rest: Vec<Edit> = Vec::new();
158158
let content: String;
159159
if exception == "BaseException" && handler_names.is_empty() {
160-
let (import_exception, binding_exception) =
161-
checker.importer().get_or_import_symbol(
162-
&ImportRequest::import("builtins", &exception),
163-
stmt.start(),
164-
checker.semantic(),
165-
)?;
160+
let (import_exception, binding_exception) = checker
161+
.importer()
162+
.get_or_import_builtin_symbol(&exception, stmt.start(), checker.semantic())?;
166163
content = format!("with {binding}({binding_exception})");
167-
rest.push(import_exception);
164+
rest.extend(import_exception);
168165
} else {
169166
content = format!("with {binding}({exception})");
170167
}

crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_0.py.snap

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,21 @@ SIM105 [*] Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`p
104104
|
105105
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(BaseException): ...`
106106
1 + import contextlib
107-
2 + import builtins
108-
3 | def foo():
109-
4 | pass
110-
5 |
107+
2 | def foo():
108+
3 | pass
109+
4 |
111110
--------------------------------------------------------------------------------
112-
24 | pass
113-
25 |
114-
26 | # SIM105
111+
23 | pass
112+
24 |
113+
25 | # SIM105
115114
- try:
116-
27 + with contextlib.suppress(builtins.BaseException):
117-
28 | foo()
115+
26 + with contextlib.suppress(BaseException):
116+
27 | foo()
118117
- except:
119118
- pass
120-
29 |
121-
30 | # SIM105
122-
31 | try:
119+
28 |
120+
29 | # SIM105
121+
30 | try:
123122
note: This is an unsafe fix and may change runtime behavior
124123

125124
SIM105 [*] Use `contextlib.suppress(a.Error, b.Error)` instead of `try`-`except`-`pass`

0 commit comments

Comments
 (0)