Refactor routing internals into modular components with stable facade#15210
Open
rajratangm wants to merge 8 commits intofastapi:masterfrom
Open
Refactor routing internals into modular components with stable facade#15210rajratangm wants to merge 8 commits intofastapi:masterfrom
rajratangm wants to merge 8 commits intofastapi:masterfrom
Conversation
Extract APIRouter, APIRoute/APIWebSocketRoute, and handler/util logic into focused modules while preserving `fastapi.routing` public imports via compatibility re-exports and adding routing facade contract tests. Made-with: Cursor
Author
|
Could a maintainer please add the label to this PR so the required label check can pass? This PR is a mechanical routing modularization with compatibility facade/re-exports and no intended API behavior change. Thanks! |
Contributor
📝 Docs previewLast commit e414cbb at: https://a707ba47.fastapitiangolo.pages.dev Modified Pages |
Exercise IntEnum status normalization and Response return-annotation inference to keep routing refactor coverage at required threshold. Made-with: Cursor
Normalize request handler typing in routing utils and explicitly type APIRoute.response_field to satisfy mypy and ty hooks. Made-with: Cursor
Cover both sync and async wrapper paths in request_response to keep aggregate coverage at the repository fail-under threshold. Made-with: Cursor
Exercise get_request_handler legacy dependant signature and execute raw response helper to close remaining uncovered lines seen in coverage-combine. Made-with: Cursor
Use an existing top-level endpoint callable for legacy get_request_handler path coverage so coverage-combine can reach 100%. Made-with: Cursor
Author
|
Could a maintainer please add the label to this PR? The required label check expects one of: breaking, security, feature, bug, refactor, upgrade, docs, lang-all, internal. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
fastapi/routing.py had grown very large and mixed multiple responsibilities (router APIs, route classes, handler factories, low-level wrapper utilities).
This made targeted maintenance, code review, and future refactors harder than necessary.
Goal
Reduce routing complexity without changing public behavior by splitting internals into focused modules while preserving the existing fastapi.routing import surface.
What changed
fastapi/routing.py is now a lightweight compatibility layer that re-exports routing symbols and documents the stability contract.
Public imports still work as before:
APIRouter
APIRoute
APIWebSocketRoute
handler/helper exports used by existing tests and extension patterns
Added explicit all to make export intent and boundaries clearer.
Routing implementation is split into focused files:
fastapi/routing_router.py
APIRouter implementation (large router API surface)
fastapi/routing_routes.py
APIRoute
APIWebSocketRoute
fastapi/routing_handlers.py
request/websocket handler factories
handler config object (RouteHandlerConfig)
fastapi/routing_utils.py
low-level wrapper and utility helpers (lifespan wrappers, serializer helpers, route wrapper helpers)
docs/en/docs/advanced/routing-architecture.md
explains facade vs internal module structure
clarifies intended stability contract
tests/test_routing_facade_contract.py
verifies expected symbols stay exported from fastapi.routing
verifies re-export identity for core classes/helpers
verifies _PING_INTERVAL compatibility exposure
Behavior compatibility notes
This PR is intended as a mechanical/structural refactor:
no intentional API behavior changes
public import paths remain stable through re-exports
compatibility for monkeypatch-driven SSE tests preserved via facade export path
Test evidence
_
_