Skip to content

pbsds/fix-my-functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Python code formatter which applies a single transform: function signature tabulation. A formatting style designed to read type annotations with ease. Based on redbaron, which in turn wraps the baron parser.

pip install fix-my-functions
fix-my-functions --help

It formats functions to look like this:

def spaghetti(self,
        determinant           : int,                       # foo
        fraction              : float      = 42,
        hidden_features       : float      = 42,           # bar
        hyper_determinant                  = None,
        hyper_fraction        : str | None = "spaghetti",  # baz
        hyper_hidden_features : str | None = "spaghetti",
        ) -> bool | None:
    pass

Notable features of this formatting, and why I like it:

  • Doubly indented arguments and return annotations.
    • Visually separates the function arguments from the function body,
    • In a way most naive text editors are able to fold the whole function.
  • The self and cls argument is folded onto the same line as the function name.
    • Saves space
  • Forced trailing comma.
    • Makes it easy to reorder lines.
  • Tabulated formatting
    • argument names, then type annotations, then default values, then comments
    • Very easy to seek and read

The bad:

  • Changes in column width touches a lot of lines, which puts the git blame on you.
  • A bit limited when restricted to 80 columns, but workable

Features

  • Comment preservation: Tries to preserve comments.
  • Iterative mode: apply only a single transformation at a time, inspired by git add --patch.

Status

I made the core of this in a day, with the intention of developing it further. I then discovered that baron has issues such as this, this and this, making it impractical to use in production.

Can this formatter be rewritten using parso? Can it be added as a knob to yapf? Maybe!

Releases

No releases published

Packages

No packages published

Languages