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 | |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame^] | 12 | Most of the current formatters for Python --- e.g., autopep8, and pep8ify --- |
| 13 | are made to remove lint errors from code. This has some obvious limitations. |
| 14 | For instance, code that conforms to the PEP 8 guidelines may not be |
| 15 | reformatted. But it doesn't mean that the code looks good. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 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 |
Peter Bengtsson | 1c60ad7 | 2015-03-24 20:05:39 -0700 | [diff] [blame] | 20 | didn't violate the style guide. The idea is also similar to the 'gofmt' tool for |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 21 | the Go programming language: end all holy wars about formatting - if the whole |
| 22 | code base of a project is simply piped through YAPF whenever modifications are |
| 23 | made, the style remains consistent throughout the project and there's no point |
| 24 | arguing about style in every code review. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 25 | |
| 26 | The ultimate goal is that the code YAPF produces is as good as the code that a |
| 27 | programmer would write if they were following the style guide. |
| 28 | |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 29 | .. footer:: |
Bill Wendling | 52e0411 | 2015-03-18 20:42:26 -0700 | [diff] [blame] | 30 | |
| 31 | YAPF is not an official Google product (experimental or otherwise), it is |
| 32 | just code that happens to be owned by Google. |
| 33 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 34 | .. contents:: |
| 35 | |
| 36 | Installation |
| 37 | ============ |
| 38 | |
Eli Bendersky | 5eb8823 | 2015-03-27 06:27:11 -0700 | [diff] [blame] | 39 | We consider YAPF to be "alpha" quality at this time. Therefore, we don't yet |
Eli Bendersky | 8a36536 | 2015-03-25 18:42:22 -0700 | [diff] [blame] | 40 | support official releases to PyPI and the most stable and correct version is |
| 41 | at the tip of the ``master`` branch in this repository. We plan to make a |
| 42 | first beta release (including to PyPI) in the next few weeks. |
| 43 | |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 44 | If you intend to use YAPF as a command-line tool rather than as a library, |
| 45 | installation is not necessary. YAPF supports being run as a directory by the |
| 46 | Python interpreter. If you cloned/unzipped yapf into ``DIR``, it's possible to |
| 47 | run:: |
| 48 | |
Eli Bendersky | b3678b3 | 2015-03-25 14:16:11 -0700 | [diff] [blame] | 49 | $ PYTHONPATH=DIR python DIR/yapf [options] ... |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 50 | |
Eli Bendersky | 5eb8823 | 2015-03-27 06:27:11 -0700 | [diff] [blame] | 51 | Python versions |
| 52 | =============== |
| 53 | |
| 54 | YAPF supports Python 2.7 and 3.4+. |
| 55 | |
| 56 | YAPF requires the code it formats to be valid Python for the version YAPF itself |
| 57 | runs under. Therefore, if you format Python 3 code with YAPF, run YAPF itself |
| 58 | under Python 3 (and similarly for Python 2). |
| 59 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 60 | Usage |
| 61 | ===== |
| 62 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 63 | Options:: |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 64 | |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame^] | 65 | usage: yapf [-h] [--style STYLE] [--noverify] [-d | -i] [-l START-END | -r] |
| 66 | ... |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 67 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 68 | Formatter for Python code. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 69 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 70 | positional arguments: |
| 71 | files |
| 72 | |
| 73 | optional arguments: |
| 74 | -h, --help show this help message and exit |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 75 | --style STYLE specify formatting style: either a style name (for |
| 76 | example "pep8" or "google"), or the name of a file |
Eli Bendersky | 2cec8b4 | 2015-03-27 06:31:34 -0700 | [diff] [blame] | 77 | with style settings. pep8 is the default. |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame^] | 78 | --noverify do not verify refomatted code for syntax errors |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 79 | -d, --diff print the diff for the fixed source |
| 80 | -i, --in-place make changes to files in place |
| 81 | -l START-END, --lines START-END |
| 82 | range of lines to reformat, one-based |
| 83 | -r, --recursive run recursively over directories |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 84 | |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame^] | 85 | .. note:: |
| 86 | |
| 87 | After reformatting a chunk of code, YAPF verifies that it's correct (can be |
| 88 | parsed by Python itself). This means that if you're reformatting Python 3 |
| 89 | code, it's best to run YAPF itself under Python 3. The same goes for Python |
| 90 | 2. |
| 91 | |
| 92 | It's possible to disable verification with the ``--noverify`` flag. |
Eli Bendersky | 891f438 | 2015-03-20 15:28:49 -0700 | [diff] [blame] | 93 | |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 94 | Formatting style |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 95 | ================ |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 96 | |
| 97 | The formatting style used by YAPF is configurable and there are many "knobs" |
| 98 | that can be used to tune how YAPF does formatting. See the ``style.py`` module |
| 99 | for the full list. |
| 100 | |
| 101 | To control the style, run YAPF with the ``--style`` argument. It accepts either |
| 102 | one of the predefined styles (currently "pep8" or "google"), or a path to a |
| 103 | configuration file that specifies the desired style. The file is a simple |
| 104 | listing of (case-insensitive) ``key = value`` pairs with a ``[style]`` heading. |
Eli Bendersky | 159fa1e | 2015-03-23 06:34:22 -0700 | [diff] [blame] | 105 | For example:: |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 106 | |
| 107 | [style] |
| 108 | based_on_style = pep8 |
| 109 | spaces_before_comment = 4 |
| 110 | split_before_logical_operator = true |
| 111 | |
| 112 | The ``based_on_style`` setting determines which of the predefined styles this |
| 113 | custom style is based on (think of it like subclassing). |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 114 | |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 115 | Example |
| 116 | ======= |
| 117 | |
| 118 | This ugly code: |
| 119 | |
| 120 | .. code-block:: python |
| 121 | |
| 122 | x = { 'a':37,'b':42, |
| 123 | |
| 124 | 'c':927} |
| 125 | |
| 126 | y = 'hello ''world' |
| 127 | z = 'hello '+'world' |
| 128 | a = 'hello {}'.format('world') |
| 129 | class foo ( object ): |
| 130 | def f (self ): |
| 131 | return 37*-+2 |
| 132 | def g(self, x,y=42): |
| 133 | return y |
| 134 | def f ( a ) : |
| 135 | return 37+-+a[42-x : y**3] |
| 136 | |
| 137 | Is reformatted into: |
| 138 | |
| 139 | .. code-block:: python |
| 140 | |
| 141 | x = {'a': 37, 'b': 42, 'c': 927} |
| 142 | |
| 143 | y = 'hello ' 'world' |
| 144 | z = 'hello ' + 'world' |
| 145 | a = 'hello {}'.format('world') |
| 146 | |
| 147 | |
| 148 | class foo(object): |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame^] | 149 | def f(self): |
| 150 | return 37 * -+2 |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 151 | |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame^] | 152 | def g(self, x, y=42): |
| 153 | return y |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 154 | |
| 155 | |
| 156 | def f(a): |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame^] | 157 | return 37 + -+a[42 - x:y ** 3] |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 158 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 159 | Why Not Improve Existing Tools? |
| 160 | =============================== |
| 161 | |
| 162 | We wanted to use clang-format's reformatting algorithm. It's very powerful and |
| 163 | designed to come up with the best formatting possible. Existing tools were |
| 164 | created with different goals in mind, and would require extensive modifications |
| 165 | to convert to using clang-format's algorithm. |
| 166 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 167 | Can I Use YAPF In My Program? |
| 168 | ============================= |
| 169 | |
| 170 | Please do! YAPF was designed to be used as a library as well as a command line |
| 171 | tool. This means that a tool or IDE plugin is free to use YAPF. |
| 172 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 173 | Gory Details |
| 174 | ============ |
| 175 | |
| 176 | Algorithm Design |
| 177 | ---------------- |
| 178 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 179 | The main data structure in YAPF is the ``UnwrappedLine`` object. It holds a list |
| 180 | of ``FormatToken``\s, that we would want to place on a single line if there were |
| 181 | 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] | 182 | 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] | 183 | formatter works on one ``UnwrappedLine`` object at a time. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 184 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 185 | An ``UnwrappedLine`` typically won't affect the formatting of lines before or |
| 186 | after it. There is a part of the algorithm that may join two or more |
| 187 | ``UnwrappedLine``\s into one line. For instance, an if-then statement with a |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 188 | short body can be placed on a single line: |
| 189 | |
| 190 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 191 | |
| 192 | if a == 42: continue |
| 193 | |
| 194 | YAPF's formatting algorithm creates a weighted tree that acts as the solution |
| 195 | space for the algorithm. Each node in the tree represents the result of a |
| 196 | formatting decision --- i.e., whether to split or not to split before a token. |
| 197 | Each formatting decision has a cost associated with it. Therefore, the cost is |
| 198 | realized on the edge between two nodes. (In reality, the weighted tree doesn't |
| 199 | have separate edge objects, so the cost resides on the nodes themselves.) |
| 200 | |
| 201 | For example, take the following Python code snippet. For the sake of this |
| 202 | example, assume that line (1) violates the column limit restriction and needs to |
| 203 | be reformatted. |
| 204 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 205 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 206 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 207 | def xxxxxxxxxxx(aaaaaaaaaaaa, bbbbbbbbb, cccccccc, dddddddd, eeeeee): # 1 |
| 208 | pass # 2 |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 209 | |
| 210 | 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] | 211 | ``FormattingDecisionState`` object) is the state of the line at that token given |
| 212 | the decision to split before the token or not. Note: the ``FormatDecisionState`` |
| 213 | objects are copied by value so each node in the graph is unique and a change in |
| 214 | one doesn't affect other nodes. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 215 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 216 | Heuristics are used to determine the costs of splitting or not splitting. |
| 217 | Because a node holds the state of the tree up to a token's insertion, it can |
| 218 | easily determine if a splitting decision will violate one of the style |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 219 | requirements. For instance, the heuristic is able to apply an extra penalty to |
| 220 | the edge when not splitting between the previous token and the one being added. |
| 221 | |
| 222 | There are some instances where we will never want to split the line, because |
| 223 | doing so will always be detrimental (i.e., it will require a backslash-newline, |
| 224 | 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] | 225 | first three tokens: ``def``, ``xxxxxxxxxxx``, and ``(``. Nor will we want to |
| 226 | split between the ``)`` and the ``:`` at the end. These regions are said to be |
| 227 | "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] | 228 | decision (left hand branch) within the unbreakable region. |
| 229 | |
| 230 | Now that we have the tree, we determine what the "best" formatting is by finding |
| 231 | the path through the tree with the lowest cost. |
| 232 | |
| 233 | And that's it! |