Skip to content

Fix typing of Lifespan to allow subclasses of Starlette#2077

Merged
Kludex merged 5 commits intoKludex:masterfrom
adriangb:fix-lifespan-typing
Mar 13, 2023
Merged

Fix typing of Lifespan to allow subclasses of Starlette#2077
Kludex merged 5 commits intoKludex:masterfrom
adriangb:fix-lifespan-typing

Conversation

@adriangb
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
Contributor 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

@adriangb adriangb requested review from Kludex and tiangolo March 11, 2023 01:11
Copy link
Copy Markdown
Contributor

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

return decorator


AppType = typing.TypeVar("AppType", bound=Starlette)
Copy link
Copy Markdown
Owner

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
Copy Markdown
Contributor 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
Copy Markdown
Owner

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


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

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

@Kludex
Copy link
Copy Markdown
Owner

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
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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
Copy Markdown
Owner

Choose a reason for hiding this comment

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

yes

from starlette.routing import BaseRoute, Router
from starlette.types import ASGIApp, Lifespan, Receive, Scope, Send

AppType = typing.TypeVar("AppType", bound="Starlette")
Copy link
Copy Markdown
Owner

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 Kludex: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.

3 participants