Add Session context manager
Issue #3736
wontfix
It would be very nice if the Session object had a context manager similar to the following:
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.flush()
Comments (5)
-
repo owner -
repo owner - changed status to wontfix
reopen if this use case can be identified.
-
reporter The following approach is still awkward (although very valid).
session = makesession() with session.transaction: # code
I would find the following to be more intuitive and useful.
with sessionmaker() as session: # code
This may be personal preference.
-
repo owner What happens at the end of that contextmanager ?
-
repo owner also, does it help that you can subclass Session and make any context managers you want ?
- Log in to comment
the usual context manager use w/ Session is:
that's already there (it needs to be in the documentation though). It does a commit() at the end. flush() is awkward because you should already be using autoflush; calling flush() manually is not very common. Does this suit your use case?