PY-SQLA0001 · async_session_expiration_policy
Require explicit non-expiring SQLAlchemy async session factories.
This is a deterministic rule for python. Read its implementation.
Definition
Section titled “Definition”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.
Evidence
Section titled “Evidence”Each finding points to one factory call whose commit expiration remains enabled or unknown. The value is the number of actionable factories.
Exceptions
Section titled “Exceptions”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.
Examples
Section titled “Examples”Bad
`sessions = async_sessionmaker(engine)` retains the default commit expiration and isreported.
Goodsessions = async_sessionmaker(engine, expire_on_commit=False) keeps post-commit attributes
available without hidden I/O.
References
Section titled “References”- Cites “SQLAlchemy documentation”, asyncio documentation, preventing implicit I/O. Open reference
- Cites “SQLAlchemy documentation”, async session factory API. Open reference