/sqlty:sqlty-rewritefrom sqlty
Wraps --sql query strings as SQL[T] with type inference, strips wrappers, and keeps them in sync after editing. For Python projects using sqlty.
Ships in sqlty. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
sqlty rewrite wraps each --sql string as SQL[T]("--sql\n..."), with T
the inferred row type, and adds the import. sqlty strip removes the wrappers.
Neither changes the string itself.
Run sqlty version: this skill may be newer or older than the installed sqlty.
When it and sqlty --help disagree, trust the command.
--sql and a newline.sqlty rewrite <file>.sqlty check <file>. It must report nothing.user_id = 1
q = "--sql\nSELECT id::INTEGER, name::TEXT FROM users WHERE id = %s"
# rewritten: user_id = 1
# rewritten: q = SQL[tuple[int, str], tuple[typing.Any]]("--sql\nSELECT id::INTEGER, name::TEXT FROM users WHERE id = %s")
rewrite leaves alone| String | Why |
|---|---|
INSERT, UPDATE, DELETE without RETURNING; DDL | returns no rows |
| SQL the parser rejects | no type to infer; sqlty check reports it |
| a column sqlty can't type | an unknown table or function, SELECT * without a schema |
f-strings, +, % | not a literal; pass values as parameters |
Make the column typeable: cast it (x::INTEGER, CAST(x AS INTEGER)) or add the
table to the schema. Don't hand-write a guessed T.
unknown = "--sql\nSELECT x FROM t"
typed = "--sql\nSELECT x::INTEGER FROM t"
# rewritten: unknown = "--sql\nSELECT x FROM t"
# rewritten: typed = SQL[tuple[int]]("--sql\nSELECT x::INTEGER FROM t")
With untyped = "sentinel", a query whose row type is unknown becomes
SQL[UNTYPED], so it is visible and sqlty check --fix types it once it can:
[tool.sqlty]
untyped = "sentinel"
pairs = "--sql\nSELECT x, y FROM pairs"
# rewritten: pairs = SQL[UNTYPED]("--sql\nSELECT x, y FROM pairs")
sqlty rewrite --untyped sentinel does the same for one run.
rewrite never replaces an existing wrapper. After changing the SQL inside
SQL[T](...), run sqlty check --fix: it rewrites every stale SQL[tuple[...]]
and SQL[UNTYPED], and leaves named row types for you to update.
from sqlty import SQL
q = SQL[tuple[int, str]]("--sql\nSELECT id::INTEGER, name::TEXT, email::TEXT FROM users")
# fixed: from sqlty import SQL
# fixed:
# fixed: q = SQL[tuple[int, str, str]]("--sql\nSELECT id::INTEGER, name::TEXT, email::TEXT FROM users")
Don't strip and rewrite a file to update it: strip removes every wrapper,
SQL[User] included, and the next rewrite writes SQL[tuple[...]].
user = SQL[User]("--sql\nSELECT id::INTEGER FROM users")
# stripped: user = "--sql\nSELECT id::INTEGER FROM users"
rewrite and check --fix write the type of the parameters too,
SQL[R, P]: a tuple, or a generated TypedDict for named parameters; a
query without parameters keeps SQL[R]. strip removes them, and a P you
wrote is kept. param-types = false turns it off.
[tool.sqlty]
schema = "schema.sql"
schema.sql:
CREATE TABLE users (id INTEGER NOT NULL, name TEXT);
by_name = "--sql\nSELECT id::INTEGER FROM users WHERE name = %s"
# rewritten: by_name = SQL[tuple[int], tuple[str | None]]("--sql\nSELECT id::INTEGER FROM users WHERE name = %s")
A type checker then rejects what the driver runs (a list for a tuple, extra keys
for a TypedDict), so turn it on when you want that.
| Command | Exits 1 when |
|---|---|
sqlty rewrite --check . | a file would be rewritten |
sqlty rewrite --diff . | a file would be rewritten; prints the diff |
sqlty strip --check . | a file still has wrappers |
For a file whose --sql strings are all plain, strip(rewrite(x)) == x.
import-style changes the import line and the wrapper name:
[tool.sqlty]
import-style = "from sqlty import SQL as Q"
query = "--sql\nSELECT id::INTEGER FROM users"
# rewritten-file: from sqlty import SQL as Q
# rewritten-file: query = Q[tuple[int]]("--sql\nSELECT id::INTEGER FROM users")
A file whose first or second line is # coding: sqlty is rewritten when Python
reads it, so it runs with wrappers that aren't on disk. Type checkers read the
file on disk, so they don't see the types: use it for scripts and notebooks,
and sqlty rewrite for code you commit.
# coding: sqlty
query = "--sql\nSELECT id::INTEGER, name::TEXT FROM users"
# decoded: query = SQL[tuple[int, str]]("--sql\nSELECT id::INTEGER, name::TEXT FROM users")
SyntaxError: unknown encoding: sqlty means the codec isn't registered: in an
editable install, import sqlty.magic before the first such module.
What SKILL.md sets; muted values are Claude Code's defaults
7 skills in this plugin
/sqltyWrite, type and check raw SQL in Python with sqlty. Use when adding or editing a query in a project that uses sqlty (strings starting with --sql, SQL[T] or SQLTemplate[T] wrappers, a [tool.sqlty] section), when generating Python row types for a query, when typing raw SQL in Go as sqlty.SQL[R, P], or when checking that queries still fit a schema migration before deploying. Routes to sqlty-rewrite, sqlty-check, sqlty-breaking, sqlty-ci and sqlty-adopt for each task./sqlty-adoptIntroduce sqlty into an existing Python project with raw SQL queries, one module at a time. Use when a project doesn't use sqlty yet and you are asked to add it, type its SQL queries, configure [tool.sqlty] (dialect, schema from a database dump or migrations), choose which queries to convert first, mark the ones sqlty can't type yet, or add the first CI gate./sqlty-breakingCheck that a schema change or migration doesn't break the queries running during a rolling deploy, with sqlty breaking, and plan an expand and contract migration. Use when adding, dropping, renaming or changing a table or column, writing a migration, making a column nullable or NOT NULL, reading a sqlty breaking finding (backward or forward), or splitting a schema change into safe pull requests./sqlty-checkRun sqlty check and act on its diagnostics, name the row type of a query, and check query parameters, including the typed parameters of SQL[R, P]. Use when sqlty check reports a diagnostic such as [unknown-column] or [narrow-type], when replacing SQL[tuple[...]] with a TypedDict, dataclass, NamedTuple or pydantic model, when a column may be NULL, when a call passes the wrong parameters, when a join inflates a SUM, AVG or COUNT ([fanout-aggregate], [chasm-join]) or compares a foreign key with a column it does not reference or keys of different tables ([foreign-key-mismatch], [entity-mismatch]), when suppressing a diagnostic with a sqlty ignore comment, or when changing a rule's severity in [tool.sqlty.rules]./sqlty-ciSet up or repair sqlty in pre-commit and CI. Use when adding the sqlty-rewrite or sqlty-check pre-commit hooks, writing a GitHub Actions job that runs sqlty rewrite --check, sqlty check and sqlty breaking, a CI run fails on a sqlty step, a hook fails to install (Python 3.14, Rust toolchain), promoting stale-type or other rules to errors, or keeping committed sources free of SQL[T] wrappers./sqlty-serverRun sqlty in an editor with sqlty server, the language server, and the VS Code extension. Use when setting up sqlty in VS Code, Cursor, VSCodium, Neovim, Vim, Helix, Emacs or another LSP client, highlighting SQL in query strings, fixing a query on save with source.fixAll.sqlty, formatting query strings with source.format.sqlty, showing sqlty breaking findings while editing, or when the editor reports nothing or different diagnostics than sqlty check.npx claudepluginhub jirikuncar/sqlty --plugin sqltyOther plugins' skills for the same job