DEV Community

khanhnam-nathan
khanhnam-nathan

Posted on

I got tired of structural anti-patterns, so I built an AST-based code cleaner for Python ๐Ÿงน

Hi DEV community! ๐Ÿ‘‹

I spend a lot of time reviewing Python code. I noticed that while formatters (like Black or Ruff) are amazing at fixing styling and whitespaces, they don't fix structural logic flaws.

If you have a deeply nested if/else block (the Arrow Anti-pattern), or a dangerous eval(), standard formatters will just make that bad logic look pretty.

So, I decided to build PyNeat โ€” a CLI tool based on Instagram's LibCST to perform deep structural refactoring.

๐Ÿš€ What PyNeat Does
PyNeat scans your Python AST in a single pass and automatically refactors anti-patterns. Unlike the built-in ast module, because it uses LibCST, it preserves 100% of your original comments and whitespace.

Currently, the v1.0 MVP fixes:

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

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

Empty Excepts: Detects silent except: pass failures.

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

Identity Checks: Upgrades == None to is None.

๐Ÿ’ก The Roadmap
It is still in its early MVP stage! I've already received some great feedback from Reddit about handling edge cases (like import hoisting), and I'm actively working on a robust pytest suite. The ultimate goal is a Rust rewrite (pyneat-rs) for massive multi-threading performance.

Links:

๐Ÿ™ GitHub: https://github.com/khanhnam-nathan/Pyneat

๐Ÿ“ฆ PyPI: pip install pyneat-cli

I would love to hear your thoughts! Have you ever struggled with writing structural refactoring tools? Any feedback on the architecture would be deeply appreciated!

Top comments (0)