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

Conversation

adriangb
Copy link
Member

@adriangb adriangb commented Mar 11, 2023

Comment on lines 539 to 547
def test_lifespan_typing():
class App(Starlette):
pass

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

App(lifespan=lifespan)
Copy link
Member Author

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

Copy link
Sponsor Member

@tiangolo tiangolo left a 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: tiangolo/fastapi#9245

@@ -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...

@@ -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")

tests/test_applications.py Show resolved Hide resolved
@Kludex
Copy link
Sponsor Member

Kludex commented Mar 12, 2023

The lifespan parameter on routing.py doesn't get a parameter inside the Lifespan, is it intended? My IDE is more permissive than yours, so I don't have problems there, but knowing your configuration (:laughing:), your IDE is probably complaining over there.

@adriangb
Copy link
Member Author

Good point. We probably have to add an Any

@adriangb adriangb requested a review from Kludex March 12, 2023 18:24
@adriangb adriangb enabled auto-merge (squash) March 12, 2023 18:26
@Kludex Kludex disabled auto-merge March 13, 2023 18:02
Comment on lines +14 to +15
AppType = typing.TypeVar("AppType", bound="Starlette")

Copy link
Member Author

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 👍🏻

Copy link
Sponsor Member

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")
Copy link
Sponsor Member

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

@Kludex Kludex enabled auto-merge (squash) March 13, 2023 18:03
@Kludex Kludex merged commit f640241 into encode:master Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants