From edc413434269d0b980a0d681341282dfbc2bb98f Mon Sep 17 00:00:00 2001 From: Purushot14 Date: Wed, 12 Mar 2025 20:58:41 +0530 Subject: [PATCH 1/3] Make router and route classes configurable via subclassing - Allow `Starlette` to specify a custom router by overriding `router_class`. - Allow `Router` to specify custom route classes by overriding `route_class` and `websocket_route_class`. - Improve extensibility for custom implementations. - Minor typo fix in Starlette class docstring. - Update .gitignore to exclude IDE (.idea) and virtual environment directories (.venv). --- .gitignore | 2 ++ starlette/applications.py | 8 ++++++-- starlette/routing.py | 11 ++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index bff8fa258..eab648d1b 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ venv*/ .python-version build/ dist/ +.idea +/.venv/ diff --git a/starlette/applications.py b/starlette/applications.py index 6df5a707c..620b67490 100644 --- a/starlette/applications.py +++ b/starlette/applications.py @@ -25,7 +25,11 @@ class Starlette: - """Creates an Starlette application.""" + """Creates a Starlette application.""" + + # This is the default router class used by Starlette. if you want to customized router have to + # override this variable in your subclass. + router_class = Router def __init__( self: AppType, @@ -71,7 +75,7 @@ def __init__( self.debug = debug self.state = State() - self.router = Router(routes, on_startup=on_startup, on_shutdown=on_shutdown, lifespan=lifespan) + self.router = self.router_class(routes, on_startup=on_startup, on_shutdown=on_shutdown, lifespan=lifespan) self.exception_handlers = {} if exception_handlers is None else dict(exception_handlers) self.user_middleware = [] if middleware is None else list(middleware) self.middleware_stack: ASGIApp | None = None diff --git a/starlette/routing.py b/starlette/routing.py index add7df0c2..e4d7a8be9 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -23,7 +23,6 @@ from starlette.types import ASGIApp, Lifespan, Receive, Scope, Send from starlette.websockets import WebSocket, WebSocketClose - class NoMatchFound(Exception): """ Raised by `.url_for(name, **path_params)` and `.url_path_for(name, **path_params)` @@ -576,6 +575,12 @@ def __call__(self: _T, app: object) -> _T: class Router: + + # The default route and websocket route classes. if you want to use customized route classes + # you have to override this class variables in your subclass. + route_class = Route + websocket_route_class = WebSocketRoute + def __init__( self, routes: typing.Sequence[BaseRoute] | None = None, @@ -782,7 +787,7 @@ def add_route( name: str | None = None, include_in_schema: bool = True, ) -> None: # pragma: no cover - route = Route( + route = self.route_class( path, endpoint=endpoint, methods=methods, @@ -797,7 +802,7 @@ def add_websocket_route( endpoint: typing.Callable[[WebSocket], typing.Awaitable[None]], name: str | None = None, ) -> None: # pragma: no cover - route = WebSocketRoute(path, endpoint=endpoint, name=name) + route = self.websocket_route_class(path, endpoint=endpoint, name=name) self.routes.append(route) def route( From c50a1e1de220f58ba44979f4ecc803c6fdd66f90 Mon Sep 17 00:00:00 2001 From: Purushot14 Date: Wed, 12 Mar 2025 21:06:44 +0530 Subject: [PATCH 2/3] linting issue fix --- starlette/routing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/starlette/routing.py b/starlette/routing.py index e4d7a8be9..f2d6b00f1 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -23,6 +23,7 @@ from starlette.types import ASGIApp, Lifespan, Receive, Scope, Send from starlette.websockets import WebSocket, WebSocketClose + class NoMatchFound(Exception): """ Raised by `.url_for(name, **path_params)` and `.url_path_for(name, **path_params)` From 79381685e7b1e6f2aa90745d8acb8adbe4acd168 Mon Sep 17 00:00:00 2001 From: Purushot14 Date: Wed, 12 Mar 2025 21:09:55 +0530 Subject: [PATCH 3/3] linting issue fix --- starlette/routing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/starlette/routing.py b/starlette/routing.py index f2d6b00f1..965af0af0 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -576,7 +576,6 @@ def __call__(self: _T, app: object) -> _T: class Router: - # The default route and websocket route classes. if you want to use customized route classes # you have to override this class variables in your subclass. route_class = Route