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

use an async context manager factory for lifespan #1227

Merged

Conversation

graingert
Copy link
Member

@graingert graingert commented Jun 30, 2021

return gen_wrapper


def _wrap_lifespan_context(
Copy link
Member Author

Choose a reason for hiding this comment

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

this stuff is a bit of a huge mess I think it's better to just make this a breaking change and make people add contextlib.asynccontextmanager to their functions

Copy link
Member

Choose a reason for hiding this comment

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

I agree...

Copy link
Member Author

Choose a reason for hiding this comment

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

@tomchristie are you happy for this to be a breaking change?

Copy link
Member

Choose a reason for hiding this comment

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

Could you explain what would be necessary for a user to convert their existing lifespan generator to an asynccontextmanager? i.e. an example?

Copy link
Member Author

Choose a reason for hiding this comment

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

You just add the @contextlib.asynccontextmanager decorator

Copy link
Member Author

Choose a reason for hiding this comment

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

@JayH5 thank you so much for this comment! Because of this I worked out I could get rid of all my ugly _wrap_lifespan_context stuff and just use asynccontextmanager(lifespan_context)

@JayH5
Copy link
Member

JayH5 commented Jul 1, 2021

Kinda sorta related (at least involves some similar code): #1205

setup.py Outdated Show resolved Hide resolved
@graingert graingert marked this pull request as ready for review July 3, 2021 10:35
@graingert graingert requested a review from JayH5 July 3, 2021 10:35
Copy link
Member

@JayH5 JayH5 left a comment

Choose a reason for hiding this comment

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

Was going to suggest updating the documentation, but now I see that we don't have docs for the generator lifespan stuff. It therefore seems likely to me that most users are using on_startup/on_shutdown, so the deprecation shouldn't affect too many.

Am happy with this PR and it's a nice clean up.

starlette/routing.py Outdated Show resolved Hide resolved
elif inspect.isasyncgenfunction(lifespan):
warnings.warn(
"async generator function lifespans are deprecated, "
"use an @contextlib.asynccontextmanager function instead",
Copy link
Member

Choose a reason for hiding this comment

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

I now read this like it should be "a contextlib..."

Suggested change
"use an @contextlib.asynccontextmanager function instead",
"use a @contextlib.asynccontextmanager function instead",

Copy link
Member Author

Choose a reason for hiding this comment

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

I read this as an at contextlib.asynccontextmanager

Copy link
Member

@JayH5 JayH5 left a comment

Choose a reason for hiding this comment

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

Nice 👍

setup.py Show resolved Hide resolved
@graingert graingert requested a review from JayH5 July 3, 2021 17:34
@graingert graingert merged commit 537ab6a into encode:master Jul 3, 2021
@graingert graingert deleted the use-a-context-manager-factory-for-lifespan branch July 3, 2021 17:43
@graingert graingert mentioned this pull request Jul 4, 2021
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants