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 |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 27 | programmer would write if they were following the style guide. It takes away |
| 28 | some of the drudgery of maintaining your code. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 29 | |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 30 | .. footer:: |
Bill Wendling | 52e0411 | 2015-03-18 20:42:26 -0700 | [diff] [blame] | 31 | |
| 32 | YAPF is not an official Google product (experimental or otherwise), it is |
| 33 | just code that happens to be owned by Google. |
| 34 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 35 | .. contents:: |
| 36 | |
| 37 | Installation |
| 38 | ============ |
| 39 | |
Eli Bendersky | 5eb8823 | 2015-03-27 06:27:11 -0700 | [diff] [blame] | 40 | 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] | 41 | support official releases to PyPI and the most stable and correct version is |
| 42 | at the tip of the ``master`` branch in this repository. We plan to make a |
| 43 | first beta release (including to PyPI) in the next few weeks. |
| 44 | |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 45 | If you intend to use YAPF as a command-line tool rather than as a library, |
| 46 | installation is not necessary. YAPF supports being run as a directory by the |
| 47 | Python interpreter. If you cloned/unzipped yapf into ``DIR``, it's possible to |
| 48 | run:: |
| 49 | |
Eli Bendersky | b3678b3 | 2015-03-25 14:16:11 -0700 | [diff] [blame] | 50 | $ PYTHONPATH=DIR python DIR/yapf [options] ... |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 51 | |
Eli Bendersky | 5eb8823 | 2015-03-27 06:27:11 -0700 | [diff] [blame] | 52 | Python versions |
| 53 | =============== |
| 54 | |
| 55 | YAPF supports Python 2.7 and 3.4+. |
| 56 | |
| 57 | YAPF requires the code it formats to be valid Python for the version YAPF itself |
| 58 | runs under. Therefore, if you format Python 3 code with YAPF, run YAPF itself |
| 59 | under Python 3 (and similarly for Python 2). |
| 60 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 61 | Usage |
| 62 | ===== |
| 63 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 64 | Options:: |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 65 | |
Bill Wendling | a1cb492 | 2015-03-30 20:18:12 -0700 | [diff] [blame] | 66 | usage: yapf [-h] [--style STYLE] [-d | -i] [-l START-END | -r] ... |
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 | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 78 | -d, --diff print the diff for the fixed source |
| 79 | -i, --in-place make changes to files in place |
| 80 | -l START-END, --lines START-END |
| 81 | range of lines to reformat, one-based |
| 82 | -r, --recursive run recursively over directories |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 83 | |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 84 | Formatting style |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 85 | ================ |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 86 | |
| 87 | The formatting style used by YAPF is configurable and there are many "knobs" |
| 88 | that can be used to tune how YAPF does formatting. See the ``style.py`` module |
| 89 | for the full list. |
| 90 | |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame^] | 91 | To control the style, run YAPF with the ``--style`` argument. It accepts one of |
| 92 | the predefined styles (e.g., ``pep8`` or ``google``), a path to a configuration |
| 93 | file that specifies the desired style, or a dictionary of key/value pairs. |
| 94 | |
| 95 | The config file is a simple listing of (case-insensitive) ``key = value`` pairs |
| 96 | with a ``[style]`` heading. For example:: |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 97 | |
| 98 | [style] |
| 99 | based_on_style = pep8 |
| 100 | spaces_before_comment = 4 |
| 101 | split_before_logical_operator = true |
| 102 | |
| 103 | The ``based_on_style`` setting determines which of the predefined styles this |
| 104 | custom style is based on (think of it like subclassing). |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 105 | |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame^] | 106 | It's also possible to do the same on the command line with a dictionary. For |
| 107 | example:: |
| 108 | |
| 109 | --style='{based_on_style: google, indent_width: 4}' |
| 110 | |
| 111 | This will take the ``google`` base style and modify it to have four space |
| 112 | indentations. |
| 113 | |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 114 | Example |
| 115 | ======= |
| 116 | |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 117 | An example of the type of formatting that YAPF can do, it will take this ugly code: |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 118 | |
| 119 | .. code-block:: python |
| 120 | |
| 121 | x = { 'a':37,'b':42, |
| 122 | |
| 123 | 'c':927} |
| 124 | |
| 125 | y = 'hello ''world' |
| 126 | z = 'hello '+'world' |
| 127 | a = 'hello {}'.format('world') |
| 128 | class foo ( object ): |
| 129 | def f (self ): |
| 130 | return 37*-+2 |
| 131 | def g(self, x,y=42): |
| 132 | return y |
| 133 | def f ( a ) : |
| 134 | return 37+-+a[42-x : y**3] |
| 135 | |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 136 | and reformat it into: |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 137 | |
| 138 | .. code-block:: python |
| 139 | |
| 140 | x = {'a': 37, 'b': 42, 'c': 927} |
| 141 | |
| 142 | y = 'hello ' 'world' |
| 143 | z = 'hello ' + 'world' |
| 144 | a = 'hello {}'.format('world') |
| 145 | |
| 146 | |
| 147 | class foo(object): |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 148 | def f(self): |
| 149 | return 37 * -+2 |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 150 | |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 151 | def g(self, x, y=42): |
| 152 | return y |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 153 | |
| 154 | |
| 155 | def f(a): |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 156 | return 37 + -+a[42 - x:y ** 3] |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 157 | |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 158 | (Potentially) Frequently Asked Questions |
| 159 | ======================================== |
| 160 | |
| 161 | Why does YAPF destroy my awesome formatting? |
| 162 | -------------------------------------------- |
| 163 | |
| 164 | YAPF tries very hard to get the formatting correct. But for some code, it won't |
| 165 | be as good as hand-formatting. In particular, large data literals may become |
| 166 | horribly disfigured under YAPF. |
| 167 | |
| 168 | The reason for this is manifold. But in essence YAPF is simply a tool to help |
| 169 | with development. It will format things to coincide with the style guide, but |
| 170 | that may not equate with readability. |
| 171 | |
| 172 | What can be done to alleviate this situation is to indicate regions YAPF should |
| 173 | ignore when reformatting something: |
| 174 | |
| 175 | .. code-block:: python |
| 176 | |
| 177 | # yapf: disable |
| 178 | FOO = { |
| 179 | # ... some very large, complex data literal. |
| 180 | } |
| 181 | |
| 182 | BAR = [ |
| 183 | # ... another large data literal. |
| 184 | ] |
| 185 | # yapf: enable |
| 186 | |
| 187 | You can also disable formatting for a single literal like this: |
| 188 | |
| 189 | .. code-block:: python |
| 190 | |
| 191 | BAZ = { |
| 192 | [1, 2, 3, 4], |
| 193 | [5, 6, 7, 8], |
| 194 | [9, 10, 11, 12] |
| 195 | } # yapf: disable |
| 196 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 197 | Why Not Improve Existing Tools? |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 198 | ------------------------------- |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 199 | |
| 200 | We wanted to use clang-format's reformatting algorithm. It's very powerful and |
| 201 | designed to come up with the best formatting possible. Existing tools were |
| 202 | created with different goals in mind, and would require extensive modifications |
| 203 | to convert to using clang-format's algorithm. |
| 204 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 205 | Can I Use YAPF In My Program? |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 206 | ----------------------------- |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 207 | |
| 208 | Please do! YAPF was designed to be used as a library as well as a command line |
| 209 | tool. This means that a tool or IDE plugin is free to use YAPF. |
| 210 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 211 | Gory Details |
| 212 | ============ |
| 213 | |
| 214 | Algorithm Design |
| 215 | ---------------- |
| 216 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 217 | The main data structure in YAPF is the ``UnwrappedLine`` object. It holds a list |
| 218 | of ``FormatToken``\s, that we would want to place on a single line if there were |
| 219 | 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] | 220 | 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] | 221 | formatter works on one ``UnwrappedLine`` object at a time. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 222 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 223 | An ``UnwrappedLine`` typically won't affect the formatting of lines before or |
| 224 | after it. There is a part of the algorithm that may join two or more |
| 225 | ``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] | 226 | short body can be placed on a single line: |
| 227 | |
| 228 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 229 | |
| 230 | if a == 42: continue |
| 231 | |
| 232 | YAPF's formatting algorithm creates a weighted tree that acts as the solution |
| 233 | space for the algorithm. Each node in the tree represents the result of a |
| 234 | formatting decision --- i.e., whether to split or not to split before a token. |
| 235 | Each formatting decision has a cost associated with it. Therefore, the cost is |
| 236 | realized on the edge between two nodes. (In reality, the weighted tree doesn't |
| 237 | have separate edge objects, so the cost resides on the nodes themselves.) |
| 238 | |
| 239 | For example, take the following Python code snippet. For the sake of this |
| 240 | example, assume that line (1) violates the column limit restriction and needs to |
| 241 | be reformatted. |
| 242 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 243 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 244 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 245 | def xxxxxxxxxxx(aaaaaaaaaaaa, bbbbbbbbb, cccccccc, dddddddd, eeeeee): # 1 |
| 246 | pass # 2 |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 247 | |
| 248 | 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] | 249 | ``FormattingDecisionState`` object) is the state of the line at that token given |
| 250 | the decision to split before the token or not. Note: the ``FormatDecisionState`` |
| 251 | objects are copied by value so each node in the graph is unique and a change in |
| 252 | one doesn't affect other nodes. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 253 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 254 | Heuristics are used to determine the costs of splitting or not splitting. |
| 255 | Because a node holds the state of the tree up to a token's insertion, it can |
| 256 | easily determine if a splitting decision will violate one of the style |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 257 | requirements. For instance, the heuristic is able to apply an extra penalty to |
| 258 | the edge when not splitting between the previous token and the one being added. |
| 259 | |
| 260 | There are some instances where we will never want to split the line, because |
| 261 | doing so will always be detrimental (i.e., it will require a backslash-newline, |
| 262 | 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] | 263 | first three tokens: ``def``, ``xxxxxxxxxxx``, and ``(``. Nor will we want to |
| 264 | split between the ``)`` and the ``:`` at the end. These regions are said to be |
| 265 | "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] | 266 | decision (left hand branch) within the unbreakable region. |
| 267 | |
| 268 | Now that we have the tree, we determine what the "best" formatting is by finding |
| 269 | the path through the tree with the lowest cost. |
| 270 | |
| 271 | And that's it! |