Skip to content

PY-SQLA0001 · async_session_expiration_policy

Require explicit non-expiring SQLAlchemy async session factories.

This is a deterministic rule for python. Read its implementation.

Find calls statically resolved to SQLAlchemy async_sessionmaker. Report a factory unless it sets expire_on_commit=False. SQLAlchemy recommends this async setting so ordinary attribute access after commit does not attempt implicit database I/O. A factory expanded through unknown keyword arguments is left unreported because its effective policy cannot be proven.

Each finding points to one factory call whose commit expiration remains enabled or unknown. The value is the number of actionable factories.

Keep expiration only when the application deliberately refreshes or awaits every subsequent access and has tests proving that lifecycle. Direct AsyncSession construction and custom factory wrappers are not inferred by this narrow rule.

Bad

`sessions = async_sessionmaker(engine)` retains the default commit expiration and is
reported.
Good

sessions = async_sessionmaker(engine, expire_on_commit=False) keeps post-commit attributes available without hidden I/O.

  • Cites “SQLAlchemy documentation”, asyncio documentation, preventing implicit I/O. Open reference
  • Cites “SQLAlchemy documentation”, async session factory API. Open reference