SQLTy vs asyncpg_typed¶
Both give asyncpg queries Python types. With
asyncpg_typed you declare the argument and row types
by hand, sql(query, args=tuple[int], resultset=Row), and each call checks them against what
PostgreSQL reports when it prepares the statement. SQLTy infers the types from your schema files
and checks them statically, before the code runs and without a database.
Quick comparison¶
| asyncpg_typed 0.1.7 | SQLTy 0.10 | |
|---|---|---|
| Types come from | Annotations you write | Casts or a schema; sqlty rewrite writes them |
| Checked | At runtime, on every call, against the live server | Statically, with no database |
| Databases | PostgreSQL through asyncpg | PostgreSQL, MySQL, SQLite; DuckDB best effort |
| Nullability | Not reported by prepared statements: an unexpected NULL raises NoneTypeError |
Inferred from NOT NULL, joins and expressions |
| Parameters | Declared; the type checker checks *args |
Typed from where they sit, checked at call sites; SQL[R, P] with param-types |
| Rows seen by the type checker | list[T], T \| None, R1 |
T through the sqlty.drivers.asyncpg helpers; a plain conn.fetch returns asyncpg's untyped Record |
| Row types | tuple, NamedTuple |
TypedDict, NamedTuple, dataclass, Pydantic, Protocol |
| Column names | NameMismatchError for a NamedTuple |
column-name-mismatch |
| Enums | StrEnum values compared with pg_enum |
A Literal of the values; StrEnum fields accepted without comparing values |
| JSON | JsonType, decoded with json or orjson |
str with driver = "asyncpg", as asyncpg loads it |
| Ranges, geometric types | asyncpg.Range[T], asyncpg.Point |
asyncpg.Range[T], asyncpg.Point and the like, with driver = "asyncpg" |
| t-strings | {1} is the ordinal of $1 |
{value} is a parameter |
| SQL built at runtime | unsafe_sql takes any str, checked when it runs |
Not checked |
Where SQLTy is stronger¶
- Inference. You never write the types by hand, and
column-type-mismatchandnullable-mismatchflag a hand-written type that no longer fits the query. - Static. Mistakes surface in the editor and CI, not on the first run, and tests don't need a PostgreSQL server.
- Nullability is inferred, so a missing
| Noneis reported before aNULLarrives. - No per-call cost. asyncpg_typed prepares each statement again on every call, and queries
pg_enumtoo when a column is an enum. - Other databases and drivers, and row types other than tuples.
Where asyncpg_typed is stronger¶
- Typed rows from asyncpg's own methods.
conn.fetchreturnsT, while SQLTy types rows only through its helpers,fetch(conn, query, ...). - The live server is checked, so schema drift in production is caught at runtime.
StrEnumvalues are compared with the database enum.
Which should you choose?¶
Choose asyncpg_typed if you are on asyncpg only, want typed results from asyncpg's own methods, and can afford a prepare on each call.
Choose SQLTy if you want mistakes caught before the code runs, with no database in CI, or you use psycopg, sqlite3 or MySQL drivers.
Use both: SQLTy checks the SQL and parameters statically, and asyncpg_typed checks the result
types against production at runtime. Don't wrap asyncpg_typed's sql(...) literals in SQL[T],
because its parameter is a LiteralString.
What SQLTy is missing¶
- Typed rows from asyncpg's own methods:
conn.fetchreturnsRecord, so typed rows need thesqlty.drivers.asyncpghelpers. - Comparing
StrEnumvalues withCREATE TYPE ... AS ENUM. - A check against the live database.
Common questions¶
Does SQLTy check asyncpg calls?¶
Yes. The parameters of fetch, fetchrow, fetchval, execute, executemany and fetchmany
are checked against the query, and the sqlty.drivers.asyncpg helpers (fetch, fetchrow,
fetchval, execute) return typed rows. asyncpg takes only an exact str, so pass an SQL[T]
through those helpers.
Sources¶
- asyncpg_typed 0.1.7 (PyPI, 2026-12-01)
- Run hands-on with sqlty 0.4.0 (2026-11-07); facts rechecked against the SQLTy 0.10.0 docs (2026-12-01)