Back to browse
GitHub Repository

Find, format, and safely extract embedded SQL from Python files.

8 starsPython

Py-SQL-cleaner – format SQL embedded in Python strings

by enumura·May 28, 2026·7 points·0 comments

AI Analysis

●●SolidNiche GemSolve My Problem

SQLGlot-powered formatter for Python-embedded SQL with template-aware skipping.

Strengths
  • Conservative detection skips f-strings and Jinja templates by default
  • Extract-to-file feature replaces strings with .sql file references
  • Dialect support for Postgres, MySQL, Redshift preserves database-specific syntax
Weaknesses
  • Early MVP with only 1 GitHub star and 2 open issues
  • No database connection means no validation against actual schemas
Target Audience

Python developers writing SQL queries in code

Similar To

sqlfmt · Black · djlint

Post Description

Hi HN, I built py-sql-cleaner, a CLI for formatting SQL embedded in Python files.

Python formatters handle Python syntax. They do not format SQL written inside Python code. On the other hand, SQL formatters usually target SQL files or raw SQL text, not SQL embedded inside a Python file.

Still, I think it is not uncommon to find long SQL queries inside Python codebases.

py-sql-cleaner detects embedded SQL inside Python files and works only on that SQL. The main things it can do are: find the SQL, format it in place, or extract it into a .sql file.

It avoids rewriting SQL that depends on runtime values or template expansion. For example, SQL containing parameters like %s or :name, or Jinja-style template variables like {{ ds }}, is skipped by default.

Try it with:

uvx py-sql-cleaner list path/to/file.py uvx py-sql-cleaner format path/to/file.py --dry-run

If you write Python, have run into this kind of SQL cleanup problem, or are just curious, I’d be happy if you take a look.

Similar Projects

Infrastructure●●●Banger

IP ranges for 22 cloud providers in 12 formats, updated daily

One-click firewall rules for 22 providers—no more hunting AWS/Azure/GCP feeds separately.

Solve My ProblemSlick
rezmoss
204mo ago