Automatic formatting of Python source code with Black
Created by: femtobit
I have recently encountered the Black code formatter for Python.
Since we already use clang-format for the C++ part of the code, which seems to work very well, I would like to suggest we discuss whether we want to introduce automatic formatting for our Python code too. Using Black makes it a lot easier to have a consistent code style throughout our Python code, regardless of the author (and consistent with the official PEP 8 style guidelines). Also, Black is written to format code in a way that diffs are small, which is useful for code review.
An Atom package is available and Visual Studio Code seems to support Black as part of the official Python extension. It can be installed via pip3 install black
. (Black itself requires Python 3 but it can also format Python 2 code.)
This PR shows what happens when letting Black reformat our current codebase. I have also included a proposed section on Black for CONTRIBUTING.md
.
Let me know what you think about this proposal.