-
-
Notifications
You must be signed in to change notification settings - Fork 954
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
Conversation
tests/test_applications.py
Outdated
def test_lifespan_typing(): | ||
class App(Starlette): | ||
pass | ||
|
||
@asynccontextmanager | ||
async def lifespan(app: App) -> AsyncIterator[None]: | ||
yield | ||
|
||
App(lifespan=lifespan) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we actually enforce mypy on our tests, but pylance reports no issues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thank you @adriangb! This looks good.
I just tested this branch locally to confirm it works, and I made the corresponding PR in FastAPI here, I'll merge that and release once this is merged and released: fastapi/fastapi#9245
starlette/applications.py
Outdated
@@ -257,3 +257,6 @@ def decorator(func: typing.Callable) -> typing.Callable: | |||
return func | |||
|
|||
return decorator | |||
|
|||
|
|||
AppType = typing.TypeVar("AppType", bound=Starlette) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
starlette/types.py
Outdated
@@ -1,7 +1,6 @@ | |||
import typing | |||
|
|||
if typing.TYPE_CHECKING: | |||
from starlette.applications import Starlette | |||
T = typing.TypeVar("T") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
T = typing.TypeVar("T") | |
AppType = typing.TypeVar("AppType") |
The lifespan parameter on |
Good point. We probably have to add an Any |
AppType = typing.TypeVar("AppType", bound="Starlette") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work? If so 👍🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
@@ -11,6 +11,8 @@ | |||
from starlette.routing import BaseRoute, Router | |||
from starlette.types import ASGIApp, Lifespan, Receive, Scope, Send | |||
|
|||
AppType = typing.TypeVar("AppType", bound="Starlette") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check if you are fine with this, please. @adriangb
Fixes fastapi/fastapi#9241 (comment)