Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 1 | ==== |
| 2 | YAPF |
| 3 | ==== |
| 4 | |
Bill Wendling | 19c44d0 | 2015-04-07 23:48:05 -0700 | [diff] [blame] | 5 | .. image:: https://badge.fury.io/py/yapf.svg |
| 6 | :target: http://badge.fury.io/py/yapf |
| 7 | :alt: PyPI version |
| 8 | |
Bill Wendling | fb8ab38 | 2015-03-18 20:24:14 -0700 | [diff] [blame] | 9 | .. image:: https://travis-ci.org/google/yapf.svg?branch=master |
| 10 | :target: https://travis-ci.org/google/yapf |
| 11 | :alt: Build status |
| 12 | |
Bill Wendling | 14ac881 | 2015-04-05 02:47:32 -0700 | [diff] [blame] | 13 | .. image:: https://coveralls.io/repos/google/yapf/badge.svg?branch=master |
| 14 | :target: https://coveralls.io/r/google/yapf?branch=master |
| 15 | :alt: Coverage status |
| 16 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 17 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 18 | Introduction |
| 19 | ============ |
| 20 | |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 21 | Most of the current formatters for Python --- e.g., autopep8, and pep8ify --- |
| 22 | are made to remove lint errors from code. This has some obvious limitations. |
| 23 | For instance, code that conforms to the PEP 8 guidelines may not be |
| 24 | reformatted. But it doesn't mean that the code looks good. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 25 | |
| 26 | YAPF takes a different approach. It's based off of 'clang-format', developed by |
| 27 | Daniel Jasper. In essence, the algorithm takes the code and reformats it to the |
| 28 | 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] | 29 | 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] | 30 | the Go programming language: end all holy wars about formatting - if the whole |
Samuel Dion-Girardeau | 5ae6349 | 2016-09-08 21:01:20 -0400 | [diff] [blame] | 31 | codebase of a project is simply piped through YAPF whenever modifications are |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 32 | made, the style remains consistent throughout the project and there's no point |
| 33 | arguing about style in every code review. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 34 | |
| 35 | 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] | 36 | programmer would write if they were following the style guide. It takes away |
| 37 | some of the drudgery of maintaining your code. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 38 | |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 39 | .. footer:: |
Bill Wendling | 52e0411 | 2015-03-18 20:42:26 -0700 | [diff] [blame] | 40 | |
| 41 | YAPF is not an official Google product (experimental or otherwise), it is |
| 42 | just code that happens to be owned by Google. |
| 43 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 44 | .. contents:: |
| 45 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 46 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 47 | Installation |
| 48 | ============ |
| 49 | |
Bill Wendling | 6e8ca7b | 2015-10-25 01:16:43 -0700 | [diff] [blame] | 50 | To install YAPF from PyPI: |
| 51 | |
Bill Wendling | 4f96aab | 2016-07-16 15:52:51 -0700 | [diff] [blame] | 52 | .. code-block:: |
Eli Bendersky | 8a36536 | 2015-03-25 18:42:22 -0700 | [diff] [blame] | 53 | |
Eli Bendersky | e0e83c1 | 2015-04-06 20:23:30 -0700 | [diff] [blame] | 54 | $ pip install yapf |
| 55 | |
Diogo de Campos | 2f246c0 | 2016-10-06 14:04:38 +0200 | [diff] [blame] | 56 | (optional) If you are using Python 2.7 and want to enable multiprocessing: |
| 57 | |
| 58 | .. code-block:: |
| 59 | |
| 60 | $ pip install futures |
| 61 | |
Eli Bendersky | e0e83c1 | 2015-04-06 20:23:30 -0700 | [diff] [blame] | 62 | YAPF is still considered in "alpha" stage, and the released version may change |
| 63 | often; therefore, the best way to keep up-to-date with the latest development |
| 64 | is to clone this repository. |
| 65 | |
| 66 | Note that if you intend to use YAPF as a command-line tool rather than as a |
| 67 | library, installation is not necessary. YAPF supports being run as a directory |
| 68 | by the Python interpreter. If you cloned/unzipped YAPF into ``DIR``, it's |
Bill Wendling | 6e8ca7b | 2015-10-25 01:16:43 -0700 | [diff] [blame] | 69 | possible to run: |
| 70 | |
Bill Wendling | 4f96aab | 2016-07-16 15:52:51 -0700 | [diff] [blame] | 71 | .. code-block:: |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 72 | |
Eli Bendersky | b3678b3 | 2015-03-25 14:16:11 -0700 | [diff] [blame] | 73 | $ PYTHONPATH=DIR python DIR/yapf [options] ... |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 74 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 75 | |
Eli Bendersky | 5eb8823 | 2015-03-27 06:27:11 -0700 | [diff] [blame] | 76 | Python versions |
| 77 | =============== |
| 78 | |
Eli Bendersky | a7bfe7e | 2015-04-05 06:33:18 -0700 | [diff] [blame] | 79 | YAPF supports Python 2.7 and 3.4.1+. |
Eli Bendersky | 5eb8823 | 2015-03-27 06:27:11 -0700 | [diff] [blame] | 80 | |
| 81 | YAPF requires the code it formats to be valid Python for the version YAPF itself |
| 82 | runs under. Therefore, if you format Python 3 code with YAPF, run YAPF itself |
| 83 | under Python 3 (and similarly for Python 2). |
| 84 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 85 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 86 | Usage |
| 87 | ===== |
| 88 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 89 | Options:: |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 90 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 91 | usage: yapf [-h] [-v] [-d | -i] [-r | -l START-END] [-e PATTERN] |
Diogo de Campos | 2f246c0 | 2016-10-06 14:04:38 +0200 | [diff] [blame] | 92 | [--style STYLE] [--style-help] [--no-local-style] [-p] |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 93 | [files [files ...]] |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 94 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 95 | Formatter for Python code. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 96 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 97 | positional arguments: |
| 98 | files |
| 99 | |
| 100 | optional arguments: |
| 101 | -h, --help show this help message and exit |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 102 | -v, --version show version number and exit |
| 103 | -d, --diff print the diff for the fixed source |
| 104 | -i, --in-place make changes to files in place |
| 105 | -r, --recursive run recursively over directories |
| 106 | -l START-END, --lines START-END |
| 107 | range of lines to reformat, one-based |
| 108 | -e PATTERN, --exclude PATTERN |
| 109 | patterns for files to exclude from formatting |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 110 | --style STYLE specify formatting style: either a style name (for |
| 111 | example "pep8" or "google"), or the name of a file |
Sam Clegg | 5170c3a | 2015-04-16 12:18:58 -0700 | [diff] [blame] | 112 | with style settings. The default is pep8 unless a |
Bill Wendling | 6e8ca7b | 2015-10-25 01:16:43 -0700 | [diff] [blame] | 113 | .style.yapf or setup.cfg file located in one of the |
| 114 | parent directories of the source file (or current |
| 115 | directory for stdin) |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 116 | --style-help show style settings and exit |
| 117 | --no-local-style don't search for local style definition (.style.yapf) |
Diogo de Campos | 2f246c0 | 2016-10-06 14:04:38 +0200 | [diff] [blame] | 118 | -p, --parallel Run yapf in parallel when formatting multiple files. |
| 119 | Requires concurrent.futures in Python 2.X |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 120 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 121 | |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 122 | Formatting style |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 123 | ================ |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 124 | |
| 125 | The formatting style used by YAPF is configurable and there are many "knobs" |
| 126 | that can be used to tune how YAPF does formatting. See the ``style.py`` module |
| 127 | for the full list. |
| 128 | |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame] | 129 | To control the style, run YAPF with the ``--style`` argument. It accepts one of |
| 130 | the predefined styles (e.g., ``pep8`` or ``google``), a path to a configuration |
| 131 | file that specifies the desired style, or a dictionary of key/value pairs. |
| 132 | |
| 133 | The config file is a simple listing of (case-insensitive) ``key = value`` pairs |
Bill Wendling | 6e8ca7b | 2015-10-25 01:16:43 -0700 | [diff] [blame] | 134 | with a ``[style]`` heading. For example: |
| 135 | |
Bill Wendling | 4f96aab | 2016-07-16 15:52:51 -0700 | [diff] [blame] | 136 | .. code-block:: |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 137 | |
| 138 | [style] |
| 139 | based_on_style = pep8 |
| 140 | spaces_before_comment = 4 |
| 141 | split_before_logical_operator = true |
| 142 | |
| 143 | The ``based_on_style`` setting determines which of the predefined styles this |
| 144 | custom style is based on (think of it like subclassing). |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 145 | |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame] | 146 | It's also possible to do the same on the command line with a dictionary. For |
Bill Wendling | 6e8ca7b | 2015-10-25 01:16:43 -0700 | [diff] [blame] | 147 | example: |
| 148 | |
Bill Wendling | 4f96aab | 2016-07-16 15:52:51 -0700 | [diff] [blame] | 149 | .. code-block:: |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame] | 150 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 151 | --style='{based_on_style: chromium, indent_width: 4}' |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame] | 152 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 153 | This will take the ``chromium`` base style and modify it to have four space |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame] | 154 | indentations. |
| 155 | |
Bill Wendling | 169790e | 2015-10-25 03:13:13 -0700 | [diff] [blame] | 156 | YAPF will search for the formatting style in the following manner: |
| 157 | |
| 158 | 1. Specified on the command line |
Joshua Moravec | d8d15af | 2015-11-05 13:16:46 -0600 | [diff] [blame] | 159 | 2. In the `[style]` section of a `.style.yapf` file in either the current |
Bill Wendling | 169790e | 2015-10-25 03:13:13 -0700 | [diff] [blame] | 160 | directory or one of its parent directories. |
Bill Wendling | 821a36f | 2016-07-13 23:02:16 -0700 | [diff] [blame] | 161 | 3. In the `[yapf]` section of a `setup.cfg` file in either the current |
Bill Wendling | 169790e | 2015-10-25 03:13:13 -0700 | [diff] [blame] | 162 | directory or one of its parent directories. |
| 163 | 4. In the `~/.config/yapf/style` file in your home directory. |
| 164 | |
| 165 | If none of those files are found, the default style is used (PEP8). |
| 166 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 167 | |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 168 | Example |
| 169 | ======= |
| 170 | |
Sam Clegg | 4357fa3 | 2015-04-08 12:21:46 -0700 | [diff] [blame] | 171 | An example of the type of formatting that YAPF can do, it will take this ugly |
| 172 | code: |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 173 | |
| 174 | .. code-block:: python |
| 175 | |
| 176 | x = { 'a':37,'b':42, |
| 177 | |
| 178 | 'c':927} |
| 179 | |
| 180 | y = 'hello ''world' |
| 181 | z = 'hello '+'world' |
| 182 | a = 'hello {}'.format('world') |
| 183 | class foo ( object ): |
| 184 | def f (self ): |
| 185 | return 37*-+2 |
| 186 | def g(self, x,y=42): |
| 187 | return y |
| 188 | def f ( a ) : |
| 189 | return 37+-+a[42-x : y**3] |
| 190 | |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 191 | and reformat it into: |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 192 | |
| 193 | .. code-block:: python |
| 194 | |
| 195 | x = {'a': 37, 'b': 42, 'c': 927} |
| 196 | |
| 197 | y = 'hello ' 'world' |
| 198 | z = 'hello ' + 'world' |
| 199 | a = 'hello {}'.format('world') |
| 200 | |
| 201 | |
| 202 | class foo(object): |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 203 | def f(self): |
| 204 | return 37 * -+2 |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 205 | |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 206 | def g(self, x, y=42): |
| 207 | return y |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 208 | |
| 209 | |
| 210 | def f(a): |
Bill Wendling | 8d8f512 | 2015-10-16 11:46:23 -0700 | [diff] [blame] | 211 | return 37 + -+a[42 - x:y**3] |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 212 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 213 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 214 | Example as a module |
| 215 | =================== |
| 216 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 217 | The two main APIs for calling yapf are ``FormatCode`` and ``FormatFile``, these |
| 218 | share several arguments which are described below: |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 219 | |
| 220 | .. code-block:: python |
| 221 | |
srinivasanramaraju | cfa7ead | 2017-02-14 00:06:58 -0800 | [diff] [blame] | 222 | >>> from yapf.yapflib.yapf_api import FormatCode # reformat a string of code |
Ćukasz Langa | 9408987 | 2015-09-22 16:02:26 -0700 | [diff] [blame] | 223 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 224 | >>> FormatCode("f ( a = 1, b = 2 )") |
| 225 | 'f(a=1, b=2)\n' |
| 226 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 227 | A ``style_config`` argument: Either a style name or a path to a file that contains |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 228 | formatting style settings. If None is specified, use the default style |
| 229 | as set in ``style.DEFAULT_STYLE_FACTORY``. |
| 230 | |
| 231 | .. code-block:: python |
| 232 | |
| 233 | >>> FormatCode("def g():\n return True", style_config='pep8') |
| 234 | 'def g():\n return True\n' |
| 235 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 236 | A ``lines`` argument: A list of tuples of lines (ints), [start, end], |
| 237 | that we want to format. The lines are 1-based indexed. It can be used by |
| 238 | third-party code (e.g., IDEs) when reformatting a snippet of code rather |
| 239 | than a whole file. |
| 240 | |
| 241 | .. code-block:: python |
| 242 | |
| 243 | >>> FormatCode("def g( ):\n a=1\n b = 2\n return a==b", lines=[(1, 1), (2, 3)]) |
| 244 | 'def g():\n a = 1\n b = 2\n return a==b\n' |
| 245 | |
| 246 | A ``print_diff`` (bool): Instead of returning the reformatted source, return a |
| 247 | diff that turns the formatted source into reformatter source. |
| 248 | |
| 249 | .. code-block:: python |
| 250 | |
| 251 | >>> print(FormatCode("a==b", filename="foo.py", print_diff=True)) |
Bill Wendling | b8645ea | 2015-06-30 22:27:56 -0700 | [diff] [blame] | 252 | --- foo.py (original) |
| 253 | +++ foo.py (reformatted) |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 254 | @@ -1 +1 @@ |
| 255 | -a==b |
| 256 | +a == b |
| 257 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 258 | Note: the ``filename`` argument for ``FormatCode`` is what is inserted into |
| 259 | the diff, the default is ``<unknown>``. |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 260 | |
| 261 | ``FormatFile`` returns reformatted code from the passed file along with its encoding: |
| 262 | |
| 263 | .. code-block:: python |
| 264 | |
srinivasanramaraju | cfa7ead | 2017-02-14 00:06:58 -0800 | [diff] [blame] | 265 | >>> from yapf.yapflib.yapf_api import FormatFile # reformat a file |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 266 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 267 | >>> print(open("foo.py").read()) # contents of file |
| 268 | a==b |
| 269 | |
| 270 | >>> FormatFile("foo.py") |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 271 | ('a == b\n', 'utf-8') |
| 272 | |
Bill Wendling | cfbb124 | 2015-09-20 12:08:18 -0700 | [diff] [blame] | 273 | The ``in-place`` argument saves the reformatted code back to the file: |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 274 | |
| 275 | .. code-block:: python |
| 276 | |
| 277 | >>> FormatFile("foo.py", in_place=True) |
| 278 | (None, 'utf-8') |
| 279 | |
| 280 | >>> print(open("foo.py").read()) # contents of file (now fixed) |
| 281 | a == b |
| 282 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 283 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 284 | Knobs |
| 285 | ===== |
| 286 | |
| 287 | ``ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT`` |
| 288 | Align closing bracket with visual indentation. |
| 289 | |
Bill Wendling | 996c3ee | 2016-05-25 23:52:24 -0700 | [diff] [blame] | 290 | ``ALLOW_MULTILINE_LAMBDAS`` |
| 291 | Allow lambdas to be formatted on more than one line. |
| 292 | |
Bill Wendling | 8d32136 | 2017-02-05 18:29:26 -0800 | [diff] [blame] | 293 | ``ALLOW_MULTILINE_DICTIONARY_KEYS`` |
| 294 | Allow dictionary keys to exist on multiple lines. For example: |
| 295 | |
| 296 | .. code-block:: python |
| 297 | |
| 298 | x = { |
| 299 | ('this is the first element of a tuple', |
| 300 | 'this is the second element of a tuple'): |
| 301 | value, |
| 302 | } |
| 303 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 304 | ``BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF`` |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 305 | Insert a blank line before a ``def`` or ``class`` immediately nested within |
Bill Wendling | 996c3ee | 2016-05-25 23:52:24 -0700 | [diff] [blame] | 306 | another ``def`` or ``class``. For example: |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 307 | |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 308 | .. code-block:: python |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 309 | |
| 310 | class Foo: |
| 311 | # <------ this blank line |
| 312 | def method(): |
| 313 | pass |
| 314 | |
Bill Wendling | 9f49752 | 2017-02-04 04:39:47 -0800 | [diff] [blame] | 315 | ``BLANK_LINE_BEFORE_CLASS_DOCSTRING`` |
| 316 | Insert a blank line before a class-level docstring. |
| 317 | |
Ben Plotnick | 7e08829 | 2016-06-09 18:29:56 -0700 | [diff] [blame] | 318 | ``COALESCE_BRACKETS`` |
| 319 | Do not split consecutive brackets. Only relevant when |
| 320 | ``DEDENT_CLOSING_BRACKETS`` is set. For example: |
| 321 | |
| 322 | .. code-block:: python |
| 323 | |
| 324 | call_func_that_takes_a_dict( |
| 325 | { |
| 326 | 'key1': 'value1', |
| 327 | 'key2': 'value2', |
| 328 | } |
| 329 | ) |
| 330 | |
| 331 | would reformat to: |
| 332 | |
| 333 | .. code-block:: python |
| 334 | |
| 335 | call_func_that_takes_a_dict({ |
| 336 | 'key1': 'value1', |
| 337 | 'key2': 'value2', |
| 338 | }) |
| 339 | |
| 340 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 341 | ``COLUMN_LIMIT`` |
James Broadhead | f4dd804 | 2016-01-07 14:40:19 +0000 | [diff] [blame] | 342 | The column limit (or max line-length) |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 343 | |
| 344 | ``CONTINUATION_INDENT_WIDTH`` |
| 345 | Indent width used for line continuations. |
| 346 | |
| 347 | ``DEDENT_CLOSING_BRACKETS`` |
| 348 | Put closing brackets on a separate line, dedented, if the bracketed |
| 349 | expression can't fit in a single line. Applies to all kinds of brackets, |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 350 | including function definitions and calls. For example: |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 351 | |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 352 | .. code-block:: python |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 353 | |
| 354 | config = { |
| 355 | 'key1': 'value1', |
| 356 | 'key2': 'value2', |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 357 | } # <--- this bracket is dedented and on a separate line |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 358 | |
| 359 | time_series = self.remote_client.query_entity_counters( |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 360 | entity='dev3246.region1', |
| 361 | key='dns.query_latency_tcp', |
| 362 | transform=Transformation.AVERAGE(window=timedelta(seconds=60)), |
| 363 | start_ts=now()-timedelta(days=3), |
| 364 | end_ts=now(), |
| 365 | ) # <--- this bracket is dedented and on a separate line |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 366 | |
Bill Wendling | 0e9b321 | 2017-01-31 14:41:00 -0800 | [diff] [blame] | 367 | ``EACH_DICT_ENTRY_ON_SEPARATE_LINE`` |
| 368 | Place each dictionary entry onto its own line. |
| 369 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 370 | ``I18N_COMMENT`` |
| 371 | The regex for an internationalization comment. The presence of this comment |
| 372 | stops reformatting of that line, because the comments are required to be |
| 373 | next to the string they translate. |
| 374 | |
| 375 | ``I18N_FUNCTION_CALL`` |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 376 | The internationalization function call names. The presence of this function |
Samuel Dion-Girardeau | 5ae6349 | 2016-09-08 21:01:20 -0400 | [diff] [blame] | 377 | stops reformatting on that line, because the string it has cannot be moved |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 378 | away from the i18n comment. |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 379 | |
| 380 | ``INDENT_DICTIONARY_VALUE`` |
| 381 | Indent the dictionary value if it cannot fit on the same line as the |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 382 | dictionary key. For example: |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 383 | |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 384 | .. code-block:: python |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 385 | |
| 386 | config = { |
| 387 | 'key1': |
| 388 | 'value1', |
| 389 | 'key2': value1 + |
| 390 | value2, |
| 391 | } |
| 392 | |
| 393 | ``INDENT_WIDTH`` |
| 394 | The number of columns to use for indentation. |
| 395 | |
| 396 | ``JOIN_MULTIPLE_LINES`` |
| 397 | Join short lines into one line. E.g., single line ``if`` statements. |
| 398 | |
Bill Wendling | 9dc7908 | 2016-05-10 00:23:53 -0700 | [diff] [blame] | 399 | ``SPACES_AROUND_POWER_OPERATOR`` |
| 400 | Set to ``True`` to prefer using spaces around ``**``. |
| 401 | |
Jiri Kuncar | f14bd17 | 2017-07-21 09:45:31 +0200 | [diff] [blame^] | 402 | ``NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS`` |
| 403 | Do not include spaces around selected binary operators. For example: |
| 404 | |
| 405 | .. code-block:: python |
| 406 | |
| 407 | 1 + 2 * 3 - 4 / 5 |
| 408 | |
| 409 | will be formatted as follows when configured with a value ``"*,/"``: |
| 410 | |
| 411 | .. code-block:: python |
| 412 | |
| 413 | 1 + 2*3 - 4/5 |
| 414 | |
Alexander Lenz | 5fda36a | 2016-08-26 17:27:57 +0200 | [diff] [blame] | 415 | ``SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN`` |
| 416 | Set to ``True`` to prefer spaces around the assignment operator for default |
| 417 | or keyword arguments. |
| 418 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 419 | ``SPACES_BEFORE_COMMENT`` |
| 420 | The number of spaces required before a trailing comment. |
| 421 | |
Bill Wendling | 996c3ee | 2016-05-25 23:52:24 -0700 | [diff] [blame] | 422 | ``SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET`` |
| 423 | Insert a space between the ending comma and closing bracket of a list, etc. |
| 424 | |
Bill Wendling | 982c5b3 | 2016-05-24 00:47:44 -0700 | [diff] [blame] | 425 | ``SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED`` |
| 426 | Split before arguments if the argument list is terminated by a comma. |
| 427 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 428 | ``SPLIT_BEFORE_BITWISE_OPERATOR`` |
| 429 | Set to ``True`` to prefer splitting before ``&``, ``|`` or ``^`` rather |
| 430 | than after. |
| 431 | |
Bill Wendling | 0e9b321 | 2017-01-31 14:41:00 -0800 | [diff] [blame] | 432 | ``SPLIT_BEFORE_DICT_SET_GENERATOR`` |
| 433 | Split before a dictionary or set generator (comp_for). For example, note |
| 434 | the split before the ``for``: |
| 435 | |
| 436 | .. code-block:: python |
| 437 | |
| 438 | foo = { |
| 439 | variable: 'Hello world, have a nice day!' |
| 440 | for variable in bar if variable != 42 |
| 441 | } |
| 442 | |
Bill Wendling | 996c3ee | 2016-05-25 23:52:24 -0700 | [diff] [blame] | 443 | ``SPLIT_BEFORE_FIRST_ARGUMENT`` |
| 444 | If an argument / parameter list is going to be split, then split before the |
| 445 | first argument. |
| 446 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 447 | ``SPLIT_BEFORE_LOGICAL_OPERATOR`` |
| 448 | Set to ``True`` to prefer splitting before ``and`` or ``or`` rather than |
| 449 | after. |
| 450 | |
| 451 | ``SPLIT_BEFORE_NAMED_ASSIGNS`` |
| 452 | Split named assignments onto individual lines. |
| 453 | |
| 454 | ``SPLIT_PENALTY_AFTER_OPENING_BRACKET`` |
| 455 | The penalty for splitting right after the opening bracket. |
| 456 | |
| 457 | ``SPLIT_PENALTY_AFTER_UNARY_OPERATOR`` |
| 458 | The penalty for splitting the line after a unary operator. |
| 459 | |
Bill Wendling | 996c3ee | 2016-05-25 23:52:24 -0700 | [diff] [blame] | 460 | ``SPLIT_PENALTY_BEFORE_IF_EXPR`` |
| 461 | The penalty for splitting right before an ``if`` expression. |
| 462 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 463 | ``SPLIT_PENALTY_BITWISE_OPERATOR`` |
| 464 | The penalty of splitting the line around the ``&``, ``|``, and ``^`` |
| 465 | operators. |
| 466 | |
| 467 | ``SPLIT_PENALTY_EXCESS_CHARACTER`` |
| 468 | The penalty for characters over the column limit. |
| 469 | |
| 470 | ``SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT`` |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 471 | The penalty incurred by adding a line split to the unwrapped line. The more |
| 472 | line splits added the higher the penalty. |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 473 | |
| 474 | ``SPLIT_PENALTY_IMPORT_NAMES`` |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 475 | The penalty of splitting a list of ``import as`` names. For example: |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 476 | |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 477 | .. code-block:: python |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 478 | |
| 479 | from a_very_long_or_indented_module_name_yada_yad import (long_argument_1, |
| 480 | long_argument_2, |
| 481 | long_argument_3) |
| 482 | |
| 483 | would reformat to something like: |
| 484 | |
Bill Wendling | c2a9e0d | 2016-05-24 20:51:04 -0700 | [diff] [blame] | 485 | .. code-block:: python |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 486 | |
| 487 | from a_very_long_or_indented_module_name_yada_yad import ( |
| 488 | long_argument_1, long_argument_2, long_argument_3) |
| 489 | |
| 490 | ``SPLIT_PENALTY_LOGICAL_OPERATOR`` |
| 491 | The penalty of splitting the line around the ``and`` and ``or`` operators. |
| 492 | |
Dracony | e582d63 | 2016-06-05 11:48:26 +0200 | [diff] [blame] | 493 | ``USE_TABS`` |
| 494 | Use the Tab character for indentation. |
| 495 | |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 496 | (Potentially) Frequently Asked Questions |
| 497 | ======================================== |
| 498 | |
| 499 | Why does YAPF destroy my awesome formatting? |
| 500 | -------------------------------------------- |
| 501 | |
| 502 | YAPF tries very hard to get the formatting correct. But for some code, it won't |
| 503 | be as good as hand-formatting. In particular, large data literals may become |
| 504 | horribly disfigured under YAPF. |
| 505 | |
Diogo Moitinho de Almeida | 24458d0 | 2015-04-02 17:57:22 -0700 | [diff] [blame] | 506 | The reason for this is many-fold. But in essence YAPF is simply a tool to help |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 507 | with development. It will format things to coincide with the style guide, but |
| 508 | that may not equate with readability. |
| 509 | |
| 510 | What can be done to alleviate this situation is to indicate regions YAPF should |
| 511 | ignore when reformatting something: |
| 512 | |
| 513 | .. code-block:: python |
| 514 | |
| 515 | # yapf: disable |
| 516 | FOO = { |
| 517 | # ... some very large, complex data literal. |
| 518 | } |
| 519 | |
| 520 | BAR = [ |
| 521 | # ... another large data literal. |
| 522 | ] |
| 523 | # yapf: enable |
| 524 | |
| 525 | You can also disable formatting for a single literal like this: |
| 526 | |
| 527 | .. code-block:: python |
| 528 | |
| 529 | BAZ = { |
Scott Sanderson | eda4e26 | 2015-07-05 21:10:06 -0400 | [diff] [blame] | 530 | (1, 2, 3, 4), |
| 531 | (5, 6, 7, 8), |
| 532 | (9, 10, 11, 12), |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 533 | } # yapf: disable |
| 534 | |
Ćukasz Langa | 9408987 | 2015-09-22 16:02:26 -0700 | [diff] [blame] | 535 | To preserve the nice dedented closing brackets, use the |
| 536 | ``dedent_closing_brackets`` in your style. Note that in this case all |
| 537 | brackets, including function definitions and calls, are going to use |
| 538 | that style. This provides consistency across the formatted codebase. |
| 539 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 540 | Why Not Improve Existing Tools? |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 541 | ------------------------------- |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 542 | |
| 543 | We wanted to use clang-format's reformatting algorithm. It's very powerful and |
| 544 | designed to come up with the best formatting possible. Existing tools were |
| 545 | created with different goals in mind, and would require extensive modifications |
| 546 | to convert to using clang-format's algorithm. |
| 547 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 548 | Can I Use YAPF In My Program? |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 549 | ----------------------------- |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 550 | |
| 551 | Please do! YAPF was designed to be used as a library as well as a command line |
| 552 | tool. This means that a tool or IDE plugin is free to use YAPF. |
| 553 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 554 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 555 | Gory Details |
| 556 | ============ |
| 557 | |
| 558 | Algorithm Design |
| 559 | ---------------- |
| 560 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 561 | The main data structure in YAPF is the ``UnwrappedLine`` object. It holds a list |
| 562 | of ``FormatToken``\s, that we would want to place on a single line if there were |
| 563 | 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] | 564 | 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] | 565 | formatter works on one ``UnwrappedLine`` object at a time. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 566 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 567 | An ``UnwrappedLine`` typically won't affect the formatting of lines before or |
| 568 | after it. There is a part of the algorithm that may join two or more |
| 569 | ``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] | 570 | short body can be placed on a single line: |
| 571 | |
| 572 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 573 | |
| 574 | if a == 42: continue |
| 575 | |
| 576 | YAPF's formatting algorithm creates a weighted tree that acts as the solution |
| 577 | space for the algorithm. Each node in the tree represents the result of a |
| 578 | formatting decision --- i.e., whether to split or not to split before a token. |
| 579 | Each formatting decision has a cost associated with it. Therefore, the cost is |
| 580 | realized on the edge between two nodes. (In reality, the weighted tree doesn't |
| 581 | have separate edge objects, so the cost resides on the nodes themselves.) |
| 582 | |
| 583 | For example, take the following Python code snippet. For the sake of this |
| 584 | example, assume that line (1) violates the column limit restriction and needs to |
| 585 | be reformatted. |
| 586 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 587 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 588 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 589 | def xxxxxxxxxxx(aaaaaaaaaaaa, bbbbbbbbb, cccccccc, dddddddd, eeeeee): # 1 |
| 590 | pass # 2 |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 591 | |
| 592 | 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] | 593 | ``FormattingDecisionState`` object) is the state of the line at that token given |
| 594 | the decision to split before the token or not. Note: the ``FormatDecisionState`` |
| 595 | objects are copied by value so each node in the graph is unique and a change in |
| 596 | one doesn't affect other nodes. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 597 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 598 | Heuristics are used to determine the costs of splitting or not splitting. |
| 599 | Because a node holds the state of the tree up to a token's insertion, it can |
| 600 | easily determine if a splitting decision will violate one of the style |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 601 | requirements. For instance, the heuristic is able to apply an extra penalty to |
| 602 | the edge when not splitting between the previous token and the one being added. |
| 603 | |
| 604 | There are some instances where we will never want to split the line, because |
| 605 | doing so will always be detrimental (i.e., it will require a backslash-newline, |
| 606 | 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] | 607 | first three tokens: ``def``, ``xxxxxxxxxxx``, and ``(``. Nor will we want to |
| 608 | split between the ``)`` and the ``:`` at the end. These regions are said to be |
| 609 | "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] | 610 | decision (left hand branch) within the unbreakable region. |
| 611 | |
| 612 | Now that we have the tree, we determine what the "best" formatting is by finding |
| 613 | the path through the tree with the lowest cost. |
| 614 | |
| 615 | And that's it! |