YAPF takes a different approach. It’s based off of ‘clang-format’, developed by Daniel Jasper. In essence, the algorithm takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn’t violate the style guide.
YAPF is not an official Google product (experimental or otherwise), it is
just code that happens to be owned by Google.
YAPF supports Python 2.7 and 3.4.1+.
YAPF requires the code it formats to be valid Python for the version YAPF itself
runs under. Therefore, if you format Python 3 code with YAPF, run YAPF itself
under Python 3 (and similarly for Python 2).
Most of the current formatters for Python — e.g., autopep8, and pep8ify — are made to remove lint errors from code. This has some obvious limitations. For instance, code that conforms to the PEP 8 guidelines may not be reformatted. But it doesn’t mean that the code looks good.
YAPF takes a different approach. It’s based off of ‘clang-format’, developed by Daniel Jasper. In essence, the algorithm takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn’t violate the style guide. The idea is also similar to the ‘gofmt’ tool for the Go programming language: end all holy wars about formatting – if the whole codebase of a project is simply piped through YAPF whenever modifications are made, the style remains consistent throughout the project and there’s no point arguing about style in every code review.
The ultimate goal is that the code YAPF produces is as good as the code that a programmer would write if they were following the style guide. It takes away some of the drudgery of maintaining your code.
Algorithm Design
The main data structure in YAPF is the UnwrappedLine object. It holds a list of FormatTokens, that we would want to place on a single line if there were no column limit. An exception being a comment in the middle of an expression statement will force the line to be formatted on more than one line. The formatter works on one UnwrappedLine object at a time.
An UnwrappedLine typically won’t affect the formatting of lines before or after it. There is a part of the algorithm that may join two or more UnwrappedLines into one line. For instance, an if-then statement with a short body can be placed on a single line:
YAPF’s formatting algorithm creates a weighted tree that acts as the solution space for the algorithm. Each node in the tree represents the result of a formatting decision — i.e., whether to split or not to split before a token. Each formatting decision has a cost associated with it. Therefore, the cost is realized on the edge between two nodes. (In reality, the weighted tree doesn’t have separate edge objects, so the cost resides on the nodes themselves.)
For example, take the following Python code snippet. For the sake of this example, assume that line (1) violates the column limit restriction and needs to be reformatted.
YAPF’s formatting algorithm creates a weighted tree that acts as the solution space for the algorithm. Each node in the tree represents the result of a formatting decision — i.e., whether to split or not to split before a token. Each formatting decision has a cost associated with it. Therefore, the cost is realized on the edge between two nodes. (In reality, the weighted tree doesn’t have separate edge objects, so the cost resides on the nodes themselves.)
For example, take the following Python code snippet. For the sake of this example, assume that line (1) violates the column limit restriction and needs to be reformatted.
How do I use YAPF with PyCharm?
Make sure your executable is at the right location:
- In Pycharm choose Preferences->Tools:
- look at YAPF Settings section -> Executable path.
- You have to put your yapf executable at this location/path if it isn’t already.
Why does YAPF destroy my awesome formatting?
YAPF tries very hard to get the formatting correct. But for some code, it won’t
be as good as hand-formatting. In particular, large data literals may become
horribly disfigured under YAPF.
The reason for this is many-fold. But in essence YAPF is simply a tool to help
with development. It will format things to coincide with the style guide, but
that may not equate with readability.
What can be done to alleviate this situation is to indicate regions YAPF should
ignore when reformatting something:
Why Not Improve Existing Tools?
We wanted to use clang-format’s reformatting algorithm. It’s very powerful and
designed to come up with the best formatting possible. Existing tools were
created with different goals in mind, and would require extensive modifications
to convert to using clang-format’s algorithm.
Can I Use YAPF In My Program?
YAPF was designed to be used as a library as well as a command line
tool. This means that a tool or IDE plugin is free to use YAPF.
YAPF Vs Autopep8
Autopep8 is formatting the parameters below each other and also starting a new line since it analysed that there are too many parameters to fit. … Yapf with the Google and pep8 setting will put the parameters below each other but will try to put the first parameter on the same line where the function begins.
Introduction
To format C and C++ code, we often use Clang-Format. Recently I downloaded a couple of Python projects from Google’s GitHub, and I found that Google’s standard is to use 2-space indentation while my preference is to use 4-space indentation. Then the question is how to parse the Python projects so that it uses 4-space indentation. I tried to do it in Visual Studio Code but found no solutions. A naive way is to replace the 2-space string to 4-space string. However, this might introduce some unexpected problems.
Somehow, I found Google has an open source tool YAPF for Python formatting. Its usages are very similar to Clang-Format, and its documentation is better than the Clang-Format’s documentation in my opinion. Since the YAPF’s documentation is self-explanatory, in this blog post I will just document some simple usages redundantly.
Installation
We install YAPF via pip.
$ pip install yapf
However, if somehow the OS requires you to add –user in order to install, you would likely have problems in using YAPF from the terminal. To solve this problem, in my case, we reinstall pip3.
$ sudo python3 -m pip uninstall pip
$ sudo apt install python3-pip –reinstall -y
$ source ~/.bashrc
Then we should be able to install YAPF for all users without having to specify –user.
Simple Usages
Use 4-Space Indentation in All Python Files
$ yapf –in-place –recursive –style=”{indent_width: 4}” *.py
- –in-place is to make modifications in-place to all the Python files, make sure you are aware of this and backup all the files before formatting.
- –style is to indicate what style details should we use. Just like Clang-Format, if we have a style file in the directory, this argument does not have to be supplemented.
- –recursive is to recursively go through the directory and its subdirectory.
Leave a Reply