-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[flake8-simplify
] fix handlers corner cases (SIM105
)
#18213
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
base: main
Are you sure you want to change the base?
Conversation
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
SIM105 | 12 | 6 | 6 | 0 | 0 |
Linter (preview)
ℹ️ ecosystem check detected linter changes. (+6 -6 violations, +0 -0 fixes in 2 projects; 53 projects unchanged)
apache/superset (+1 -1 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL
+ superset/migrations/versions/2015-12-04_09-42_1a48a5411020_adding_slug_to_dash.py:35:5: SIM105 Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`pass` - superset/migrations/versions/2015-12-04_09-42_1a48a5411020_adding_slug_to_dash.py:35:5: SIM105 Use `contextlib.suppress(Exception)` instead of `try`-`except`-`pass`
binary-husky/gpt_academic (+5 -5 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview
+ crazy_functions/Latex_Function.py:76:5: SIM105 Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`pass` - crazy_functions/Latex_Function.py:76:5: SIM105 Use `contextlib.suppress(Exception)` instead of `try`-`except`-`pass` + multi_language.py:363:9: SIM105 Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`pass` - multi_language.py:363:9: SIM105 Use `contextlib.suppress(Exception)` instead of `try`-`except`-`pass` + request_llms/com_qwenapi.py:57:21: SIM105 Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`pass` - request_llms/com_qwenapi.py:57:21: SIM105 Use `contextlib.suppress(Exception)` instead of `try`-`except`-`pass` + request_llms/oai_std_model_template.py:72:5: SIM105 Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`pass` - request_llms/oai_std_model_template.py:72:5: SIM105 Use `contextlib.suppress(Exception)` instead of `try`-`except`-`pass` + toolbox.py:463:13: SIM105 Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`pass` - toolbox.py:463:13: SIM105 Use `contextlib.suppress(Exception)` instead of `try`-`except`-`pass`
Changes by rule (1 rules affected)
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
SIM105 | 12 | 6 | 6 | 0 | 0 |
"Exception".to_string() | ||
if type_.is_none() { | ||
// case where there are no handler names provided at all | ||
"BaseException".to_string() |
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.
This won’t work if BaseException
is shadowed. Here is an example of how to safely insert a builtin symbol:
ruff/crates/ruff_linter/src/rules/flake8_bugbear/rules/unreliable_callable_check.rs
Lines 78 to 82 in b35bf8a
let (import_edit, binding) = checker.importer().get_or_import_builtin_symbol( | |
"callable", | |
expr.start(), | |
checker.semantic(), | |
)?; |
@ntBre I have a problem. How I could import more stuff at the same time? Do you have an example where it was made in another rule? I'm asking because here I would like to import |
Would this work ruff/crates/ruff_linter/src/rules/ruff/rules/quadratic_list_summation.rs Lines 105 to 115 in 9ae698f
|
Hey, I hope understood what you were asking ;-) |
format!("with {binding}({exception})"), | ||
let mut rest: Vec<Edit> = Vec::new(); | ||
let content: String; | ||
if exception == "BaseException" { |
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.
Is this condition correct when BaseException
is shadowed and caught explicitly?
BaseException = ValueError
try:
int("a")
except BaseException:
pass
The fixed version should use suppress(BaseException)
, not suppress(builtins.BaseException)
.
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.
Good catch. No, it was't. I added a new test case and fix that
It should be fine now |
The PR addresses issue #18209.