Skip to main content

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"...") and obj.sql(t"...") call patterns
  • Respects your .sqlfluff configuration 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

sqlfluff_tstring-0.1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlfluff_tstring-0.1.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

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

Hashes for sqlfluff_tstring-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b5030c7d58e505dd6132ae92ee567ba6210df7acc290dd8973aba65733d65add
MD5 cd78f1865d5f1abbcdfee121f018b2f5
BLAKE2b-256 5b2b10d07865f83e5344473cc479a86e938853e8a6b0e38db5e18c9f2f620511

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlfluff_tstring-0.1.1.tar.gz:

Publisher: publish.yaml on kzuraw/sqlfluff-tstring

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlfluff_tstring-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlfluff_tstring-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8df0604ee5853dae33066b07bdba6120fa9b0265dc07a100ee3777a304577bc9
MD5 9e1227ee88974000f5721260f24e5c65
BLAKE2b-256 9cee66b339ea9bc753bc459a6bf9a90c69ef475e52467b9f09e096b0451b70d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlfluff_tstring-0.1.1-py3-none-any.whl:

Publisher: publish.yaml on kzuraw/sqlfluff-tstring

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page