Module

routing.router

Compiled router with trie-based path matching.

Routes are registered during setup and compiled into an immutable lookup structure when the app freezes.

Classes

_TrieNode 1
A node in the route trie. Mutable during compilation only.

A node in the route trie. Mutable during compilation only.

Methods

Internal Methods 1
__init__ 0
def __init__(self) -> None
_ParamEdge 4
A parameter edge in the trie.

A parameter edge in the trie.

Attributes

Name Type Description
param_name str
param_type str
regex re.Pattern[str]
node _TrieNode
_CatchAllEdge 2
A catch-all (path) edge — consumes remaining path.

A catch-all (path) edge — consumes remaining path.

Attributes

Name Type Description
param_name str
route_by_method dict[str, Route]
Router 7
Compiled router with trie-based path matching. Usage:: router = Router() router.add(Route…

Compiled router with trie-based path matching.

Usage::

router = Router()
router.add(Route("/users", handler, frozenset({"GET"})))
router.add(Route("/users/{id:int}", handler, frozenset({"GET"})))
router.compile()
match = router.match("GET", "/users/42")

Methods

routes 0 list[Route]
Return all registered routes. Traverses the trie to collect every unique Route…
property
def routes(self) -> list[Route]

Return all registered routes.

Traverses the trie to collect every unique Route object. Useful for introspection and contract validation.

Returns
list[Route]
add 1
Add a route to the router. Must be called before compile().
def add(self, route: Route) -> None
Parameters
Name Type Description
route
compile 0
Freeze the router. No more routes can be added.
def compile(self) -> None
match 2 RouteMatch
Match a request path and method against compiled routes. Returns a ``RouteMatc…
def match(self, method: str, path: str) -> RouteMatch

Match a request path and method against compiled routes.

Returns aRouteMatchon success. RaisesNotFoundif no route matches the path. RaisesMethodNotAllowedif the path matches but the method doesn't.

Parameters
Name Type Description
method
path
Returns
RouteMatch
Internal Methods 3
__init__ 0
def __init__(self) -> None
_collect_routes 3
Recursively collect routes from the trie.
def _collect_routes(self, node: _TrieNode, seen: set[int], result: list[Route]) -> None
Parameters
Name Type Description
node
seen
result
_match_node 4 tuple[_TrieNode, dict[st…
Recursively match path parts against the trie.
def _match_node(self, node: _TrieNode, parts: list[str], index: int, params: dict[str, str]) -> tuple[_TrieNode, dict[str, str]] | None
Parameters
Name Type Description
node
parts
index
params
Returns
tuple[_TrieNode, dict[str, str]] | None

Functions

parse_path 1 list[PathSegment]
Parse a route path string into segments. Examples:: "/users" -> …
def parse_path(path: str) -> list[PathSegment]

Parse a route path string into segments.

Examples::

"/users"          -> [PathSegment("users")]
"/users/{id}"     -> [PathSegment("users"), PathSegment("{id}", is_param=True, ...)]
"/users/{id:int}" -> [PathSegment("{id:int}", is_param=True, param_type="int")]
"/files/{path:path}" -> [PathSegment("{path:path}", is_param=True, param_type="path")]
Parameters
Name Type Description
path str
Returns
list[PathSegment]