Tags Python

In the past couple of years, automated reformatting tools came into prominence with go fmt for the Go programming language and clang-format for C, C++ and Java. It's very rare to encounter unformatted Go code, and the same becomes true of C++ in a number of projects (a few major open-source projects start enforcing formatting in pre-commit rules and such).

Python didn't have such a tool; well, it kinda did. There's a bunch of auto-fixers there like autopep8, but all of these serve slightly different roles. autopep8's focus is larger than just whitespace and formatting, and it won't touch code that isn't violating PEP 8 and just looks ugly. This is somewhat similar to the many existing auto linters and fixers for C++, and yet clang-format shot into prominence, for a good reason. There's a good case to make for a tool that just cares about formatting (without actually modifying the code's AST in any way), and reformats the whole code to consistently follow a single standard.

YAPF was conceived as a new tool to do this for Python. It's out there now, open-source; The first alpha release was pushed to PyPI yesterday, so you can go ahead and run: pip install yapf, or just use the downloaded or cloned source directory to run it. Here's an example:

$ cat /tmp/code.py
x = {  'a':37,'b':42,

'c':927}

y = 'hello ''world'
z = 'hello '+'world'
a = 'hello {}'.format('world')
class foo  (     object  ):
  def f    (self   ):
    return       37*-2
  def g(self, x,y=42):
      return y
def f  (   a ) :
  return      37-a[42-x :  y**3]


$ python yapf /tmp/code.py
x = {'a': 37, 'b': 42, 'c': 927}

y = 'hello ' 'world'
z = 'hello ' + 'world'
a = 'hello {}'.format('world')


class foo(object):
    def f(self):
        return 37 * -2

    def g(self, x, y=42):
        return y


def f(a):
    return 37 - a[42 - x:y ** 3]

YAPF also accepts the -i flag to overwrite a file, and a bunch of other configuration parameters. Check it out with yapf --help.

There are two big advantages for using YAPF for all your code:

  1. It makes you think (and obsess) about formatting much less when writing / tweaking code. This saves time when coding.
  2. It makes code from different developers consistent in a single project. This aids reading code, so IMHO it's the more important advantage.

I care about this tool a lot - not only because I find it really useful, but also because I had the privelege to participate in its development. Since its initial release it got a huge amount of attention with more than 2000 stars on Github as of this morning, in just a couple of weeks - there's obviously a need for such a tool out there! It's also being used in a growing number of Python projects internally at Google.

Python is a language that carries the code readability flag tall and proud. And yet, until YAPF, my feeling was that even C++ had a better auto-formatting story with clang-format. Hopefully YAPF will change this.

So please, try YAPF out, use it for your code, integrate it into your development process, report bugs, contribute pull requests.