From c046d33b278ac99bc00607e697755e8c7492b6dc Mon Sep 17 00:00:00 2001 From: essentiaMarco <131397104+essentiaMarco@users.noreply.github.com> Date: Sun, 15 Mar 2026 17:22:43 -0700 Subject: [PATCH] fix: pre-commit and type checks (ruff E731, mypy/ty cli and applications) Made-with: Cursor --- fastapi/applications.py | 9 +++++++-- fastapi/cli.py | 6 +++--- fastapi/dependencies/utils.py | 13 +++++++++---- fastapi/routing.py | 1 - tests/test_dependency_lifespan_scope.py | 5 +++-- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index eb32dde142..f19f7e3bc3 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -1025,8 +1025,13 @@ class FastAPI(Starlette): """ ), ] = {} + _inner_lifespan: Callable[[Any], Any] if lifespan is None: - _inner_lifespan = lambda app: routing._DefaultLifespan(app.router) + + def _default_lifespan(app: Any) -> Any: + return routing._DefaultLifespan(app.router) + + _inner_lifespan = _default_lifespan elif inspect.isasyncgenfunction(lifespan): _inner_lifespan = asynccontextmanager(lifespan) elif inspect.isgeneratorfunction(lifespan): @@ -1050,7 +1055,7 @@ class FastAPI(Starlette): generate_unique_id_function=generate_unique_id_function, strict_content_type=strict_content_type, ) - self.router._fastapi_app = self + self.router._fastapi_app = self # type: ignore[attr-defined] self.exception_handlers: dict[ Any, Callable[[Request, Any], Response | Awaitable[Response]] ] = {} if exception_handlers is None else dict(exception_handlers) diff --git a/fastapi/cli.py b/fastapi/cli.py index fda271a53a..2dd2febdb9 100644 --- a/fastapi/cli.py +++ b/fastapi/cli.py @@ -1,12 +1,12 @@ try: - from fastapi_cli.cli import main as cli_main + from fastapi_cli.cli import main as cli_main # type: ignore[import-not-found] except ImportError: # pragma: no cover - cli_main = None # type: ignore + cli_main = None def main() -> None: - if not cli_main: # type: ignore[truthy-function] # ty: ignore[unused-ignore-comment] + if not cli_main: message = 'To use the fastapi command, please install "fastapi[standard]":\n\n\tpip install "fastapi[standard]"\n' print(message) raise RuntimeError(message) # noqa: B904 diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index b634ebaefd..ab04134c1f 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -328,13 +328,18 @@ def get_dependant( '"request", it cannot depend on dependencies with scope "function".' ) # Lifespan-scoped dependencies can only depend on other lifespan-scoped deps. - if dependant.computed_scope == "lifespan" and param_details.depends.scope not in ( - None, - "lifespan", + if ( + dependant.computed_scope == "lifespan" + and param_details.depends.scope + not in ( + None, + "lifespan", + ) ): assert dependant.call + call_name = getattr(dependant.call, "__name__", "") raise DependencyScopeError( - f'The dependency "{dependant.call.__name__}" has a scope of ' + f'The dependency "{call_name}" has a scope of ' '"lifespan", it cannot depend on dependencies with scope ' f'"{param_details.depends.scope}".' ) diff --git a/fastapi/routing.py b/fastapi/routing.py index c3cbf5afd1..c0746fbe78 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -231,7 +231,6 @@ async def _run_lifespan_dependencies( ) -> None: """Solve all lifespan-scoped dependencies and fill dependency_cache.""" from starlette.requests import Request - from starlette.types import Receive, Send lifespan_deps = _collect_lifespan_dependants(router) if not lifespan_deps: diff --git a/tests/test_dependency_lifespan_scope.py b/tests/test_dependency_lifespan_scope.py index 1d3c06e426..7635949c51 100644 --- a/tests/test_dependency_lifespan_scope.py +++ b/tests/test_dependency_lifespan_scope.py @@ -62,7 +62,7 @@ def test_lifespan_dependency_with_custom_lifespan() -> None: @app.get("/") def root( - pool: Annotated[str, Depends(get_pool, scope="lifespan")] + pool: Annotated[str, Depends(get_pool, scope="lifespan")], ) -> dict[str, str]: return {"pool": pool} @@ -90,7 +90,7 @@ def test_lifespan_dependency_same_instance_across_requests() -> None: @app.get("/") def root( - s: Annotated[object, Depends(get_singleton, scope="lifespan")] + s: Annotated[object, Depends(get_singleton, scope="lifespan")], ) -> dict[str, bool]: return {"is_singleton": len(instances) == 1 and s is instances[0]} @@ -123,6 +123,7 @@ def test_collect_lifespan_dependants_route_level_scope() -> None: def test_lifespan_dependency_synthetic_request_receive_send() -> None: """Lifespan dep that uses Request.receive covers noop_receive during startup.""" + async def lifespan_dep(request: Request) -> str: await request.receive() return "ok"