]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
🎨 Fix types for lifespan, upgrade Starlette to 0.26.1 (#9245)
authorSebastián Ramírez <tiangolo@gmail.com>
Tue, 14 Mar 2023 02:19:04 +0000 (03:19 +0100)
committerGitHub <noreply@github.com>
Tue, 14 Mar 2023 02:19:04 +0000 (03:19 +0100)
fastapi/applications.py
fastapi/routing.py
pyproject.toml

index 3305259365295e6495401aa6b497f73539822b89..8b3a74d3c833d7fb7d91c15080afea550b157e8a 100644 (file)
@@ -9,6 +9,7 @@ from typing import (
     Optional,
     Sequence,
     Type,
+    TypeVar,
     Union,
 )
 
@@ -43,10 +44,12 @@ from starlette.responses import HTMLResponse, JSONResponse, Response
 from starlette.routing import BaseRoute
 from starlette.types import ASGIApp, Lifespan, Receive, Scope, Send
 
+AppType = TypeVar("AppType", bound="FastAPI")
+
 
 class FastAPI(Starlette):
     def __init__(
-        self,
+        self: AppType,
         *,
         debug: bool = False,
         routes: Optional[List[BaseRoute]] = None,
@@ -71,7 +74,7 @@ class FastAPI(Starlette):
         ] = None,
         on_startup: Optional[Sequence[Callable[[], Any]]] = None,
         on_shutdown: Optional[Sequence[Callable[[], Any]]] = None,
-        lifespan: Optional[Lifespan] = None,
+        lifespan: Optional[Lifespan[AppType]] = None,
         terms_of_service: Optional[str] = None,
         contact: Optional[Dict[str, Union[str, Any]]] = None,
         license_info: Optional[Dict[str, Union[str, Any]]] = None,
index c227ea6bae8bc83a94bc4396b996329f17a624f1..06c71bffadd983e94801cbb1b7d040af42f8e603 100644 (file)
@@ -492,7 +492,9 @@ class APIRouter(routing.Router):
         route_class: Type[APIRoute] = APIRoute,
         on_startup: Optional[Sequence[Callable[[], Any]]] = None,
         on_shutdown: Optional[Sequence[Callable[[], Any]]] = None,
-        lifespan: Optional[Lifespan] = None,
+        # the generic to Lifespan[AppType] is the type of the top level application
+        # which the router cannot know statically, so we use typing.Any
+        lifespan: Optional[Lifespan[Any]] = None,
         deprecated: Optional[bool] = None,
         include_in_schema: bool = True,
         generate_unique_id_function: Callable[[APIRoute], str] = Default(
index 2ae582722b54b9ffefe5f263274a806707a53cd7..b8d006359739871c9e87872c4aa0d00d4193a0e3 100644 (file)
@@ -41,7 +41,7 @@ classifiers = [
     "Topic :: Internet :: WWW/HTTP",
 ]
 dependencies = [
-    "starlette>=0.26.0,<0.27.0",
+    "starlette>=0.26.1,<0.27.0",
     "pydantic >=1.6.2,!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0",
 ]
 dynamic = ["version"]