It would be useful if the transaction context manager returned it's underlying transaction. The use case I have specifically is that I'm running some tests and it's nice to wrap the entire test in a transaction and rollback, what I have in mind is in pytest a structure like:
async with conn.transaction() as transaction:
yield conn
await transaction.rollback()
currently the __aenter__ method doesn't return anything so this obviously doesn't work which means I have to use a try/finally block like so:
transaction = conn.transaction()
await transaction.start()
try:
yield conn
finally:
await transaction.rollback()
I can imagine that there are other usecases where it's useful to have access to the transaction in a contextmanager. I see that the transaction has a _managed attribute which explicitly disallows this kind of behaviour. What is the reasoning and is it possible to change this? I would be happy to contribute a PR if someone can explain the problem to me.
It would be useful if the transaction context manager returned it's underlying transaction. The use case I have specifically is that I'm running some tests and it's nice to wrap the entire test in a transaction and rollback, what I have in mind is in pytest a structure like:
currently the
__aenter__method doesn't return anything so this obviously doesn't work which means I have to use a try/finally block like so:I can imagine that there are other usecases where it's useful to have access to the transaction in a contextmanager. I see that the transaction has a
_managedattribute which explicitly disallows this kind of behaviour. What is the reasoning and is it possible to change this? I would be happy to contribute a PR if someone can explain the problem to me.