extra-raises-in-docstring¶
Part of: Enrichment Check
| Check | enrichment |
| Category | recommended |
| Applies to | functions, methods |
| Since | v1.16.0 |
Docstring documents exceptions not raised in the function body
What it detects¶
This rule flags functions and methods whose Raises: section documents exception types that are not actually raised in the function body. It compares documented exception names against raise statements found via scope-aware AST walking.
Why is this a problem?¶
Documenting exceptions that are never raised misleads callers into writing unnecessary try/except blocks. After a refactor that removes or changes exception paths, leftover Raises: entries create false expectations about error handling.
How to Fix¶
Remove the documented exception from the Raises: section, or add the
corresponding raise statement to the function body:
Only document exceptions that the function directly raises. Exceptions from called functions should be documented in their docstrings.
Example¶
Configuration¶
This rule is opt-in and disabled by default due to false positives on propagated exceptions (exceptions raised by callees but documented in the caller's Raises: section). Enable it with:
The forward check (missing-raises) is controlled independently by require-raises and remains enabled by default.
Exceptions¶
This rule is automatically skipped when:
- The function is
@abstractmethod(interface contract documentation) - The function body is a stub (
pass,..., orraise NotImplementedError) - The symbol is a class, module, or other non-function type
- The docstring has no
Raises:section