Справочник: analysis¶
Публичные символы слоя analysis. check_raises, RaisesReport, RouteDiscrepancy — из корня; collect_raised — из подпакета analysis.
from fastapi_typed_errors import check_raises, RaisesReport, RouteDiscrepancy
from fastapi_typed_errors.analysis import collect_raised
check_raises¶
fastapi_typed_errors.check_raises
¶
check_raises(
target: FastAPI | APIRouter,
*,
allow_overdeclared: bool = False,
max_depth: int = 10,
) -> RaisesReport
Check that every route's Raises declarations match the errors it can raise.
For each API route the declared set is read from the endpoint's return
annotation and the raised set is collected from the endpoint plus every
dependency (security schemes excluded). Discrepancies are reported in two
directions: undeclared (raised but not declared) and overdeclared
(declared but not found raised).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
FastAPI | APIRouter
|
The FastAPI application or router to check. |
required |
allow_overdeclared
|
bool
|
When |
False
|
max_depth
|
int
|
How many call levels below each body to expand. |
10
|
Returns:
| Name | Type | Description |
|---|---|---|
RaisesReport |
RaisesReport
|
The discrepant routes and the number of routes checked. |
RaisesReport¶
fastapi_typed_errors.RaisesReport
dataclass
¶
RaisesReport(
routes: tuple[RouteDiscrepancy, ...], checked: int
)
Result of checking a whole application or router.
Attributes:
| Name | Type | Description |
|---|---|---|
routes |
tuple[RouteDiscrepancy, ...]
|
The discrepant routes, in registration order. |
checked |
int
|
How many API routes were examined. |
ok
property
¶
Whether every checked route matched its declarations.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
RouteDiscrepancy¶
fastapi_typed_errors.RouteDiscrepancy
dataclass
¶
RouteDiscrepancy(
path: str,
methods: frozenset[str],
undeclared: tuple[type[BaseError[Any]], ...],
overdeclared: tuple[type[BaseError[Any]], ...],
)
One route whose declared errors disagree with the errors found in code.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The full (prefixed) route path. |
methods |
frozenset[str]
|
The route's HTTP methods. |
undeclared |
tuple[type[BaseError[Any]], ...]
|
Errors raised in code but absent from |
overdeclared |
tuple[type[BaseError[Any]], ...]
|
Errors declared via |
collect_raised¶
Движок AST-обхода, общий для check_raises и with_errors(auto=True).
fastapi_typed_errors.analysis.collect_raised
¶
collect_raised(
fn: Callable[..., Any], *, max_depth: int = 10
) -> frozenset[type[BaseError[Any]]]
Find every BaseError subclass a callable can raise, directly or via helpers.
The body is parsed with ast; raise sites and error classes passed as
call arguments (the get_or_404(error=NotFoundError) pattern) are collected,
and called module-level helpers are followed up to max_depth levels deep.
Names are resolved against the function's globals and closure; unresolvable or
unsourceable callables (builtins, C extensions, lambdas) are silently skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[..., Any]
|
The callable to analyze (function, method, |
required |
max_depth
|
int
|
How many call levels below the body to expand (0 = body only). |
10
|
Returns:
| Type | Description |
|---|---|
frozenset[type[BaseError[Any]]]
|
frozenset[type[BaseError[Any]]]: The concrete error classes found. |