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

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlfluff_tstring-0.1.0.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.1.0.tar.gz
Algorithm Hash digest
SHA256 69ddcea7a747429ff473c91630e88fcb29b5b47f8d296ecc093fdb04520d73d1
MD5 4ad5362dc272bb647f76d619c29edcdc
BLAKE2b-256 072a4d013428618646d1be827c2e7ec28909f28ba23b05042eab79ad45206855

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sqlfluff_tstring-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 27d404c7df3ea5699a831f50d02ad44d3b8121fb8f5e00befc47c74791f5ecb5
MD5 6828738f4874769d01e1bbd06e57c44a
BLAKE2b-256 b64e4be5545d136758d8ff24d4ee024dc92504f776a95eaf7699961393234c51

See more details on using hashes here.

Provenance

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