diff --git a/fastapi/routing.py b/fastapi/routing.py index 5228c5e20..e28314612 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -1589,7 +1589,7 @@ class _IncludedRouter(BaseRoute): with self._rebuild_lock: if routes_version == self._effective_candidates_version: return self._effective_candidates - new_candidates: list["_EffectiveRouteContext | _IncludedRouter"] = [] + new_candidates: list[_EffectiveRouteContext | _IncludedRouter] = [] candidates = self.original_router.routes for route in candidates: if isinstance(route, _IncludedRouter): @@ -1614,7 +1614,7 @@ class _IncludedRouter(BaseRoute): with self._rebuild_lock: if routes_version == self._effective_low_priority_routes_version: return self._effective_low_priority_routes - new_low_priority: list["_EffectiveRouteContext"] = [] + new_low_priority: list[_EffectiveRouteContext] = [] for route in self.original_router._low_priority_routes: route_context = self._build_effective_context(route) if route_context is not None: diff --git a/tests/test_thread_safety.py b/tests/test_thread_safety.py index aef704bcf..e8dd88dd0 100644 --- a/tests/test_thread_safety.py +++ b/tests/test_thread_safety.py @@ -1,4 +1,5 @@ """Test thread safety of _IncludedRouter cache rebuild.""" + import sys import threading @@ -15,8 +16,10 @@ def test_effective_candidates_thread_safety(): app = FastAPI() router = APIRouter() for i in range(N_ROUTES): + def endpoint(i: int = i): return {"i": i} + router.add_api_route(f"/r{i}", endpoint, methods=["GET"]) app.include_router(router) @@ -42,9 +45,7 @@ def test_effective_candidates_thread_safety(): assert not errors, f"Errors during concurrent requests: {errors}" - included = next( - r for r in app.router.routes if isinstance(r, _IncludedRouter) - ) + included = next(r for r in app.router.routes if isinstance(r, _IncludedRouter)) n_candidates = len(included._effective_candidates) assert n_candidates == N_ROUTES, ( f"Expected {N_ROUTES} cached candidates, got {n_candidates}" @@ -59,9 +60,13 @@ def test_effective_low_priority_thread_safety(): app = FastAPI() router = APIRouter() for i in range(N_ROUTES): + def endpoint(i: int = i): return {"i": i} - router.add_api_route(f"/r{i}", endpoint, methods=["GET"], include_in_schema=False) + + router.add_api_route( + f"/r{i}", endpoint, methods=["GET"], include_in_schema=False + ) app.include_router(router) sys.setswitchinterval(1e-5)