HeadlinesBriefing favicon HeadlinesBriefing.com

py-sql-cleaner: New CLI Tool Formats Embedded SQL in Python Files

Hacker News •
×

A new command-line utility called py-sql-cleaner addresses a common pain point in Python codebases where developers write lengthy SQL queries inside triple-quoted strings. Unlike standard Python formatters that ignore embedded SQL, or SQL formatters that expect standalone .sql files, this tool specifically targets and reformats SQL embedded within Python code.

Built on SQLGlot, the formatter defaults to generic SQL dialect but supports database-specific modes including MySQL, PostgreSQL, and Redshift. The tool intelligently skips unsafe blocks—f-strings, Jinja templates, and parameterized queries using %s or :name syntax—avoiding potential runtime issues. This conservative approach preserves runtime behavior while cleaning up readable SQL.

Developers can list detected SQL blocks, preview formatting changes with --dry-run, apply formatting in place, or extract queries into separate .sql files. The tool also includes a check command for CI pipelines to enforce formatting standards across teams.

Currently an early MVP, py-sql-cleaner focuses exclusively on triple-quoted strings rather than single-line SQL. Installation options include PyPI, pipx, or direct execution via uvx without installation. The project represents a practical solution for teams managing complex analytical queries in their Python codebases.