Format multiline strings with proper indentation β A Python code formatter that formats only the literal string arguments of textwrap.dedent() calls.
format-dedent automatically formats multiline strings inside textwrap.dedent() calls to make them visually match their runtime output. This makes your code more readable while preserving the exact behavior.
Key features:
- π― Surgical precision β Only formats strings inside
dedent()calls, leaves everything else untouched - π Two modes β Format existing dedent strings OR automatically add
dedent()to strings that need it - π Safe β Validates that formatting doesn't change runtime behavior
- π¨ Smart indentation β Aligns content with the visual structure of your code
- π§Ή Clean β Removes trailing whitespace and normalizes spacing
uv tool install format-dedentPreview formatted output without modifying files:
uvx format-dedent yourfile.pyWrite changes to files:
uvx format-dedent yourfile.py --writeFormat multiple files or directories:
uvx format-dedent src/ tests/ --writeAutomatically wrap multiline strings with dedent() calls:
uvx format-dedent yourfile.py --add-dedent --writeThis will:
- Find multiline strings where
dedent(str) == str(no leading indentation to remove) - Wrap them with
dedent()calls - Add
from textwrap import dedentimport if needed
python -m format_dedent [OPTIONS] [FILES/DIRECTORIES]
Options:
-w, --write Write changes to files (default: output to stdout)
--add-dedent Add dedent() calls to multiline strings
-h, --help Show help messageBehavior:
- Default β Output formatted code to stdout (no file modification)
--writeβ Modify files directly and print confirmation
Use format-dedent as a pre-commit hook to automatically format dedent strings before each commit.
Add this to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/15r10nk/format-dedent
rev: v0.1.0 # Use the latest version
hooks:
- id: format-dedentBefore formatting:
import textwrap
def get_sql_query():
return textwrap.dedent("""
SELECT users.name, orders.total
FROM users
JOIN orders ON users.id = orders.user_id
WHERE orders.status = 'complete'
""")Inconsistent indentation inside the string makes it hard to read and understand the actual SQL query structure.
After formatting:
import textwrap
def get_sql_query():
return textwrap.dedent("""
SELECT users.name, orders.total
FROM users
JOIN orders ON users.id = orders.user_id
WHERE orders.status = 'complete'
""")The key insight: The indentation you see in the source code now matches what dedent() returns. When this code runs, dedent() strips the common leading whitespace, and you get properly formatted SQL.
Before:
def get_message():
message = """
Hello World!
This is a message.
"""
return messageAfter running with --add-dedent:
from textwrap import dedent
def get_message():
message = dedent("""
Hello World!
This is a message.
""")
return messageβ What changed:
- Detected that the string has no leading whitespace (left-aligned)
- Wrapped it with
dedent()for consistency - Added the import statement automatically
- Reformatted with proper indentation matching the code structure
Why use dedent here? Even though this string doesn't need dedenting now, using dedent() consistently makes it easier to modify the string later. You can add indentation for readability without affecting the runtime output.
Before:
from textwrap import dedent
def render_html():
return dedent("""
<div class="container">
<h1>Welcome</h1>
<p>This is a paragraph.</p>
</div>
""")After:
from textwrap import dedent
def render_html():
return dedent("""
<div class="container">
<h1>Welcome</h1>
<p>This is a paragraph.</p>
</div>
""")I would like to thank my sponsors. Without them, I would not be able to invest so much time in my projects.
- Parse β Uses Python's AST module to analyze source code
- Find β Locates all
dedent()ortextwrap.dedent()calls with string arguments - Analyze β Determines the appropriate indentation level based on context
- Validate β Ensures
dedent(original) == dedent(formatted)(behavior unchanged) - Replace β Updates the source file with formatted strings
The key insight: Strings are formatted in the source to visually match their runtime output after dedent() processes them. This makes the code more readable without changing behavior.
- Non-destructive β Always validates that
dedent(original) == dedent(formatted) - Preserves behavior β Formatted strings have identical runtime output
- Quote style aware β Maintains your choice of
"""vs''' - Escape handling β Correctly handles backslashes and escape sequences
- Python 3.8+ β Works with modern Python versions
What gets formatted:
- β
Literal strings inside
textwrap.dedent()calls - β
Literal strings inside
dedent()calls (when imported)
What doesn't get formatted:
- β Regular strings (not in dedent calls)
- β F-strings (can't be wrapped with dedent)
- β String concatenations
- β Docstrings at module level
- β All other code (completely untouched)
# Clone the repository
git clone https://github.com/15r10nk/format-dedent.git
cd format-dedent
# Install with development dependencies
pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test file
pytest tests/test_formatter.pyTests use inline-snapshot for snapshot testing.
MIT License - See LICENSE file for details
Contributions are welcome! Please feel free to submit a Pull Request.