PY-EXCE0004 · nullable_exception_return_suppression
Use suppress when one guarded return maps an exception to None.
This is a deterministic rule for python. Read its implementation.
Definition
Section titled “Definition”Report an ordinary try whose protected body is exactly one value-returning statement and
whose sole handler catches one non-tuple exception type and returns None. Replace the whole
statement with a contextlib.suppress block that keeps the successful return and an explicit
fallback return. This states the nullable result without a handler whose only action is the
absence of a value.
Evidence
Section titled “Evidence”Retain the complete try, its one protected return, the caught expression, and the handler’s
one return None. The emitted value is the number of exact regions in each file.
Exceptions
Section titled “Exceptions”Keep exception groups, tuple catches, bound exception aliases, multiple handlers, else or
finally clauses, multiple protected statements, multiple handler statements, multiline
returns, and handlers that perform any action besides returning None. Those shapes need a
wider control-flow proof and receive no suggestion from this rule.
Examples
Section titled “Examples”A try holding return parse(value) followed by an except ValidationError holding
return None becomes a with suppress(ValidationError) guarded return followed by
return None.
References
Section titled “References”- Adapts Ruff SIM105 suppressible-exception. Open reference
- Cites “The Python Standard Library”, contextlib suppress. Open reference