[false-positive] WPS503 is unhappy about module-level try/except
webknjaz opened this issue · 2 comments
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
Yes, I agree. I faced this problem multiple times. Thanks for the report!