Skip to main content

Python code quality tool with LLM-aware rules, plugin system, and enterprise features

Project description

prefact

PyPI version Python 3.10+ License: Apache-2.0 Code style: black

Automatic Python prefactoring toolkit — detect, fix, and validate common code issues introduced by LLMs and humans alike.

The Problem

img.png

When using LLMs for code generation, they often silently change import paths from absolute to deep relative:

# ❌ LLM introduces this
from ....llm.generator import generate_strategy
from ....loaders.yaml_loader import save_strategy_yaml

# ✅ You wanted this
from planfile.llm.generator import generate_strategy
from planfile.loaders.yaml_loader import save_strategy_yaml

prefact automatically detects, fixes, and validates such issues in a three-phase pipeline.

Features

Rule ID Auto-fix Description
Relative → Absolute imports relative-imports Converts from ....x import y to from pkg.x import y
Unused imports unused-imports Removes imports never referenced in the module
Duplicate imports duplicate-imports Removes the same name imported twice
Wildcard imports wildcard-imports 🔍 Flags from x import *
Unsorted imports sorted-imports 🔍 Flags import blocks not ordered stdlib→3rd-party→local
String concatenation string-concat 🔍 Flags "Hello " + name → suggests f-strings
Print statements print-statements 🔍 Flags debug print() calls
Missing return types missing-return-type 🔍 Flags public functions without return type hints

✅ = auto-fix · 🔍 = scan-only (report)

Examples

The examples/ directory contains comprehensive examples for different use cases:

Example Description
sample-project Realistic project with all issues demonstrated
01-individual-rules Each rule explained with before/after code
02-multiple-rules Combining multiple rules for comprehensive cleanup
03-output-formats Console vs JSON output examples
04-custom-rules Writing your own prefactoring rules
05-ci-cd GitHub Actions, GitLab CI, Azure DevOps configs
06-api-usage Using prefact programmatically from Python

Quick Example

# Try the sample project
cd examples/sample-project
prefact scan --path . --config prefact.yaml
prefact fix --path . --config prefact.yaml

See examples/README.md for a detailed guide to all examples.

Installation

pip install -e .

# with dev dependencies (pytest)
pip install -e ".[dev]"

Quick Start

# Generate config file
prefact init

# List all available rules
prefact rules

# Scan only (no changes)
prefact scan --path ./my_project --package mypackage

# Fix + validate (with backups)
prefact fix --path ./my_project --package mypackage

# Dry-run (show what would change)
prefact fix --path ./my_project --package mypackage --dry-run

# Check a single file
prefact check ./my_project/src/mypackage/core/service.py --package mypackage

# JSON output for CI
prefact fix --path . --format json -o report.json

📚 Want to see prefact in action? Check out our comprehensive examples with real-world scenarios!

Pipeline Architecture

┌─────────┐      ┌─────────┐      ┌────────────┐
│  SCAN   │ ──→  │   FIX   │ ──→  │  VALIDATE  │
│         │      │         │      │            │
│ Detect  │      │ Apply   │      │ Syntax OK? │
│ issues  │      │ fixes   │      │ Regressions│
│ per rule│      │ + backup│      │ preserved? │
└─────────┘      └─────────┘      └────────────┘
  1. Scan — each rule walks the AST / CST and emits Issue objects
  2. Fix — rules with auto-fix transform the source (via libcst for formatting-safe changes)
  3. Validate — post-fix checks: syntax valid, no regressions, import counts preserved

Configuration

Create prefact.yaml (auto-generated via prefact init):

package_name: planfile

include:
  - "**/*.py"

exclude:
  - "**/venv/**"
  - "**/build/**"

rules:
  relative-imports:
    enabled: true
    severity: warning
  unused-imports:
    enabled: true
    severity: info
  duplicate-imports:
    enabled: true
  wildcard-imports:
    enabled: true
    severity: error
  sorted-imports:
    enabled: false
  string-concat:
    enabled: true
  print-statements:
    enabled: true
    options:
      ignore_patterns: ["cli.py", "scripts/"]
  missing-return-type:
    enabled: false

Python API

from pathlib import Path
from prefact.config import Config
from prefact.engine import RefactoringEngine

config = Config(
    project_root=Path("./my_project"),
    package_name="planfile",
    dry_run=False,
    backup=True,
)

engine = RefactoringEngine(config)
result = engine.run()

print(f"Found {result.total_issues} issues")
print(f"Fixed {result.total_fixed}")
print(f"All valid: {result.all_valid}")

Writing Custom Rules

Extend BaseRule and use the @register decorator:

from prefact.rules import BaseRule, register
from prefact.models import Issue, Fix, ValidationResult

@register
class MyCustomRule(BaseRule):
    rule_id = "my-custom-rule"
    description = "Does something useful."

    def scan_file(self, path, source):
        # Return list[Issue]
        ...

    def fix(self, path, source, issues):
        # Return (fixed_source, list[Fix])
        ...

    def validate(self, path, original, fixed):
        # Return ValidationResult
        ...

CI/CD Integration

# GitHub Actions
- name: prefact check
  run: |
    pip install ./prefact
    prefact scan --path . --format json -o prefact-report.json
    prefact fix --path . --dry-run

Running Tests

pip install -e ".[dev]"
pytest -v

License

Apache License 2.0 - see LICENSE for details.

Author

Created by Tom Sapletta - tom@sapletta.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prefact-0.1.20.tar.gz (90.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prefact-0.1.20-py3-none-any.whl (107.6 kB view details)

Uploaded Python 3

File details

Details for the file prefact-0.1.20.tar.gz.

File metadata

  • Download URL: prefact-0.1.20.tar.gz
  • Upload date:
  • Size: 90.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for prefact-0.1.20.tar.gz
Algorithm Hash digest
SHA256 cf10020e15c2fa305cae660f415043df1b1c55e06ab80593bde32328139be2c9
MD5 8bc12ecb6fce8c7652c8b8515b273a40
BLAKE2b-256 f8759cacccdf4c850635e5113e0ac7ff4d57d3d2153a9605a99d5dc16ebcacb8

See more details on using hashes here.

File details

Details for the file prefact-0.1.20-py3-none-any.whl.

File metadata

  • Download URL: prefact-0.1.20-py3-none-any.whl
  • Upload date:
  • Size: 107.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for prefact-0.1.20-py3-none-any.whl
Algorithm Hash digest
SHA256 0b88db84c6da8928c24f659d6b03293b4c8094da4d5354aeaa6b462af9458dd3
MD5 be64448ef39a21d49114d939308dfb7f
BLAKE2b-256 b5e3fa3c6d7ae4324d72b8e5f1cf86b8c5fff6d3617d77225134585bf718b174

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page