Skip to content

Latest commit

 

History

History
129 lines (86 loc) · 2.63 KB

File metadata and controls

129 lines (86 loc) · 2.63 KB

Flynt - String Formatting Converter

Flynt is a tool that automatically converts old-style Python string formatting to f-strings, which are more readable and efficient.

Overview

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

Installation

Flynt is included as a development dependency:

# Install with other development dependencies
uv sync --dev

To install it directly:

uv pip install flynt

How It's Used in This Project

In this project, Flynt is used to:

  1. Automatically convert old-style string formatting to f-strings
  2. Maintain consistent string formatting across the codebase
  3. Run as part of the pre-commit hooks and CI/CD pipeline

Configuration in This Project

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

Basic Usage

Running Flynt

To run Flynt on the project:

# Run via poethepoet
uv run poe flynt

# Run directly
uv run flynt --aggressive src tests

Common Command-Line Options

# 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/*" src

Examples

Before and After Examples

.format() Style

Before:

name = "World"
greeting = "Hello, {}!".format(name)

After:

name = "World"
greeting = f"Hello, {name}!"

% Style

Before:

name = "World"
greeting = "Hello, %s!" % name

After:

name = "World"
greeting = f"Hello, {name}!"

Best Practices

  1. Run Flynt regularly: Include Flynt in your pre-commit hooks to ensure consistent string formatting.
  2. Use f-strings for new code: Write new code using f-strings directly.
  3. Review conversions: Some complex string formatting might need manual review after conversion.
  4. Combine with other formatting tools: Use Flynt alongside tools like Ruff for comprehensive code formatting.

Resources