Skip to content

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.

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.

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.

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.

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.