Exception Must Inherit a Base

ID

python.exception_inherit

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Python

Tags

reliability, suspicious-construct

Description

Reports classes whose name ends with Error, Exception, or Warning but which do not inherit from any base class. In Python every exception must derive (transitively) from BaseException; a class that lacks any base is a TypeError the moment someone tries to raise it.

class FooError:                    # FLAW — not raisable
    pass

class FooException:                # FLAW — same problem
    pass

class FooError(Exception):         # OK
    pass

class FooError(ValueError):        # OK — inherits transitively
    pass

Rationale

The bug surfaces only at the first raise FooError(…​) — sometimes long after the class was added. Static detection turns a runtime crash into a code review.

The rule is conservative: it fires only on classes whose name follows the standard exception-suffix conventions (*Error, *Exception, *Warning). Classes intended to be exceptions but named differently are out of scope.

Remediation

Add an explicit base. Exception is the conventional choice for new exception types; pick a more specific base (ValueError, IOError, …​) when the new exception is a refinement of an existing one.

class FooError(Exception):
    pass