python/fallback-on-exception
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| Python | warning | none, edit by hand | All versions |
Catching a broad Exception without binding, logging, or re-raising it and calling a second implementation silently hides bugs behind a legacy fallback path. Fix the primary path or let the real error surface.
def compute_score(values: list[float]) -> float: try: return compute_score_v2(values) except Exception: return compute_score_v1(values) return compute_score_v2(values)What the rule reports
Section titled “What the rule reports”The rule reports a try statement whose body runs a call (not an import) and whose bare except: or except Exception: clause binds no exception variable, does not log or raise, and returns or assigns the result of another call.
The rule leaves alone the cases in the list:
try/except ImportErrorblocks, whichpython/import-fallbackhandles separately.- Handlers that catch a specific expected error such as
KeyErrororValueError. - Handlers that bind the exception (
except Exception as error:) to inspect, report, log, or re-raise it. - Empty or
passhandlers.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
Remove the legacy fallback call and the broad except block. If a narrow, expected failure genuinely needs recovery, catch only that specific exception type and log or handle the error explicitly.
Diagnostic message
Section titled “Diagnostic message”Catching a broad exception without logging or re-raising and calling a fallback implementation hides failures behind a legacy code path. Fix or fail loudly on the primary path.See all rules in the Python pack.