Flynt is a tool that automatically converts old-style Python string formatting to f-strings, which are more readable and efficient.
Flynt scans your Python code for string formatting patterns like .format() and % formatting and converts them to modern f-strings. This improves:
- Code readability
- Performance (f-strings are faster)
- Maintainability
Flynt is included as a development dependency:
# Install with other development dependencies
uv sync --devTo install it directly:
uv pip install flyntIn this project, Flynt is used to:
- Automatically convert old-style string formatting to f-strings
- Maintain consistent string formatting across the codebase
- Run as part of the pre-commit hooks and CI/CD pipeline
Flynt is configured as a poethepoet task:
[tool.poe.tasks]
flynt = "flynt --aggressive --fail-on-change --quiet src tests"This configuration:
- Uses aggressive mode to convert more string formats
- Fails if changes are needed (useful for CI)
- Runs quietly to reduce output noise
- Targets both src and tests directories
To run Flynt on the project:
# Run via poethepoet
uv run poe flynt
# Run directly
uv run flynt --aggressive src tests# Convert a specific file
uv run flynt path/to/file.py
# Show diffs of changes
uv run flynt --dry-run src
# Transform multiline strings
uv run flynt --transform-concats src
# Exclude specific files
uv run flynt --exclude "*/migrations/*" srcBefore:
name = "World"
greeting = "Hello, {}!".format(name)After:
name = "World"
greeting = f"Hello, {name}!"Before:
name = "World"
greeting = "Hello, %s!" % nameAfter:
name = "World"
greeting = f"Hello, {name}!"- Run Flynt regularly: Include Flynt in your pre-commit hooks to ensure consistent string formatting.
- Use f-strings for new code: Write new code using f-strings directly.
- Review conversions: Some complex string formatting might need manual review after conversion.
- Combine with other formatting tools: Use Flynt alongside tools like Ruff for comprehensive code formatting.