Skip to content

feature request - return transaction object from transaction context manager #625

@RmStorm

Description

@RmStorm

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions