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

Add lifespan context v3 #5503

Closed

Conversation

JonathanPlasse
Copy link
Contributor

rebased @uSpike's work from #2944 addressing #2943

@codecov
Copy link

codecov bot commented Oct 16, 2022

Codecov Report

Patch coverage: 100.00% and no project coverage change

Comparison is base (cf73051) 100.00% compared to head (302adda) 100.00%.

❗ Current head 302adda differs from pull request most recent head 0ef7cfa. Consider uploading reports for the commit 0ef7cfa to get more accurate results

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     
Impacted Files Coverage Δ
fastapi/applications.py 100.00% <ø> (ø)
fastapi/routing.py 100.00% <ø> (ø)
tests/test_router_events.py 100.00% <100.00%> (ø)
tests/test_datastructures.py 100.00% <0.00%> (ø)

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.
📢 Do you have feedback about the report comment? Let us know in this issue.

@github-actions
Copy link
Contributor

📝 Docs preview for commit 807f811 at: https://634c221ee1f2e413b23dcacc--fastapi.netlify.app

Copy link
Contributor

@JarroVGIT JarroVGIT left a 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).

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

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?

Copy link
Contributor Author

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.


def test_app_lifespan_state(state: State) -> None:
@asynccontextmanager
async def lifespan(app: Starlette) -> AsyncIterator[None]:
Copy link
Contributor

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.

Copy link
Sponsor Collaborator

Choose a reason for hiding this comment

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

ref in code:

) -> AsyncGenerator[_T, None]:

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor Author

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.

@JonathanPlasse
Copy link
Contributor Author

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

This is the 3rd pull request made to add lifespan to FastAPI.

@github-actions
Copy link
Contributor

📝 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,
Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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

@github-actions
Copy link
Contributor

📝 Docs preview for commit 8c47d6a at: https://6352ffe1a3b96c66122559fc--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 1df567d at: https://6353a7059f22b0626235cbf9--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 302adda at: https://6353ad93c6297166c53d82dc--fastapi.netlify.app

@JonathanPlasse
Copy link
Contributor Author

All requested changes have been made.

@github-actions
Copy link
Contributor

📝 Docs preview for commit 62b8340 at: https://63736bd69dcd6b37faffe1b1--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit c5c9b64 at: https://63837abfda05a672ca4a4c33--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 87f9445 at: https://639ce70189c20f052ba4cf44--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 2c05463 at: https://63c6b375d289a546e6b326ee--fastapi.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2023

📝 Docs preview for commit 0ef7cfa at: https://640623f5c1f3f70f5eff81ff--fastapi.netlify.app

@tiangolo
Copy link
Owner

tiangolo commented Mar 7, 2023

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 0.93.0, released in the next few hours. 🎉

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@tiangolo tiangolo closed this Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants