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 |
| 31 | code base of a project is simply piped through YAPF whenever modifications are |
| 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 | |
Eli Bendersky | e0e83c1 | 2015-04-06 20:23:30 -0700 | [diff] [blame] | 50 | To install YAPF from PyPI:: |
Eli Bendersky | 8a36536 | 2015-03-25 18:42:22 -0700 | [diff] [blame] | 51 | |
Eli Bendersky | e0e83c1 | 2015-04-06 20:23:30 -0700 | [diff] [blame] | 52 | $ pip install yapf |
| 53 | |
| 54 | YAPF is still considered in "alpha" stage, and the released version may change |
| 55 | often; therefore, the best way to keep up-to-date with the latest development |
| 56 | is to clone this repository. |
| 57 | |
| 58 | Note that if you intend to use YAPF as a command-line tool rather than as a |
| 59 | library, installation is not necessary. YAPF supports being run as a directory |
| 60 | by the Python interpreter. If you cloned/unzipped YAPF into ``DIR``, it's |
| 61 | possible to run:: |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 62 | |
Eli Bendersky | b3678b3 | 2015-03-25 14:16:11 -0700 | [diff] [blame] | 63 | $ PYTHONPATH=DIR python DIR/yapf [options] ... |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 64 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 65 | |
Eli Bendersky | 5eb8823 | 2015-03-27 06:27:11 -0700 | [diff] [blame] | 66 | Python versions |
| 67 | =============== |
| 68 | |
Eli Bendersky | a7bfe7e | 2015-04-05 06:33:18 -0700 | [diff] [blame] | 69 | YAPF supports Python 2.7 and 3.4.1+. |
Eli Bendersky | 5eb8823 | 2015-03-27 06:27:11 -0700 | [diff] [blame] | 70 | |
| 71 | YAPF requires the code it formats to be valid Python for the version YAPF itself |
| 72 | runs under. Therefore, if you format Python 3 code with YAPF, run YAPF itself |
| 73 | under Python 3 (and similarly for Python 2). |
| 74 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 75 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 76 | Usage |
| 77 | ===== |
| 78 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 79 | Options:: |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 80 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 81 | usage: yapf [-h] [-v] [-d | -i] [-r | -l START-END] [-e PATTERN] |
| 82 | [--style STYLE] [--style-help] [--no-local-style] |
| 83 | [--verify] |
| 84 | [files [files ...]] |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 85 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 86 | Formatter for Python code. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 87 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 88 | positional arguments: |
| 89 | files |
| 90 | |
| 91 | optional arguments: |
| 92 | -h, --help show this help message and exit |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 93 | -v, --version show version number and exit |
| 94 | -d, --diff print the diff for the fixed source |
| 95 | -i, --in-place make changes to files in place |
| 96 | -r, --recursive run recursively over directories |
| 97 | -l START-END, --lines START-END |
| 98 | range of lines to reformat, one-based |
| 99 | -e PATTERN, --exclude PATTERN |
| 100 | patterns for files to exclude from formatting |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 101 | --style STYLE specify formatting style: either a style name (for |
| 102 | example "pep8" or "google"), or the name of a file |
Sam Clegg | 5170c3a | 2015-04-16 12:18:58 -0700 | [diff] [blame] | 103 | with style settings. The default is pep8 unless a |
| 104 | .style.yapf file located in one of the parent |
| 105 | directories of the source file (or current directory |
| 106 | for stdin) |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 107 | --style-help show style settings and exit |
| 108 | --no-local-style don't search for local style definition (.style.yapf) |
| 109 | --verify try to verify reformatted code for syntax errors |
| 110 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 111 | |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 112 | Formatting style |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 113 | ================ |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 114 | |
| 115 | The formatting style used by YAPF is configurable and there are many "knobs" |
| 116 | that can be used to tune how YAPF does formatting. See the ``style.py`` module |
| 117 | for the full list. |
| 118 | |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame] | 119 | To control the style, run YAPF with the ``--style`` argument. It accepts one of |
| 120 | the predefined styles (e.g., ``pep8`` or ``google``), a path to a configuration |
| 121 | file that specifies the desired style, or a dictionary of key/value pairs. |
| 122 | |
| 123 | The config file is a simple listing of (case-insensitive) ``key = value`` pairs |
| 124 | with a ``[style]`` heading. For example:: |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 125 | |
| 126 | [style] |
| 127 | based_on_style = pep8 |
| 128 | spaces_before_comment = 4 |
| 129 | split_before_logical_operator = true |
| 130 | |
| 131 | The ``based_on_style`` setting determines which of the predefined styles this |
| 132 | custom style is based on (think of it like subclassing). |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 133 | |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame] | 134 | It's also possible to do the same on the command line with a dictionary. For |
| 135 | example:: |
| 136 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 137 | --style='{based_on_style: chromium, indent_width: 4}' |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame] | 138 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 139 | 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] | 140 | indentations. |
| 141 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 142 | |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 143 | Example |
| 144 | ======= |
| 145 | |
Sam Clegg | 4357fa3 | 2015-04-08 12:21:46 -0700 | [diff] [blame] | 146 | An example of the type of formatting that YAPF can do, it will take this ugly |
| 147 | code: |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 148 | |
| 149 | .. code-block:: python |
| 150 | |
| 151 | x = { 'a':37,'b':42, |
| 152 | |
| 153 | 'c':927} |
| 154 | |
| 155 | y = 'hello ''world' |
| 156 | z = 'hello '+'world' |
| 157 | a = 'hello {}'.format('world') |
| 158 | class foo ( object ): |
| 159 | def f (self ): |
| 160 | return 37*-+2 |
| 161 | def g(self, x,y=42): |
| 162 | return y |
| 163 | def f ( a ) : |
| 164 | return 37+-+a[42-x : y**3] |
| 165 | |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 166 | and reformat it into: |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 167 | |
| 168 | .. code-block:: python |
| 169 | |
| 170 | x = {'a': 37, 'b': 42, 'c': 927} |
| 171 | |
| 172 | y = 'hello ' 'world' |
| 173 | z = 'hello ' + 'world' |
| 174 | a = 'hello {}'.format('world') |
| 175 | |
| 176 | |
| 177 | class foo(object): |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 178 | def f(self): |
| 179 | return 37 * -+2 |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 180 | |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 181 | def g(self, x, y=42): |
| 182 | return y |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 183 | |
| 184 | |
| 185 | def f(a): |
Bill Wendling | 8d8f512 | 2015-10-16 11:46:23 -0700 | [diff] [blame] | 186 | return 37 + -+a[42 - x:y**3] |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 187 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 188 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 189 | Example as a module |
| 190 | =================== |
| 191 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 192 | The two main APIs for calling yapf are ``FormatCode`` and ``FormatFile``, these |
| 193 | share several arguments which are described below: |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 194 | |
| 195 | .. code-block:: python |
| 196 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 197 | >>> from yapf.yapf_api import FormatCode # reformat a string of code |
Ćukasz Langa | 9408987 | 2015-09-22 16:02:26 -0700 | [diff] [blame] | 198 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 199 | >>> FormatCode("f ( a = 1, b = 2 )") |
| 200 | 'f(a=1, b=2)\n' |
| 201 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 202 | 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] | 203 | formatting style settings. If None is specified, use the default style |
| 204 | as set in ``style.DEFAULT_STYLE_FACTORY``. |
| 205 | |
| 206 | .. code-block:: python |
| 207 | |
| 208 | >>> FormatCode("def g():\n return True", style_config='pep8') |
| 209 | 'def g():\n return True\n' |
| 210 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 211 | A ``lines`` argument: A list of tuples of lines (ints), [start, end], |
| 212 | that we want to format. The lines are 1-based indexed. It can be used by |
| 213 | third-party code (e.g., IDEs) when reformatting a snippet of code rather |
| 214 | than a whole file. |
| 215 | |
| 216 | .. code-block:: python |
| 217 | |
| 218 | >>> FormatCode("def g( ):\n a=1\n b = 2\n return a==b", lines=[(1, 1), (2, 3)]) |
| 219 | 'def g():\n a = 1\n b = 2\n return a==b\n' |
| 220 | |
| 221 | A ``print_diff`` (bool): Instead of returning the reformatted source, return a |
| 222 | diff that turns the formatted source into reformatter source. |
| 223 | |
| 224 | .. code-block:: python |
| 225 | |
| 226 | >>> print(FormatCode("a==b", filename="foo.py", print_diff=True)) |
Bill Wendling | b8645ea | 2015-06-30 22:27:56 -0700 | [diff] [blame] | 227 | --- foo.py (original) |
| 228 | +++ foo.py (reformatted) |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 229 | @@ -1 +1 @@ |
| 230 | -a==b |
| 231 | +a == b |
| 232 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 233 | Note: the ``filename`` argument for ``FormatCode`` is what is inserted into |
| 234 | the diff, the default is ``<unknown>``. |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 235 | |
| 236 | ``FormatFile`` returns reformatted code from the passed file along with its encoding: |
| 237 | |
| 238 | .. code-block:: python |
| 239 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 240 | >>> from yapf.yapf_api import FormatFile # reformat a file |
| 241 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 242 | >>> print(open("foo.py").read()) # contents of file |
| 243 | a==b |
| 244 | |
| 245 | >>> FormatFile("foo.py") |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 246 | ('a == b\n', 'utf-8') |
| 247 | |
Bill Wendling | cfbb124 | 2015-09-20 12:08:18 -0700 | [diff] [blame] | 248 | The ``in-place`` argument saves the reformatted code back to the file: |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 249 | |
| 250 | .. code-block:: python |
| 251 | |
| 252 | >>> FormatFile("foo.py", in_place=True) |
| 253 | (None, 'utf-8') |
| 254 | |
| 255 | >>> print(open("foo.py").read()) # contents of file (now fixed) |
| 256 | a == b |
| 257 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 258 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 259 | Knobs |
| 260 | ===== |
| 261 | |
| 262 | ``ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT`` |
| 263 | Align closing bracket with visual indentation. |
| 264 | |
| 265 | ``BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF`` |
| 266 | Insert a blank line before a 'def' or 'class' immediately nested |
| 267 | within another 'def' or 'class'. |
| 268 | |
| 269 | For example: |
| 270 | |
Alfie Bowman | d1ae8a8 | 2015-10-21 12:30:43 +0100 | [diff] [blame^] | 271 | .. code-block:: python |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 272 | |
| 273 | class Foo: |
| 274 | # <------ this blank line |
| 275 | def method(): |
| 276 | pass |
| 277 | |
| 278 | ``COLUMN_LIMIT`` |
| 279 | The column limit. |
| 280 | |
| 281 | ``CONTINUATION_INDENT_WIDTH`` |
| 282 | Indent width used for line continuations. |
| 283 | |
| 284 | ``DEDENT_CLOSING_BRACKETS`` |
| 285 | Put closing brackets on a separate line, dedented, if the bracketed |
| 286 | expression can't fit in a single line. Applies to all kinds of brackets, |
| 287 | including function definitions and calls. |
| 288 | |
| 289 | For example: |
| 290 | |
Alfie Bowman | d1ae8a8 | 2015-10-21 12:30:43 +0100 | [diff] [blame^] | 291 | .. code-block:: python |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 292 | |
| 293 | config = { |
| 294 | 'key1': 'value1', |
| 295 | 'key2': 'value2', |
| 296 | } # <--- this bracket is dedented and on a separate line |
| 297 | |
| 298 | time_series = self.remote_client.query_entity_counters( |
| 299 | entity='dev3246.region1', |
| 300 | key='dns.query_latency_tcp', |
| 301 | transform=Transformation.AVERAGE(window=timedelta(seconds=60)), |
| 302 | start_ts=now()-timedelta(days=3), |
| 303 | end_ts=now(), |
| 304 | ) # <--- this bracket is dedented and on a separate line |
| 305 | |
| 306 | ``I18N_COMMENT`` |
| 307 | The regex for an internationalization comment. The presence of this comment |
| 308 | stops reformatting of that line, because the comments are required to be |
| 309 | next to the string they translate. |
| 310 | |
| 311 | ``I18N_FUNCTION_CALL`` |
| 312 | The internationalization function call names. The presence of this |
| 313 | function stops reformattting on that line, because the string it has |
| 314 | cannot be moved away from the i18n comment. |
| 315 | |
| 316 | ``INDENT_DICTIONARY_VALUE`` |
| 317 | Indent the dictionary value if it cannot fit on the same line as the |
| 318 | dictionary key. |
| 319 | |
| 320 | For example: |
| 321 | |
Alfie Bowman | d1ae8a8 | 2015-10-21 12:30:43 +0100 | [diff] [blame^] | 322 | .. code-block:: python |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 323 | |
| 324 | config = { |
| 325 | 'key1': |
| 326 | 'value1', |
| 327 | 'key2': value1 + |
| 328 | value2, |
| 329 | } |
| 330 | |
| 331 | ``INDENT_WIDTH`` |
| 332 | The number of columns to use for indentation. |
| 333 | |
| 334 | ``JOIN_MULTIPLE_LINES`` |
| 335 | Join short lines into one line. E.g., single line ``if`` statements. |
| 336 | |
| 337 | ``SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET`` |
| 338 | Insert a space between the ending comma and closing bracket of a list, |
| 339 | etc. |
| 340 | |
| 341 | ``SPACES_BEFORE_COMMENT`` |
| 342 | The number of spaces required before a trailing comment. |
| 343 | |
| 344 | ``SPLIT_BEFORE_BITWISE_OPERATOR`` |
| 345 | Set to ``True`` to prefer splitting before ``&``, ``|`` or ``^`` rather |
| 346 | than after. |
| 347 | |
| 348 | ``SPLIT_BEFORE_LOGICAL_OPERATOR`` |
| 349 | Set to ``True`` to prefer splitting before ``and`` or ``or`` rather than |
| 350 | after. |
| 351 | |
| 352 | ``SPLIT_BEFORE_NAMED_ASSIGNS`` |
| 353 | Split named assignments onto individual lines. |
| 354 | |
| 355 | ``SPLIT_PENALTY_AFTER_OPENING_BRACKET`` |
| 356 | The penalty for splitting right after the opening bracket. |
| 357 | |
| 358 | ``SPLIT_PENALTY_AFTER_UNARY_OPERATOR`` |
| 359 | The penalty for splitting the line after a unary operator. |
| 360 | |
| 361 | ``SPLIT_PENALTY_BITWISE_OPERATOR`` |
| 362 | The penalty of splitting the line around the ``&``, ``|``, and ``^`` |
| 363 | operators. |
| 364 | |
| 365 | ``SPLIT_PENALTY_EXCESS_CHARACTER`` |
| 366 | The penalty for characters over the column limit. |
| 367 | |
| 368 | ``SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT`` |
| 369 | The penalty incurred by adding a line split to the unwrapped line. The |
| 370 | more line splits added the higher the penalty. |
| 371 | |
| 372 | ``SPLIT_PENALTY_IMPORT_NAMES`` |
| 373 | The penalty of splitting a list of ``import as`` names. |
| 374 | |
| 375 | For example: |
| 376 | |
Alfie Bowman | d1ae8a8 | 2015-10-21 12:30:43 +0100 | [diff] [blame^] | 377 | .. code-block:: python |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 378 | |
| 379 | from a_very_long_or_indented_module_name_yada_yad import (long_argument_1, |
| 380 | long_argument_2, |
| 381 | long_argument_3) |
| 382 | |
| 383 | would reformat to something like: |
| 384 | |
Alfie Bowman | d1ae8a8 | 2015-10-21 12:30:43 +0100 | [diff] [blame^] | 385 | .. code-block:: python |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 386 | |
| 387 | from a_very_long_or_indented_module_name_yada_yad import ( |
| 388 | long_argument_1, long_argument_2, long_argument_3) |
| 389 | |
| 390 | ``SPLIT_PENALTY_LOGICAL_OPERATOR`` |
| 391 | The penalty of splitting the line around the ``and`` and ``or`` operators. |
| 392 | |
| 393 | |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 394 | (Potentially) Frequently Asked Questions |
| 395 | ======================================== |
| 396 | |
| 397 | Why does YAPF destroy my awesome formatting? |
| 398 | -------------------------------------------- |
| 399 | |
| 400 | YAPF tries very hard to get the formatting correct. But for some code, it won't |
| 401 | be as good as hand-formatting. In particular, large data literals may become |
| 402 | horribly disfigured under YAPF. |
| 403 | |
Diogo Moitinho de Almeida | 24458d0 | 2015-04-02 17:57:22 -0700 | [diff] [blame] | 404 | 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] | 405 | with development. It will format things to coincide with the style guide, but |
| 406 | that may not equate with readability. |
| 407 | |
| 408 | What can be done to alleviate this situation is to indicate regions YAPF should |
| 409 | ignore when reformatting something: |
| 410 | |
| 411 | .. code-block:: python |
| 412 | |
| 413 | # yapf: disable |
| 414 | FOO = { |
| 415 | # ... some very large, complex data literal. |
| 416 | } |
| 417 | |
| 418 | BAR = [ |
| 419 | # ... another large data literal. |
| 420 | ] |
| 421 | # yapf: enable |
| 422 | |
| 423 | You can also disable formatting for a single literal like this: |
| 424 | |
| 425 | .. code-block:: python |
| 426 | |
| 427 | BAZ = { |
Scott Sanderson | eda4e26 | 2015-07-05 21:10:06 -0400 | [diff] [blame] | 428 | (1, 2, 3, 4), |
| 429 | (5, 6, 7, 8), |
| 430 | (9, 10, 11, 12), |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 431 | } # yapf: disable |
| 432 | |
Ćukasz Langa | 9408987 | 2015-09-22 16:02:26 -0700 | [diff] [blame] | 433 | To preserve the nice dedented closing brackets, use the |
| 434 | ``dedent_closing_brackets`` in your style. Note that in this case all |
| 435 | brackets, including function definitions and calls, are going to use |
| 436 | that style. This provides consistency across the formatted codebase. |
| 437 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 438 | Why Not Improve Existing Tools? |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 439 | ------------------------------- |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 440 | |
| 441 | We wanted to use clang-format's reformatting algorithm. It's very powerful and |
| 442 | designed to come up with the best formatting possible. Existing tools were |
| 443 | created with different goals in mind, and would require extensive modifications |
| 444 | to convert to using clang-format's algorithm. |
| 445 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 446 | Can I Use YAPF In My Program? |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 447 | ----------------------------- |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 448 | |
| 449 | Please do! YAPF was designed to be used as a library as well as a command line |
| 450 | tool. This means that a tool or IDE plugin is free to use YAPF. |
| 451 | |
Bill Wendling | f09121c | 2015-10-20 22:59:33 -0700 | [diff] [blame] | 452 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 453 | Gory Details |
| 454 | ============ |
| 455 | |
| 456 | Algorithm Design |
| 457 | ---------------- |
| 458 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 459 | The main data structure in YAPF is the ``UnwrappedLine`` object. It holds a list |
| 460 | of ``FormatToken``\s, that we would want to place on a single line if there were |
| 461 | 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] | 462 | 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] | 463 | formatter works on one ``UnwrappedLine`` object at a time. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 464 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 465 | An ``UnwrappedLine`` typically won't affect the formatting of lines before or |
| 466 | after it. There is a part of the algorithm that may join two or more |
| 467 | ``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] | 468 | short body can be placed on a single line: |
| 469 | |
| 470 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 471 | |
| 472 | if a == 42: continue |
| 473 | |
| 474 | YAPF's formatting algorithm creates a weighted tree that acts as the solution |
| 475 | space for the algorithm. Each node in the tree represents the result of a |
| 476 | formatting decision --- i.e., whether to split or not to split before a token. |
| 477 | Each formatting decision has a cost associated with it. Therefore, the cost is |
| 478 | realized on the edge between two nodes. (In reality, the weighted tree doesn't |
| 479 | have separate edge objects, so the cost resides on the nodes themselves.) |
| 480 | |
| 481 | For example, take the following Python code snippet. For the sake of this |
| 482 | example, assume that line (1) violates the column limit restriction and needs to |
| 483 | be reformatted. |
| 484 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 485 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 486 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 487 | def xxxxxxxxxxx(aaaaaaaaaaaa, bbbbbbbbb, cccccccc, dddddddd, eeeeee): # 1 |
| 488 | pass # 2 |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 489 | |
| 490 | 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] | 491 | ``FormattingDecisionState`` object) is the state of the line at that token given |
| 492 | the decision to split before the token or not. Note: the ``FormatDecisionState`` |
| 493 | objects are copied by value so each node in the graph is unique and a change in |
| 494 | one doesn't affect other nodes. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 495 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 496 | Heuristics are used to determine the costs of splitting or not splitting. |
| 497 | Because a node holds the state of the tree up to a token's insertion, it can |
| 498 | easily determine if a splitting decision will violate one of the style |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 499 | requirements. For instance, the heuristic is able to apply an extra penalty to |
| 500 | the edge when not splitting between the previous token and the one being added. |
| 501 | |
| 502 | There are some instances where we will never want to split the line, because |
| 503 | doing so will always be detrimental (i.e., it will require a backslash-newline, |
| 504 | 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] | 505 | first three tokens: ``def``, ``xxxxxxxxxxx``, and ``(``. Nor will we want to |
| 506 | split between the ``)`` and the ``:`` at the end. These regions are said to be |
| 507 | "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] | 508 | decision (left hand branch) within the unbreakable region. |
| 509 | |
| 510 | Now that we have the tree, we determine what the "best" formatting is by finding |
| 511 | the path through the tree with the lowest cost. |
| 512 | |
| 513 | And that's it! |