Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing of Lifespan to allow subclasses of Starlette #2077

Merged
merged 5 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions starlette/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Starlette:
"""

def __init__(
self,
self: "AppType",
debug: bool = False,
routes: typing.Optional[typing.Sequence[BaseRoute]] = None,
middleware: typing.Optional[typing.Sequence[Middleware]] = None,
Expand All @@ -58,7 +58,7 @@ def __init__(
] = None,
on_startup: typing.Optional[typing.Sequence[typing.Callable]] = None,
on_shutdown: typing.Optional[typing.Sequence[typing.Callable]] = None,
lifespan: typing.Optional[Lifespan] = None,
lifespan: typing.Optional[Lifespan["AppType"]] = None,
) -> None:
# The lifespan context function is a newer style that replaces
# on_startup / on_shutdown handlers. Use one or the other, not both.
Expand Down Expand Up @@ -257,3 +257,6 @@ def decorator(func: typing.Callable) -> typing.Callable:
return func

return decorator


AppType = typing.TypeVar("AppType", bound=Starlette)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to the top, or is there an issue with string typing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we can't. It needs to reference Starlette so it has to come after it.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested here, and it works before...

9 changes: 4 additions & 5 deletions starlette/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import typing

if typing.TYPE_CHECKING:
from starlette.applications import Starlette
T = typing.TypeVar("T")
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
T = typing.TypeVar("T")
AppType = typing.TypeVar("AppType")


Scope = typing.MutableMapping[str, typing.Any]
Message = typing.MutableMapping[str, typing.Any]
Expand All @@ -11,8 +10,8 @@

ASGIApp = typing.Callable[[Scope, Receive, Send], typing.Awaitable[None]]

StatelessLifespan = typing.Callable[["Starlette"], typing.AsyncContextManager[None]]
StatelessLifespan = typing.Callable[[T], typing.AsyncContextManager[None]]
StatefulLifespan = typing.Callable[
["Starlette"], typing.AsyncContextManager[typing.Mapping[str, typing.Any]]
[T], typing.AsyncContextManager[typing.Mapping[str, typing.Any]]
]
Lifespan = typing.Union[StatelessLifespan, StatefulLifespan]
Lifespan = typing.Union[StatelessLifespan[T], StatefulLifespan[T]]
13 changes: 12 additions & 1 deletion tests/test_applications.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from contextlib import asynccontextmanager
from typing import Any, Callable
from typing import Any, AsyncIterator, Callable

import anyio
import httpx
Expand Down Expand Up @@ -534,3 +534,14 @@ def get_app() -> ASGIApp:
test_client_factory(app).get("/foo")

assert SimpleInitializableMiddleware.counter == 2


def test_lifespan_typing():
class App(Starlette):
pass

@asynccontextmanager
async def lifespan(app: App) -> AsyncIterator[None]: # pragma: no cover
yield

App(lifespan=lifespan)
adriangb marked this conversation as resolved.
Show resolved Hide resolved