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. Uses hardcoded settings: PostgreSQL dialect, uppercase keywords/literals, and 88-character line length.

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/

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

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
  -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.2.1.tar.gz (5.6 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.2.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file sqlfluff_tstring-0.2.1.tar.gz.

File metadata

  • Download URL: sqlfluff_tstring-0.2.1.tar.gz
  • Upload date:
  • Size: 5.6 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.2.1.tar.gz
Algorithm Hash digest
SHA256 00107d20ea6751c082197a5d6dedcab559f373046c6e66a77117c780d8a11905
MD5 c28ef70093563517099048973e75dd6c
BLAKE2b-256 502ecefefe917287a9d6b70ea4a513e0472701ad0df0438e42e47ac46735712e

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlfluff_tstring-0.2.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.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlfluff_tstring-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7fcac4f4c45607f72cc457b54306f0008fd0b0d04d12e0fcd357b8808996a956
MD5 32a2fe1102deeb550fc97e691b1711ce
BLAKE2b-256 b485011c92bdbb8d5a53fc9c2595aeab5ae5a05fbbee3c8128449b580d79ae2c

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlfluff_tstring-0.2.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