Auto formatters for Python πŸ‘¨β€πŸ’»πŸ€–

Kevin Peters
Jun 2, 2018 Β· 9 min read
  • This directly reduces the number of discussions about unnecessary things and let the developers focus on writing actual code
  • It will also help with onboarding developers on the code base because the style of the code is consistent
  • Less merge conflicts since the style will almost always be the same

autopep8 β€” GitHub

autopep8 is an auto formatter built and open-sourced and made by several developers. It is maybe the most used right now since it is one of the oldest tools out there. It uses pycodestyle to analyze which parts of your code do not fit to the pep-guidelines and will try to fix them. There is a list of features listed in the README of the repository on which pep guidelines are supported. You can find this list here. The tool will also do small additional checks. As of the time of writing the repository had around 2200 stars (June 2018).

yapf β€” GitHub

Yet another Python formatter is another tool which is produced and maintained by none other than Google. It has ~7200 stars (June 2018) on GitHub and follows a different mindset in comparison to autopep8. It will not make code compliant with pep guidelines or try to fix linting issues. It will just format the code. This requires manual work then to make the code look nice which wastes developer time. Other than that yapf is really configurable. It includes defaults for pep8, Google, Facebook and Chromium styling. You can also change a lot of style rules. More information can be found in the Knobs section of the README. The tool also offers an online version where you can try out the formatting. It can be found here.

black β€” GitHub

The last popular auto formatter which is considered in this blog article. It is an initiative of Łukasz Langa who is a Python Core Developer. The tool is used by different open source libraries like Fabric 2 and pytest. It has around 3800 stars on GitHub (June 2018) and the main incentive is to not have that many options so you do not even have to discuss the options. This mindset is also followed by prettier, a big JavaScript auto formatter.


Left is the base code, yapf (Google, pep8) did not make any changes, the code in the middle is formatted by black and the right one is formatted by yapf (Facebook)

Base code, autopep8 and yapf (Google, pep8)
black and yapf (Facebook)

Base code
autopep8
black
yapf (Facebook)
yapf (Google), yapf (pep8)

Base code
autopep8
black
yapf (Facebook)
yapf (Google), yapf (pep8)

Conclusion

All of the formatters are doing a good job at formatting the code. But in my opinion, autopep8 is not really formatting but more or less just trying make your code compliant to pep8. Still, the code might look bad and does not fulfill the requirement of being an auto formatter.


3YOURMIND-Tech

Blog of the technology team at @3YOURMIND. We give you an insight look into the team. We are working with Python, Vue.js, JavaScript, Java, C++ and many more languages.

Kevin Peters

Written by

Full-Stack Developer with a passion for Vue.js, JavaScript and .NET Core, also doing Python/Django development. https://www.kevinpeters.net/

3YOURMIND-Tech

Blog of the technology team at @3YOURMIND. We give you an insight look into the team. We are working with Python, Vue.js, JavaScript, Java, C++ and many more languages.