Module

contracts.routes

Route helpers for contracts checker.

Functions

_normalize_path 1 str
Normalize path for lookup (strip slashes, empty becomes '').
def _normalize_path(path: str) -> str
Parameters
Name Type Description
path str
Returns
str
attr_to_method 2 str
Map a URL-bearing template attribute name to HTTP method.
def attr_to_method(attr: str, method_override: str | None = None) -> str
Parameters
Name Type Description
attr str
method_override str | None Default:None
Returns
str
collect_route_paths 1 dict[str, frozenset[str]]
Map route path to allowed methods.
def collect_route_paths(router: Router) -> dict[str, frozenset[str]]
Parameters
Name Type Description
router Router
Returns
dict[str, frozenset[str]]
path_matches_route 2 bool
Check if URL could match a route pattern.
def path_matches_route(url: str, route_path: str) -> bool
Parameters
Name Type Description
url str
route_path str
Returns
bool
build_route_index 1 tuple[dict[str, tuple[st…
Pre-segment routes for O(1) static and O(parametric) URL matching. Returns (st…
def build_route_index(route_paths: dict[str, frozenset[str]]) -> tuple[dict[str, tuple[str, frozenset[str]]], list[tuple[str, frozenset[str]]]]

Pre-segment routes for O(1) static and O(parametric) URL matching.

Returns (static_routes, parametric_routes). For static URLs use dict lookup; for parametric URLs scan only parametric_routes.

Parameters
Name Type Description
route_paths dict[str, frozenset[str]]
Returns
tuple[dict[str, tuple[str, frozenset[str]]], list[tuple[str, frozenset[str]]]]
find_matching_route 3 tuple[str, frozenset[str…
Find route matching URL. O(1) for static, O(parametric) for parametric.
def find_matching_route(url: str, static_routes: dict[str, tuple[str, frozenset[str]]], parametric_routes: list[tuple[str, frozenset[str]]]) -> tuple[str, frozenset[str]] | None
Parameters
Name Type Description
url str
static_routes dict[str, tuple[str, frozenset[str]]]
parametric_routes list[tuple[str, frozenset[str]]]
Returns
tuple[str, frozenset[str]] | None