PyNeat: Deep structural code refactoring in Python using LibCST

Posted by AssociateEmotional11@reddit | Python | View on Reddit | 11 comments

Hi r/Python,

I spend a lot of time reviewing code and noticed that while formatters fix styling, they don't fix structural logic flaws. So, I built PyNeat, a CLI tool based on `LibCST` to perform deep structural refactoring.

### What My Project Does

PyNeat scans your Python AST in a single pass and automatically refactors structural anti-patterns while preserving 100% of your original comments and whitespace. It currently fixes:

  1. **The Arrow Anti-pattern:** Flattens deeply nested `if/else` using guard clauses.

  2. **Dangerous Evals:** Safely converts `eval()` to AST literals.

  3. **Empty Excepts:** Detects silent `except: pass` failures.

  4. **Mutable Defaults:** Fixes the infamous `def func(items=[])` memory leak.

  5. **Identity Checks:** Upgrades `== None` to `is None`, and `is 200` to `== 200`.

### Target Audience

This tool is intended for production use by developers, reviewers, and teams dealing with legacy codebases (or quickly generated boilerplate) that need structural clean-up without breaking logic. The current v1.0 MVP is pure Python, but a Rust rewrite (`pyneat-rs`) for massive multi-threading is on the roadmap.

### Comparison

* **vs. Black / Ruff:** Black and Ruff are formatters and linters. They fix whitespace, line length, and warn you about bad code. PyNeat actually *rewrites the logic* (e.g., inverts an `if` condition and injects an early `return` to flatten the code).

* **vs. Built-in `ast` module:** The standard `ast` module drops your comments and whitespace when unparsing. PyNeat uses Instagram's `LibCST` (Concrete Syntax Tree) so the output preserves every single comment and blank line exactly as you wrote it.

**Links:**

* **GitHub:** https://github.com/khanhnam-nathan/Pyneat

* **PyPI:** `pip install pyneat-cli`

I would love to hear your brutally honest feedback on the architecture, the AST traversal approach, or any edge cases I might have missed!