Skip to main content

A Python refactoring and optimization linter that analyzes code for performance issues, complexity problems, and opportunities for improvement

Project description

PyRefactor

A Python refactoring and optimization linter that uses AST analysis to identify performance issues, complexity problems, and code improvements.

Python 3.9+

Features

  • Multi-threaded Analysis: Fast parallel file processing
  • Configurable Detectors: Enable/disable specific detectors
  • Severity Levels: Issues categorized as INFO, LOW, MEDIUM, or HIGH
  • Flexible Output: Group by file or severity
  • Cross-platform: Works on Windows, macOS, and Linux

Detectors

  • Complexity: High cyclomatic complexity functions
  • Performance: String concatenation in loops, uncached calls, inefficient operations
  • Boolean Logic: Overcomplicated boolean expressions
  • Loops: Nested loops, invariant code, comprehension opportunities
  • Duplication: Duplicate code blocks
  • Context Manager: Missing with statements for resource operations
  • Control Flow: Unnecessary else after return/raise/break/continue
  • Dictionary Operations: Non-idiomatic dict patterns, missing .get(), unnecessary .keys()
  • Comparisons: Chained comparisons, singleton checks, type() vs isinstance()

Installation

Recommended: Via pip

pip install pyrefactor

Standalone Executable

Download the latest release from the Releases section. No Python installation required.

From Source

git clone https://github.com/tboy1337/PyRefactor.git
cd PyRefactor
pip install -e .

Requirements: Python 3.9+

Usage

# Analyze a file or directory
pyrefactor myfile.py
pyrefactor src/

# Show only medium/high severity issues
pyrefactor --min-severity medium src/

# Group by severity level
pyrefactor --group-by severity src/

# Use more workers for faster analysis
pyrefactor --jobs 8 src/

# Custom configuration file
pyrefactor --config custom.toml src/

Options

  • -c, --config: Configuration file path (default: pyproject.toml)
  • -g, --group-by: Group by file or severity (default: file)
  • --min-severity: Minimum severity to report: info, low, medium, high (default: info)
  • -j, --jobs: Number of parallel workers (default: 4)
  • -v, --verbose: Enable verbose logging
  • --version: Show version

Exit Codes

  • 0 - No issues or only INFO/LOW severity
  • 1 - MEDIUM/HIGH severity issues found
  • 2 - Analysis error (syntax errors, invalid paths)

Configuration

Configure via TOML file (e.g., pyproject.toml):

[tool.pyrefactor]
exclude_patterns = ["__pycache__", ".venv", "build", "dist"]

[tool.pyrefactor.complexity]
max_complexity = 10

[tool.pyrefactor.performance]
enabled = true
min_concatenations = 3
min_duplicate_calls = 3

[tool.pyrefactor.boolean_logic]
enabled = true
min_depth = 3

[tool.pyrefactor.loops]
enabled = true
max_nesting = 3

[tool.pyrefactor.duplication]
enabled = true
min_lines = 5
similarity_threshold = 0.8

[tool.pyrefactor.context_manager]
enabled = true

[tool.pyrefactor.control_flow]
enabled = true

[tool.pyrefactor.dict_operations]
enabled = true

[tool.pyrefactor.comparisons]
enabled = true

Configuration is searched in: --configpyproject.tomlpyrefactor.ini → defaults

CI/CD Integration

Pre-commit Hook

repos:
  - repo: local
    hooks:
      - id: pyrefactor
        name: PyRefactor
        entry: pyrefactor
        language: system
        types: [python]
        args: [--min-severity=medium]

GitHub Actions

name: Code Quality
on: [push, pull_request]

jobs:
  pyrefactor:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: '3.9'
      - run: pip install pyrefactor
      - run: pyrefactor --min-severity medium src/

Contributing

Contributions are welcome! This project is under a Commercial Restricted License (CRL). For commercial use, contact the copyright holder.

  1. Follow existing code style (Black, isort)
  2. Add tests for new features (>95% coverage)
  3. Run type checking and linting

License

Licensed under the CRL license - see LICENSE.md for details.

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

pyrefactor-1.0.6.tar.gz (50.2 kB view details)

Uploaded Source

Built Distribution

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

pyrefactor-1.0.6-py3-none-any.whl (40.7 kB view details)

Uploaded Python 3

File details

Details for the file pyrefactor-1.0.6.tar.gz.

File metadata

  • Download URL: pyrefactor-1.0.6.tar.gz
  • Upload date:
  • Size: 50.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for pyrefactor-1.0.6.tar.gz
Algorithm Hash digest
SHA256 04849ebe6ea219f4b6568455220f0921f8c31d90463337520d764fa96e63d4b3
MD5 44bad463540155aaabeb8d1a014b4d66
BLAKE2b-256 ce857010327bbb4a7ed9e505bb4cf9761dc5f438c27836165c3adffca1277b1e

See more details on using hashes here.

File details

Details for the file pyrefactor-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: pyrefactor-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 40.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for pyrefactor-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 40a470e20f53c8eb0cf5e16e9b924398d806d63364b1fe93a6a05df85682cec1
MD5 8ae06bcd8d8396a0e0bba48ee8ea7328
BLAKE2b-256 df0001e9d1baea9aa516e236bcdf01e6f6fd7895102c9ccfcfb7672919e498cc

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