Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 1 | ==== |
| 2 | YAPF |
| 3 | ==== |
| 4 | |
Bill Wendling | fb8ab38 | 2015-03-18 20:24:14 -0700 | [diff] [blame] | 5 | .. image:: https://travis-ci.org/google/yapf.svg?branch=master |
| 6 | :target: https://travis-ci.org/google/yapf |
| 7 | :alt: Build status |
| 8 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 9 | Introduction |
| 10 | ============ |
| 11 | |
| 12 | Most of the current formatters for Python -- e.g., autopep8, and pep8ify -- are |
| 13 | made to remove lint errors from code. This has some obvious limitations. For |
| 14 | instance, code that conforms to the PEP 8 guidelines may not be reformatted. |
| 15 | But it doesn't mean that the code looks good. |
| 16 | |
| 17 | YAPF takes a different approach. It's based off of 'clang-format', developed by |
| 18 | Daniel Jasper. In essence, the algorithm takes the code and reformats it to the |
| 19 | best formatting that conforms to the style guide, even if the original code |
| 20 | didn't violate the style guide. |
| 21 | |
| 22 | The ultimate goal is that the code YAPF produces is as good as the code that a |
| 23 | programmer would write if they were following the style guide. |
| 24 | |
Bill Wendling | 52e0411 | 2015-03-18 20:42:26 -0700 | [diff] [blame] | 25 | .. note:: |
| 26 | |
| 27 | YAPF is not an official Google product (experimental or otherwise), it is |
| 28 | just code that happens to be owned by Google. |
| 29 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 30 | .. contents:: |
| 31 | |
| 32 | Installation |
| 33 | ============ |
| 34 | |
Eli Bendersky | 891f438 | 2015-03-20 15:28:49 -0700 | [diff] [blame] | 35 | YAPF supports Python 2.7 and 3.4+. |
Eli Bendersky | 78b616e | 2015-03-19 05:17:31 -0700 | [diff] [blame] | 36 | |
| 37 | To install YAPF from the source directory:: |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 38 | |
| 39 | $ sudo python ./setup.py install |
| 40 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 41 | Usage |
| 42 | ===== |
| 43 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 44 | Options:: |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 45 | |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 46 | |
| 47 | usage: yapf [-h] [--style STYLE] [-d | -i] [-l START-END | -r] ... |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 48 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 49 | Formatter for Python code. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 50 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 51 | positional arguments: |
| 52 | files |
| 53 | |
| 54 | optional arguments: |
| 55 | -h, --help show this help message and exit |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 56 | --style STYLE specify formatting style: either a style name (for |
| 57 | example "pep8" or "google"), or the name of a file |
| 58 | with style settings |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 59 | -d, --diff print the diff for the fixed source |
| 60 | -i, --in-place make changes to files in place |
| 61 | -l START-END, --lines START-END |
| 62 | range of lines to reformat, one-based |
| 63 | -r, --recursive run recursively over directories |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 64 | |
Eli Bendersky | 891f438 | 2015-03-20 15:28:49 -0700 | [diff] [blame] | 65 | Note: after reformatting a chunk of code, YAPF verifies that it's correct (can |
| 66 | be parsed by Python itself). This means that if you're reformatting Python 3 |
| 67 | code, it's best to run YAPF itself under Python 3. The same goes for Python 2. |
| 68 | |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 69 | Formatting style |
| 70 | ---------------- |
| 71 | |
| 72 | The formatting style used by YAPF is configurable and there are many "knobs" |
| 73 | that can be used to tune how YAPF does formatting. See the ``style.py`` module |
| 74 | for the full list. |
| 75 | |
| 76 | To control the style, run YAPF with the ``--style`` argument. It accepts either |
| 77 | one of the predefined styles (currently "pep8" or "google"), or a path to a |
| 78 | configuration file that specifies the desired style. The file is a simple |
| 79 | listing of (case-insensitive) ``key = value`` pairs with a ``[style]`` heading. |
Eli Bendersky | 159fa1e | 2015-03-23 06:34:22 -0700 | [diff] [blame^] | 80 | For example:: |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 81 | |
| 82 | [style] |
| 83 | based_on_style = pep8 |
| 84 | spaces_before_comment = 4 |
| 85 | split_before_logical_operator = true |
| 86 | |
| 87 | The ``based_on_style`` setting determines which of the predefined styles this |
| 88 | custom style is based on (think of it like subclassing). |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 89 | |
| 90 | Why Not Improve Existing Tools? |
| 91 | =============================== |
| 92 | |
| 93 | We wanted to use clang-format's reformatting algorithm. It's very powerful and |
| 94 | designed to come up with the best formatting possible. Existing tools were |
| 95 | created with different goals in mind, and would require extensive modifications |
| 96 | to convert to using clang-format's algorithm. |
| 97 | |
| 98 | |
| 99 | Can I Use YAPF In My Program? |
| 100 | ============================= |
| 101 | |
| 102 | Please do! YAPF was designed to be used as a library as well as a command line |
| 103 | tool. This means that a tool or IDE plugin is free to use YAPF. |
| 104 | |
| 105 | |
| 106 | Gory Details |
| 107 | ============ |
| 108 | |
| 109 | Algorithm Design |
| 110 | ---------------- |
| 111 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 112 | The main data structure in YAPF is the ``UnwrappedLine`` object. It holds a list |
| 113 | of ``FormatToken``\s, that we would want to place on a single line if there were |
| 114 | no column limit. An exception being a comment in the middle of an expression |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 115 | statement will force the line to be formatted on more than one line. The |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 116 | formatter works on one ``UnwrappedLine`` object at a time. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 117 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 118 | An ``UnwrappedLine`` typically won't affect the formatting of lines before or |
| 119 | after it. There is a part of the algorithm that may join two or more |
| 120 | ``UnwrappedLine``\s into one line. For instance, an if-then statement with a |
| 121 | short body can be placed on a single line:: |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 122 | |
| 123 | if a == 42: continue |
| 124 | |
| 125 | YAPF's formatting algorithm creates a weighted tree that acts as the solution |
| 126 | space for the algorithm. Each node in the tree represents the result of a |
| 127 | formatting decision --- i.e., whether to split or not to split before a token. |
| 128 | Each formatting decision has a cost associated with it. Therefore, the cost is |
| 129 | realized on the edge between two nodes. (In reality, the weighted tree doesn't |
| 130 | have separate edge objects, so the cost resides on the nodes themselves.) |
| 131 | |
| 132 | For example, take the following Python code snippet. For the sake of this |
| 133 | example, assume that line (1) violates the column limit restriction and needs to |
| 134 | be reformatted. |
| 135 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 136 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 137 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 138 | def xxxxxxxxxxx(aaaaaaaaaaaa, bbbbbbbbb, cccccccc, dddddddd, eeeeee): # 1 |
| 139 | pass # 2 |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 140 | |
| 141 | For line (1), the algorithm will build a tree where each node (a |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 142 | ``FormattingDecisionState`` object) is the state of the line at that token given |
| 143 | the decision to split before the token or not. Note: the ``FormatDecisionState`` |
| 144 | objects are copied by value so each node in the graph is unique and a change in |
| 145 | one doesn't affect other nodes. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 146 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 147 | Heuristics are used to determine the costs of splitting or not splitting. |
| 148 | Because a node holds the state of the tree up to a token's insertion, it can |
| 149 | easily determine if a splitting decision will violate one of the style |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 150 | requirements. For instance, the heuristic is able to apply an extra penalty to |
| 151 | the edge when not splitting between the previous token and the one being added. |
| 152 | |
| 153 | There are some instances where we will never want to split the line, because |
| 154 | doing so will always be detrimental (i.e., it will require a backslash-newline, |
| 155 | which is very rarely desirable). For line (1), we will never want to split the |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 156 | first three tokens: ``def``, ``xxxxxxxxxxx``, and ``(``. Nor will we want to |
| 157 | split between the ``)`` and the ``:`` at the end. These regions are said to be |
| 158 | "unbreakable." This is reflected in the tree by there not being a "split" |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 159 | decision (left hand branch) within the unbreakable region. |
| 160 | |
| 161 | Now that we have the tree, we determine what the "best" formatting is by finding |
| 162 | the path through the tree with the lowest cost. |
| 163 | |
| 164 | And that's it! |