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.
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 (thresholded), repeated uncached calls in loops, inefficient operations
- Boolean Logic: Overcomplicated boolean expressions
- Loops: Nested loops, invariant code, comprehension opportunities
- Duplication: Duplicate code blocks
- Context Manager: Missing
withstatements for resource operations - Control Flow: Unnecessary
elseafterreturn/raise/break/continue - Dictionary Operations: Non-idiomatic dict patterns, missing
.get(), unnecessary.keys() - Comparisons: Chained comparisons, singleton checks,
type()vsisinstance()
See docs/RULES.md for the full rule catalog (C001–C006, P001–P007, B001/B004–B007, L001–L004, D001, R001–R016).
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.12+
Status: PyRefactor is currently in Beta (see PyPI classifiers). APIs and rule behavior may change between releases.
License: This project uses the Commercial Restricted License (CRL). Personal and non-commercial use is permitted; commercial use requires permission from the copyright holder.
See docs/CONFIGURATION.md for full configuration reference (TOML and INI formats).
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 byfileorseverity(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 MEDIUM/HIGH severity issues (INFO/LOW only). Per-file syntax or parse errors are reported in output but do not change the exit code.1- MEDIUM/HIGH severity issues found2- Configuration, path, or orchestration error (invalid paths, missing config, no Python files to analyze)
Configuration
Configure via TOML file (e.g., pyproject.toml):
[tool.pyrefactor]
exclude_patterns = ["__pycache__", ".venv", "build", "dist"]
[tool.pyrefactor.complexity]
enabled = true
max_cyclomatic_complexity = 10
max_branches = 10
max_nesting_depth = 3
max_function_lines = 50
max_arguments = 5
max_local_variables = 15
[tool.pyrefactor.performance]
enabled = true
min_concatenations = 3
min_duplicate_calls = 3
[tool.pyrefactor.boolean_logic]
enabled = true
max_boolean_operators = 3
[tool.pyrefactor.loops]
enabled = true
[tool.pyrefactor.duplication]
enabled = true
min_duplicate_lines = 5
similarity_threshold = 0.85
[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: --config → pyproject.toml → pyrefactor.ini → defaults
INI configuration (pyrefactor.ini)
[complexity]
enabled = true
max_cyclomatic_complexity = 10
[duplication]
enabled = true
min_duplicate_lines = 5
similarity_threshold = 0.85
[general]
exclude_patterns = __pycache__, .venv, build, dist
See docs/CONFIGURATION.md for the complete configuration reference.
Note: The PyPI package version (pyproject.toml) may differ from GitHub release build numbers used for standalone executables.
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.12'
- 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.
Development
Install the package with development dependencies:
pip install -e ".[dev]"
Alternatively:
pip install -e .
pip install -r requirements-dev.txt
Run the local verification script (formatting, type checks, lint, security scan, tests):
python scripts/verify.py
On Windows you can also use py scripts/verify.py. The script uses sys.executable and absolute paths so it behaves the same on Windows, macOS, and Linux.
Run tests directly:
pytest
- Follow existing code style (Black, isort)
- Add tests for new features (>90% coverage)
- Run type checking and linting
License
Licensed under the CRL license - see LICENSE.md for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyrefactor-1.0.12.tar.gz.
File metadata
- Download URL: pyrefactor-1.0.12.tar.gz
- Upload date:
- Size: 64.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c433f6973344abb1c1b13c71b88d139ac7fb6b6c463a49c80308cdbeb8bd191f
|
|
| MD5 |
2ec198cb09a3601fa576f6bf7bd6b869
|
|
| BLAKE2b-256 |
4ca9ef2d1835c3dc580e2c39973c340397fd94b74654281fd437f69e86849c98
|
File details
Details for the file pyrefactor-1.0.12-py3-none-any.whl.
File metadata
- Download URL: pyrefactor-1.0.12-py3-none-any.whl
- Upload date:
- Size: 44.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
415aa328e95a75f0c75be123835f7b26218811d0815c07426735fb0cc4f4d235
|
|
| MD5 |
f0e12e143bdc4796d1b84d054c068f06
|
|
| BLAKE2b-256 |
888ca887c5309c9381568a84da00c0d76504c8535524e6e0c558d0264219dad7
|