Show HN: Python Source Code Refactoring Toolkit via AST

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • SaaSHub - Software Alternatives and Reviews
  • prefactor

    Tool for writing Python refactorings

  • Nice, I've always thought Python could do better by adopting source transformation tools like babel. I implemented something similar using lib2to3, which preserves whitespace and comments more accurately: https://github.com/banga/prefactor

  • hpy

    HPy: a better API for Python

  • Projects that depend on CPython C extensions should consider migrating to HPy[1] for C extension compatibility across Python implementations.

    [1] https://github.com/hpyproject/hpy

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
  • Bowler

    Safe code refactoring for modern Python.

  • A similar type project. Though I haven't seen much activity recently:

    https://github.com/facebookincubator/Bowler

  • prosodia

    Parse Backus-Naur form syntaxs into parsers to generate ASTs of arbitrary syntaxs.

  • I wrote a library that takes a language specification (BNF, ABNF, etc) into a custom AST in Python. Then you can define a set of visitors for the custom AST to transform the tree into whatever you want. I implemented some checks integrated into the Python type system to type-check the visitors and I've used the library to do some non-trivial manipulations before.

    It's mostly a pet project and I just wanted to share since it could maybe at least inspire something.

    https://github.com/chrisphilip322/prosodia

  • refactor

    AST-based fragmental source code refactoring toolkit for Python

  • Indeed! I also would suggest people to use a CST implementation (parso / LibCST) instead of refactor if they intend do large scale refactors, but from what I can see in my previous attempts (e.g teyit, a unittest assertion formatter) when you deal with small code fragments (a single expression, or a small statement) then you generally don't need to worry much about the style. The only concern is the literals (especially strings, which there are a few different variations of the same AST) where you could resurrect them back from the token stream (which the CustomUnparser representative in refactor allows).

    The real start point for this project was to find / replace all type()'s in CPython codebase with type(type()) (e.g type('') would become type(str)) which is very light weight transformation, and I was able to write a script which did it without having any major problems about style on over 2000 files. Here it is for the reference: https://github.com/isidentical/refactor/blob/master/examples...

    Also one thing to note here is that; in the last couple of years, thanks to black (and yapf), the adoptance of code formatters have really increased which is very nice for custom refactoring tools like refactor since the end-code would be refactored anyways so that means if you convert a multi line call, or a list to a single line version then the formatter you use probably reformat that segment anyways.

    But thanks for authoring Bowler! It is a very cool project.

  • semgrep

    Lightweight static analysis for many languages. Find bug variants with patterns that look like source code.

  • Maybe something like this:

    https://semgrep.dev/

    I found it last week. It may be able to detect a potential issue in multiple languages, but not sure if it supports refactoring.

  • py2js

  • Nice! I like the syntax, this reminds me of the structural search and replace feature from IntelliJ IDEA. Is this the project you were referring to?

    https://github.com/erezsh/py2js

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts