Open-source SQLAlchemy tooling

Tooling that feels native to SQLAlchemy.

Django developers get assertNumQueries, nplusone, and the Debug Toolbar. SQLAlchemy developers get told to write their own event listener. These are the missing pieces — small, sharply scoped, and held to a hard quality bar.

Packages

queryspy

N+1 and query-budget detection for SQLAlchemy 2.0 — sync and async.

N+1 detected: 11 queries for User.addresses (lazy load)
  triggered from app/services/users.py:28 in list_users()
  SELECT address.id AS address_id, address.email AS address_email, ...
  fix: .options(selectinload(User.addresses))

Point it at the test suite you already have. pytest --queryspy-strict fails any test that triggers an N+1 and names the line of your code responsible, with a fix you can paste. Async included — attribution walks the greenlet chain SQLAlchemy runs lazy loads inside.

pytest-sqla

Per-test SQLAlchemy 2.x sessions that roll back — sync and async.

def test_signup(sqla_session):
    signup(sqla_session, "ada@example.com")  # commits, as production code does
    assert sqla_session.scalar(select(func.count(User.id))) == 1
    # ...and the next test starts from an empty table again.

Every test gets a connection, session and sessionmaker inside one outer transaction, so your code can commit() and rollback() as it does in production and everything is undone at teardown. Async sessions open on the test's own event loop, SQLite gets SQLAlchemy's savepoint fix applied for you, and a commit that escapes fails the test that made it instead of leaking into the next one.

More is planned. Nothing is announced until it ships.

Principles

Public API only
No monkeypatching of SQLAlchemy internals. That is precisely why the previous generation of these tools stopped working instead of being ported.
A false positive is worse than a missed detection
A tool that flags correct code gets uninstalled the same day. False-positive suites are release gates here, weighted equally with detection.
Measure, don't assume
Behaviour claims in these repos are backed by scripts that were actually run against a real session. Two of queryspy's design decisions reversed once measured.
Minimal dependencies
Every package declares the smallest runtime closure it can defend, and the release gate audits that closure rather than the development tree.