Skip to content

[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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

VascoSch92
Copy link
Contributor

The PR addresses issue #18209.

Copy link
Contributor

github-actions bot commented May 20, 2025

ruff-ecosystem results

Linter (stable)

ℹ️ 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 --no-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)

+ 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

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()
Copy link

@dscorbett dscorbett May 20, 2025

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:

let (import_edit, binding) = checker.importer().get_or_import_builtin_symbol(
"callable",
expr.start(),
checker.semantic(),
)?;

@VascoSch92
Copy link
Contributor Author

@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 subprocess from contextlib and also BaseException

@MichaReiser
Copy link
Member

Would this work

let (reduce_edit, reduce_binding) = checker.importer().get_or_import_symbol(
&ImportRequest::import("functools", "reduce"),
call.start(),
checker.semantic(),
)?;
let (iadd_edit, iadd_binding) = checker.importer().get_or_import_symbol(
&ImportRequest::import("operator", "iadd"),
iterable.start(),
checker.semantic(),
)?;

@VascoSch92 VascoSch92 requested a review from dscorbett May 21, 2025 16:49
@VascoSch92
Copy link
Contributor Author

Hey,
thanks for the help.

I hope understood what you were asking ;-)

format!("with {binding}({exception})"),
let mut rest: Vec<Edit> = Vec::new();
let content: String;
if exception == "BaseException" {

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).

Copy link
Contributor Author

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

@VascoSch92
Copy link
Contributor Author

It should be fine now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants