You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A powerful Python refactoring and optimization linter that analyzes your code for performance issues, complexity problems, and opportunities for improvement.
3
+
A Python refactoring and optimization linter that uses AST analysis to identify performance issues, complexity problems, and code improvements.
PyRefactor uses Abstract Syntax Tree (AST) analysis to identify code patterns that can be refactored for better performance, readability, and maintainability. It provides actionable insights with severity levels and detailed explanations for each detected issue.
10
-
11
7
## Features
12
8
13
-
-**Multi-threaded Analysis**: Analyze multiple files in parallel for faster results
14
-
-**Configurable Detectors**: Enable or disable specific detectors based on your needs
15
-
-**Severity Levels**: Issues categorized as INFO, LOW, MEDIUM, or HIGH severity
16
-
-**Multiple Output Formats**: Group results by file or severity level
17
-
-**Flexible Configuration**: Configure via `pyproject.toml`, custom config files, or command-line arguments
9
+
-**Multi-threaded Analysis**: Fast parallel file processing
10
+
-**Configurable Detectors**: Enable/disable specific detectors
11
+
-**Severity Levels**: Issues categorized as INFO, LOW, MEDIUM, or HIGH
12
+
-**Flexible Output**: Group by file or severity
18
13
-**Cross-platform**: Works on Windows, macOS, and Linux
19
14
20
15
## Detectors
21
16
22
-
PyRefactor includes the following specialized detectors:
23
-
24
-
### Complexity Detector
25
-
Identifies functions and methods with high cyclomatic complexity that should be refactored for better maintainability.
26
-
27
-
### Performance Detector
28
-
Finds performance anti-patterns including:
29
-
- Inefficient string concatenation in loops
30
-
- Multiple function calls that could be cached
31
-
- Inefficient list operations
32
-
- Unnecessary comprehensions
33
-
34
-
### Boolean Logic Detector
35
-
Detects overcomplicated boolean expressions and suggests simplifications:
36
-
- Complex conditional chains
37
-
- Redundant boolean operations
38
-
- Opportunities for boolean algebra simplification
39
-
40
-
### Loops Detector
41
-
Identifies loop-related issues:
42
-
- Nested loops that could be optimized
43
-
- Loop invariant code that should be hoisted
44
-
- Loops that could be replaced with comprehensions or built-in functions
45
-
46
-
### Duplication Detector
47
-
Finds duplicated code blocks that should be extracted into reusable functions.
48
-
49
-
### Context Manager Detector
50
-
Detects resource-allocating operations that should use `with` statements:
51
-
- File operations (`open()`, etc.)
52
-
- Lock acquisitions
53
-
- Context managers not used properly
54
-
-**Impact**: Prevents resource leaks and ensures proper cleanup
55
-
56
-
### Control Flow Detector
57
-
Identifies unnecessary control flow patterns:
58
-
- Unnecessary `else` after `return` statement
59
-
- Unnecessary `else` after `raise` statement
60
-
- Unnecessary `else` after `break` statement
61
-
- Unnecessary `else` after `continue` statement
62
-
-**Impact**: Reduces nesting and improves code readability
63
-
64
-
### Dictionary Operations Detector
65
-
Finds inefficient or non-idiomatic dictionary operations:
66
-
- Opportunities to use `dict.get(key, default)` instead of if/else
67
-
- Unnecessary `.keys()` calls when iterating
68
-
- Loops that should use `.items()` instead of indexing
69
-
- Opportunities for dictionary comprehensions
70
-
-**Impact**: More Pythonic and performant code
71
-
72
-
### Comparisons Detector
73
-
Detects non-idiomatic comparison patterns:
74
-
- Multiple `==` comparisons that could use `in` operator
75
-
- Comparisons that could be chained (e.g., `a < b < c`)
76
-
- Singleton comparisons with `==` instead of `is` (for `None`, `True`, `False`)
77
-
- Using `type()` instead of `isinstance()` for type checking
78
-
-**Impact**: Cleaner, more idiomatic code
17
+
-**Complexity**: High cyclomatic complexity functions
18
+
-**Performance**: String concatenation in loops, uncached calls, inefficient operations
PyRefactor can be configured using a TOML configuration file. By default, it looks for configuration in `pyproject.toml` or a file specified with `--config`.
**Issue**: `ModuleNotFoundError: No module named 'pyrefactor'`
281
-
282
-
**Solution**: Make sure PyRefactor is installed: `py -m pip install -e .`
283
-
284
-
---
285
-
286
-
**Issue**: Syntax errors when analyzing Python 3.12 or older code
287
-
288
-
**Solution**: PyRefactor requires Python 3.13+. Ensure your environment is using Python 3.13 or newer.
289
-
290
-
---
291
-
292
-
**Issue**: Analysis is slow on large codebases
293
-
294
-
**Solution**: Increase the number of parallel workers: `pyrefactor --jobs 8 src/`
295
-
296
162
## Contributing
297
163
298
-
Contributions are welcome! Please note that this project is under a Commercial Restricted License (CRL). For commercial use, please contact the copyright holder.
299
-
300
-
### Guidelines
164
+
Contributions are welcome! This project is under a Commercial Restricted License (CRL). For commercial use, contact the copyright holder.
301
165
302
-
1. Follow the existing code style (Black, isort)
303
-
2. Add tests for new features
304
-
3. Ensure all tests pass and coverage remains >95%
305
-
4. Update documentation as needed
306
-
5. Run type checking and linting before submitting
166
+
1. Follow existing code style (Black, isort)
167
+
2. Add tests for new features (>95% coverage)
168
+
3. Run type checking and linting
307
169
308
170
## License
309
171
310
-
This project is licensed under the CRL license - see [LICENSE.md](LICENSE.md) for details.
172
+
Licensed under the CRL license - see [LICENSE.md](LICENSE.md) for details.
0 commit comments