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
Add lifespan context v3 #5503
Add lifespan context v3 #5503
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #5503 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 540 540
Lines 13969 13971 +2
=========================================
+ Hits 13969 13971 +2
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
📝 Docs preview for commit 807f811 at: https://634c221ee1f2e413b23dcacc--fastapi.netlify.app |
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.
I think it's better to import specific type annotations rather than the whole of Starlette
class, just for typing.
Is there a reason you call this 'lifespan context v3'? What does the v3 refer to? This is quite established behaviour in Starlette (by the looks of it; around 3 years in or so).
fastapi/routing.py
Outdated
@@ -41,6 +42,7 @@ | |||
from pydantic.error_wrappers import ErrorWrapper, ValidationError | |||
from pydantic.fields import ModelField, Undefined | |||
from starlette import routing | |||
from starlette.applications import 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.
You only use this import for Typing purposes, wouldn't it make more sense to make use of the more general ASGIApp
type in starlette.types
?
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.
In starlette.routing
the type used is Any.
tests/test_router_events.py
Outdated
|
||
def test_app_lifespan_state(state: State) -> None: | ||
@asynccontextmanager | ||
async def lifespan(app: Starlette) -> AsyncIterator[None]: |
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.
This would be a AsynGenerator
, no🤔? Because of the yield
in this function.
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.
ref in code:
fastapi/fastapi/concurrency.py
Line 19 in f67b19f
) -> AsyncGenerator[_T, None]: |
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.
According to StackOverflow, AsyncIterator
is also correct as a generator is an iterator, so it is correct and also less complex to type.
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.
Every generator is an iterator, but not every iterator is a generator. The point of typing in python is to provide information on the object type as precise as possible. If your take your argument to the extreme, everything could be typed as object
and it would be technically correct, but not really useful.
I don't agree with "less complex to type", it is literally one argument more (as generators have a 'send' type and a 'yield' type)?
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.
You are right, I made the change.
807f811
to
e7c761c
Compare
This is the 3rd pull request made to add lifespan to FastAPI. |
📝 Docs preview for commit e4f1776 at: https://6352ddd0da9e0a4530022b64--fastapi.netlify.app |
@@ -480,6 +481,7 @@ def __init__( | |||
route_class: Type[APIRoute] = APIRoute, | |||
on_startup: Optional[Sequence[Callable[[], Any]]] = None, | |||
on_shutdown: Optional[Sequence[Callable[[], Any]]] = None, | |||
lifespan: Optional[Callable[[Any], AsyncContextManager[Any]]] = None, |
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.
Now it is just inconsistent, no? It's typed FastAPI
on L74 in applications.py, but here it's typed Any
, while I would still argue the correct typing would be the ASGIApp
from starlette.types
. Because, if you have different orders of middleware (which would all be ASGIApp
) you don't have to specifically have a FastAPI
app object in the lifespan param.
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.
Interestingly this is actually how it is typed in Starlette I just noticed. But my remark would remain the same, am I missing something maybe?
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.
Any
could be replaced by FastAPi
which would only be imported if typing.TYPE_CHECKING
is True (during static analysis and not runtime).
📝 Docs preview for commit 8c47d6a at: https://6352ffe1a3b96c66122559fc--fastapi.netlify.app |
📝 Docs preview for commit 1df567d at: https://6353a7059f22b0626235cbf9--fastapi.netlify.app |
1df567d
to
302adda
Compare
📝 Docs preview for commit 302adda at: https://6353ad93c6297166c53d82dc--fastapi.netlify.app |
All requested changes have been made. |
302adda
to
62b8340
Compare
📝 Docs preview for commit 62b8340 at: https://63736bd69dcd6b37faffe1b1--fastapi.netlify.app |
62b8340
to
c5c9b64
Compare
📝 Docs preview for commit c5c9b64 at: https://63837abfda05a672ca4a4c33--fastapi.netlify.app |
📝 Docs preview for commit 87f9445 at: https://639ce70189c20f052ba4cf44--fastapi.netlify.app |
generator lifespans were deprectated in starlette 0.16.0 https://github.com/encode/starlette/releases/tag/0.16.0
87f9445
to
2c05463
Compare
📝 Docs preview for commit 2c05463 at: https://63c6b375d289a546e6b326ee--fastapi.netlify.app |
📝 Docs preview for commit 0ef7cfa at: https://640623f5c1f3f70f5eff81ff--fastapi.netlify.app |
Thank you @JonathanPlasse! 🍰 And thanks for the help @JarroVGIT, @Kludex, @yezz123 🙇 I included your commits in the original PR #2944 and merged that one. So I'll now close this one. This will be available in FastAPI
|
rebased @uSpike's work from #2944 addressing #2943