Auto-format SQL inside Python t-strings using sqlfluff
Project description
sqlfluff-tstring
Auto-format SQL inside Python t-strings using sqlfluff.
Finds sql(t"...") calls in .py files and formats the embedded SQL, preserving interpolations and respecting your .sqlfluff config.
Requires Python 3.14+ (PEP 750 t-strings).
Installation
pip install sqlfluff-tstring
Usage
# Format files in-place
sqlfluff-tstring src/
# Check mode (exit 1 if changes needed, for CI)
sqlfluff-tstring --check src/
# Show diff without writing
sqlfluff-tstring --diff src/
# Override SQL dialect
sqlfluff-tstring --dialect postgres src/
# Use a specific .sqlfluff config file
sqlfluff-tstring --config path/to/.sqlfluff src/
What it does
Given a file like:
from sql_tstring import sql
query = sql(t"select * from users where id = {uid} and name = {name}")
Running sqlfluff-tstring produces:
from sql_tstring import sql
query = sql(t"""
select * from users
where id = {uid} and name = {name}
""")
- Interpolations (
{uid},{name!r},{val:.2f}) are preserved through formatting - Single quotes auto-upgrade to triple quotes when sqlfluff introduces newlines
- Multiline content in triple-quoted t-strings is wrapped with leading/trailing newlines
- Supports
sql(t"...")andobj.sql(t"...")call patterns - Respects your
.sqlfluffconfiguration for dialect and rules
CLI options
sqlfluff-tstring [OPTIONS] [PATHS...]
positional arguments:
paths Files or directories to format (default: .)
options:
--check Exit 1 if changes needed (CI mode)
--diff Show diff, don't write changes
--config PATH Path to .sqlfluff config file
--dialect DIALECT Override SQL dialect
-v, --verbose Show unchanged files
-q, --quiet Suppress all output
Exit codes: 0 = success/no changes, 1 = changes needed (check mode).
Development
uv sync
uv run pytest
uv run ruff check src/ tests/
uv run ty check src/
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlfluff_tstring-0.1.1.tar.gz.
File metadata
- Download URL: sqlfluff_tstring-0.1.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5030c7d58e505dd6132ae92ee567ba6210df7acc290dd8973aba65733d65add
|
|
| MD5 |
cd78f1865d5f1abbcdfee121f018b2f5
|
|
| BLAKE2b-256 |
5b2b10d07865f83e5344473cc479a86e938853e8a6b0e38db5e18c9f2f620511
|
Provenance
The following attestation bundles were made for sqlfluff_tstring-0.1.1.tar.gz:
Publisher:
publish.yaml on kzuraw/sqlfluff-tstring
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlfluff_tstring-0.1.1.tar.gz -
Subject digest:
b5030c7d58e505dd6132ae92ee567ba6210df7acc290dd8973aba65733d65add - Sigstore transparency entry: 1059763289
- Sigstore integration time:
-
Permalink:
kzuraw/sqlfluff-tstring@5d5d565dadb006906cecb95f9cab61f21a7b2ef6 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/kzuraw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@5d5d565dadb006906cecb95f9cab61f21a7b2ef6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file sqlfluff_tstring-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sqlfluff_tstring-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8df0604ee5853dae33066b07bdba6120fa9b0265dc07a100ee3777a304577bc9
|
|
| MD5 |
9e1227ee88974000f5721260f24e5c65
|
|
| BLAKE2b-256 |
9cee66b339ea9bc753bc459a6bf9a90c69ef475e52467b9f09e096b0451b70d7
|
Provenance
The following attestation bundles were made for sqlfluff_tstring-0.1.1-py3-none-any.whl:
Publisher:
publish.yaml on kzuraw/sqlfluff-tstring
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlfluff_tstring-0.1.1-py3-none-any.whl -
Subject digest:
8df0604ee5853dae33066b07bdba6120fa9b0265dc07a100ee3777a304577bc9 - Sigstore transparency entry: 1059763300
- Sigstore integration time:
-
Permalink:
kzuraw/sqlfluff-tstring@5d5d565dadb006906cecb95f9cab61f21a7b2ef6 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/kzuraw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@5d5d565dadb006906cecb95f9cab61f21a7b2ef6 -
Trigger Event:
release
-
Statement type: