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 context managers to manage cleanups #29

Merged
merged 4 commits into from
Aug 13, 2023
Merged

Use context managers to manage cleanups #29

merged 4 commits into from
Aug 13, 2023

Conversation

hynek
Copy link
Owner

@hynek hynek commented Aug 13, 2023

Makes us better Python citizens and allows to pass context managers like sqlalalchemy.Engine.connect directly as factories.

Only downside is that we can't detect the async-ness of factories anymore because an async context manager function is a sync function that returns an async object…

Is this what you had in mind @RonnyPfannschmidt?

@RonnyPfannschmidt
Copy link

At first glance,this Looks like it

@hynek hynek merged commit 26c4845 into main Aug 13, 2023
@hynek hynek deleted the contextmanagers branch August 13, 2023 08:09
@hynek
Copy link
Owner Author

hynek commented Aug 13, 2023

thanks for the quick feedback!

@RonnyPfannschmidt
Copy link

Asyncness is still detectable, by the incoming function Type

Additionally normal Generators Are not awaitabe

@hynek
Copy link
Owner Author

hynek commented Aug 13, 2023

Asyncness is still detectable, by the incoming function Type

How? The usual ways using iscoroutinefunction and issyncgenfunction both return False.

@hynek
Copy link
Owner Author

hynek commented Aug 14, 2023

Also fun fact, I've run into problems immediately with this approach and had to add an enter option to registration: 396f6df 😅

@RonnyPfannschmidt
Copy link

It May be a good idea to separate factories and contexts on the registration level

@hynek
Copy link
Owner Author

hynek commented Aug 14, 2023

That's not really the problem (so far). It's just that independently from how factories represented internally, a factory may return a context manager and the question is what to do with it.

Just return it? Means you have to write boilerplate like this for all of them:

def factory():
    with Engine.connect() as conn:
        yield conn

This was the previous behavior.

If you always enter them, you lose control over when they entered. My personal example where that did something I didn't want is my unit of work transaction manager.


It's really just a trade-off between convenience and surprise whether enter should be True or False by default.

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.

2 participants