Skip to content
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

[false-positive] WPS503 is unhappy about module-level try/except #3057

Open
webknjaz opened this issue Sep 28, 2024 · 1 comment
Open

[false-positive] WPS503 is unhappy about module-level try/except #3057

webknjaz opened this issue Sep 28, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@webknjaz
Copy link
Contributor

What's wrong

There is a common pattern of falling back to back-up modules when imports fail.

Here's what I mean:

try:
    import optional_dep as mod
except ImportError as import_err:
    optional_dep_import_exc: Exception | None = import_err
    import fallback_dep as mod
else:
    optional_dep_import_exc = None

If this was inside a function/method where the except block interrupts the control flow to bail from said function for good, it'd make sense. But not on the module level, where the exception is suppressed, and an alternative is provided. In this case, else: is vital since it usually shouldn't be executed regardless (it's not a finally:, after all).

In general, the check seems to be confused about the context of the else-block. This rule would mostly make sense where all except blocks escape the control flow (or continue an external loop, or something like that).

How it should be

This should not trigger a rule violation.

Flake8 version and plugins

N/A

pip information

N/A

OS information

N/A

@webknjaz webknjaz added the bug Something isn't working label Sep 28, 2024
@sobolevn
Copy link
Member

Yes, I agree. I faced this problem multiple times. Thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants