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.2.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.2-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlfluff_tstring-0.2.2.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.2.tar.gz
Algorithm Hash digest
SHA256 f8d530ea72867dfc0f06a1319db12be7bd45280ae5aa013b00ad8f430b3471a2
MD5 ca590e713b5e4c66d0ff7497865cfa39
BLAKE2b-256 5037993219e88afe44715357e8ff2510977c35556cbeaba7a780d92c7f45470c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlfluff_tstring-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4c71fa78b74d719874ec4b1a45b57098dac1ed3f3f57c23f590a03831e577aff
MD5 d0f992ac87ea144995001dbc67976036
BLAKE2b-256 48d79cca62ddb24e5bd1f5bf81997fcc8abcdeb70e2de43c22e931e5c4bbed9e

See more details on using hashes here.

Provenance

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