blob: e3085401f4020a2f1d395e3d38f6fb3a6f569cb1 [file] [log] [blame]
Bill Wendling7d623452015-03-18 13:36:07 -07001====
2YAPF
3====
4
Bill Wendling19c44d02015-04-07 23:48:05 -07005.. image:: https://badge.fury.io/py/yapf.svg
6 :target: http://badge.fury.io/py/yapf
7 :alt: PyPI version
8
Bill Wendlingfb8ab382015-03-18 20:24:14 -07009.. image:: https://travis-ci.org/google/yapf.svg?branch=master
10 :target: https://travis-ci.org/google/yapf
11 :alt: Build status
12
Bill Wendling14ac8812015-04-05 02:47:32 -070013.. 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 Wendlingf09121c2015-10-20 22:59:33 -070017
Bill Wendling7d623452015-03-18 13:36:07 -070018Introduction
19============
20
Bill Wendling5632e672015-03-29 17:06:07 -070021Most of the current formatters for Python --- e.g., autopep8, and pep8ify ---
22are made to remove lint errors from code. This has some obvious limitations.
23For instance, code that conforms to the PEP 8 guidelines may not be
24reformatted. But it doesn't mean that the code looks good.
Bill Wendling7d623452015-03-18 13:36:07 -070025
26YAPF takes a different approach. It's based off of 'clang-format', developed by
27Daniel Jasper. In essence, the algorithm takes the code and reformats it to the
28best formatting that conforms to the style guide, even if the original code
Peter Bengtsson1c60ad72015-03-24 20:05:39 -070029didn't violate the style guide. The idea is also similar to the 'gofmt' tool for
Eli Bendersky07072f82015-03-23 06:41:14 -070030the Go programming language: end all holy wars about formatting - if the whole
31code base of a project is simply piped through YAPF whenever modifications are
32made, the style remains consistent throughout the project and there's no point
33arguing about style in every code review.
Bill Wendling7d623452015-03-18 13:36:07 -070034
35The ultimate goal is that the code YAPF produces is as good as the code that a
Bill Wendling8fb9c482015-03-29 17:32:07 -070036programmer would write if they were following the style guide. It takes away
37some of the drudgery of maintaining your code.
Bill Wendling7d623452015-03-18 13:36:07 -070038
Bill Wendlingf5e50b62015-03-28 23:38:12 -070039.. footer::
Bill Wendling52e04112015-03-18 20:42:26 -070040
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 Wendling7d623452015-03-18 13:36:07 -070044.. contents::
45
Bill Wendlingf09121c2015-10-20 22:59:33 -070046
Bill Wendling7d623452015-03-18 13:36:07 -070047Installation
48============
49
Bill Wendling6e8ca7b2015-10-25 01:16:43 -070050To install YAPF from PyPI:
51
52.. code-block:: shell
Eli Bendersky8a365362015-03-25 18:42:22 -070053
Eli Benderskye0e83c12015-04-06 20:23:30 -070054 $ pip install yapf
55
56YAPF is still considered in "alpha" stage, and the released version may change
57often; therefore, the best way to keep up-to-date with the latest development
58is to clone this repository.
59
60Note that if you intend to use YAPF as a command-line tool rather than as a
61library, installation is not necessary. YAPF supports being run as a directory
62by the Python interpreter. If you cloned/unzipped YAPF into ``DIR``, it's
Bill Wendling6e8ca7b2015-10-25 01:16:43 -070063possible to run:
64
65.. code-block:: shell
Eli Bendersky07072f82015-03-23 06:41:14 -070066
Eli Benderskyb3678b32015-03-25 14:16:11 -070067 $ PYTHONPATH=DIR python DIR/yapf [options] ...
Eli Bendersky07072f82015-03-23 06:41:14 -070068
Bill Wendlingf09121c2015-10-20 22:59:33 -070069
Eli Bendersky5eb88232015-03-27 06:27:11 -070070Python versions
71===============
72
Eli Benderskya7bfe7e2015-04-05 06:33:18 -070073YAPF supports Python 2.7 and 3.4.1+.
Eli Bendersky5eb88232015-03-27 06:27:11 -070074
75YAPF requires the code it formats to be valid Python for the version YAPF itself
76runs under. Therefore, if you format Python 3 code with YAPF, run YAPF itself
77under Python 3 (and similarly for Python 2).
78
Bill Wendlingf09121c2015-10-20 22:59:33 -070079
Bill Wendling7d623452015-03-18 13:36:07 -070080Usage
81=====
82
Bill Wendlingfa22c892015-03-18 13:42:25 -070083Options::
Bill Wendling7d623452015-03-18 13:36:07 -070084
Bill Wendlingf09121c2015-10-20 22:59:33 -070085 usage: yapf [-h] [-v] [-d | -i] [-r | -l START-END] [-e PATTERN]
86 [--style STYLE] [--style-help] [--no-local-style]
87 [--verify]
88 [files [files ...]]
Bill Wendling7d623452015-03-18 13:36:07 -070089
Bill Wendlingfa22c892015-03-18 13:42:25 -070090 Formatter for Python code.
Bill Wendling7d623452015-03-18 13:36:07 -070091
Bill Wendlingfa22c892015-03-18 13:42:25 -070092 positional arguments:
93 files
94
95 optional arguments:
96 -h, --help show this help message and exit
Bill Wendlingf09121c2015-10-20 22:59:33 -070097 -v, --version show version number and exit
98 -d, --diff print the diff for the fixed source
99 -i, --in-place make changes to files in place
100 -r, --recursive run recursively over directories
101 -l START-END, --lines START-END
102 range of lines to reformat, one-based
103 -e PATTERN, --exclude PATTERN
104 patterns for files to exclude from formatting
Eli Bendersky83d2bd02015-03-23 06:33:48 -0700105 --style STYLE specify formatting style: either a style name (for
106 example "pep8" or "google"), or the name of a file
Sam Clegg5170c3a2015-04-16 12:18:58 -0700107 with style settings. The default is pep8 unless a
Bill Wendling6e8ca7b2015-10-25 01:16:43 -0700108 .style.yapf or setup.cfg file located in one of the
109 parent directories of the source file (or current
110 directory for stdin)
Bill Wendlingf09121c2015-10-20 22:59:33 -0700111 --style-help show style settings and exit
112 --no-local-style don't search for local style definition (.style.yapf)
113 --verify try to verify reformatted code for syntax errors
114
Bill Wendling7d623452015-03-18 13:36:07 -0700115
Eli Bendersky83d2bd02015-03-23 06:33:48 -0700116Formatting style
Bill Wendlingf5e50b62015-03-28 23:38:12 -0700117================
Eli Bendersky83d2bd02015-03-23 06:33:48 -0700118
119The formatting style used by YAPF is configurable and there are many "knobs"
120that can be used to tune how YAPF does formatting. See the ``style.py`` module
121for the full list.
122
Bill Wendlingc0167792015-04-02 01:58:39 -0700123To control the style, run YAPF with the ``--style`` argument. It accepts one of
124the predefined styles (e.g., ``pep8`` or ``google``), a path to a configuration
125file that specifies the desired style, or a dictionary of key/value pairs.
126
127The config file is a simple listing of (case-insensitive) ``key = value`` pairs
Bill Wendling6e8ca7b2015-10-25 01:16:43 -0700128with a ``[style]`` heading. For example:
129
130.. code-block:: guess
Eli Bendersky83d2bd02015-03-23 06:33:48 -0700131
132 [style]
133 based_on_style = pep8
134 spaces_before_comment = 4
135 split_before_logical_operator = true
136
137The ``based_on_style`` setting determines which of the predefined styles this
138custom style is based on (think of it like subclassing).
Bill Wendling7d623452015-03-18 13:36:07 -0700139
Bill Wendlingc0167792015-04-02 01:58:39 -0700140It's also possible to do the same on the command line with a dictionary. For
Bill Wendling6e8ca7b2015-10-25 01:16:43 -0700141example:
142
143.. code-block:: guess
Bill Wendlingc0167792015-04-02 01:58:39 -0700144
Bill Wendlingf09121c2015-10-20 22:59:33 -0700145 --style='{based_on_style: chromium, indent_width: 4}'
Bill Wendlingc0167792015-04-02 01:58:39 -0700146
Bill Wendlingf09121c2015-10-20 22:59:33 -0700147This will take the ``chromium`` base style and modify it to have four space
Bill Wendlingc0167792015-04-02 01:58:39 -0700148indentations.
149
Bill Wendlingf09121c2015-10-20 22:59:33 -0700150
Bill Wendlingf5e50b62015-03-28 23:38:12 -0700151Example
152=======
153
Sam Clegg4357fa32015-04-08 12:21:46 -0700154An example of the type of formatting that YAPF can do, it will take this ugly
155code:
Bill Wendlingf5e50b62015-03-28 23:38:12 -0700156
157.. code-block:: python
158
159 x = { 'a':37,'b':42,
160
161 'c':927}
162
163 y = 'hello ''world'
164 z = 'hello '+'world'
165 a = 'hello {}'.format('world')
166 class foo ( object ):
167 def f (self ):
168 return 37*-+2
169 def g(self, x,y=42):
170 return y
171 def f ( a ) :
172 return 37+-+a[42-x : y**3]
173
Bill Wendling8fb9c482015-03-29 17:32:07 -0700174and reformat it into:
Bill Wendlingf5e50b62015-03-28 23:38:12 -0700175
176.. code-block:: python
177
178 x = {'a': 37, 'b': 42, 'c': 927}
179
180 y = 'hello ' 'world'
181 z = 'hello ' + 'world'
182 a = 'hello {}'.format('world')
183
184
185 class foo(object):
Bill Wendling5632e672015-03-29 17:06:07 -0700186 def f(self):
187 return 37 * -+2
Bill Wendlingf5e50b62015-03-28 23:38:12 -0700188
Bill Wendling5632e672015-03-29 17:06:07 -0700189 def g(self, x, y=42):
190 return y
Bill Wendlingf5e50b62015-03-28 23:38:12 -0700191
192
193 def f(a):
Bill Wendling8d8f5122015-10-16 11:46:23 -0700194 return 37 + -+a[42 - x:y**3]
Bill Wendlingf5e50b62015-03-28 23:38:12 -0700195
Bill Wendlingf09121c2015-10-20 22:59:33 -0700196
Andy Haydena00a6bf2015-06-15 18:47:41 -0700197Example as a module
198===================
199
Andy Hayden4af71682015-06-17 15:42:43 -0700200The two main APIs for calling yapf are ``FormatCode`` and ``FormatFile``, these
201share several arguments which are described below:
Andy Haydena00a6bf2015-06-15 18:47:41 -0700202
203.. code-block:: python
204
Andy Hayden4af71682015-06-17 15:42:43 -0700205 >>> from yapf.yapf_api import FormatCode # reformat a string of code
Ɓukasz Langa94089872015-09-22 16:02:26 -0700206
Andy Haydena00a6bf2015-06-15 18:47:41 -0700207 >>> FormatCode("f ( a = 1, b = 2 )")
208 'f(a=1, b=2)\n'
209
Andy Hayden4af71682015-06-17 15:42:43 -0700210A ``style_config`` argument: Either a style name or a path to a file that contains
Andy Haydena00a6bf2015-06-15 18:47:41 -0700211formatting style settings. If None is specified, use the default style
212as set in ``style.DEFAULT_STYLE_FACTORY``.
213
214.. code-block:: python
215
216 >>> FormatCode("def g():\n return True", style_config='pep8')
217 'def g():\n return True\n'
218
Andy Haydena00a6bf2015-06-15 18:47:41 -0700219A ``lines`` argument: A list of tuples of lines (ints), [start, end],
220that we want to format. The lines are 1-based indexed. It can be used by
221third-party code (e.g., IDEs) when reformatting a snippet of code rather
222than a whole file.
223
224.. code-block:: python
225
226 >>> FormatCode("def g( ):\n a=1\n b = 2\n return a==b", lines=[(1, 1), (2, 3)])
227 'def g():\n a = 1\n b = 2\n return a==b\n'
228
229A ``print_diff`` (bool): Instead of returning the reformatted source, return a
230diff that turns the formatted source into reformatter source.
231
232.. code-block:: python
233
234 >>> print(FormatCode("a==b", filename="foo.py", print_diff=True))
Bill Wendlingb8645ea2015-06-30 22:27:56 -0700235 --- foo.py (original)
236 +++ foo.py (reformatted)
Andy Haydena00a6bf2015-06-15 18:47:41 -0700237 @@ -1 +1 @@
238 -a==b
239 +a == b
240
Andy Hayden4af71682015-06-17 15:42:43 -0700241Note: the ``filename`` argument for ``FormatCode`` is what is inserted into
242the diff, the default is ``<unknown>``.
Andy Haydena00a6bf2015-06-15 18:47:41 -0700243
244``FormatFile`` returns reformatted code from the passed file along with its encoding:
245
246.. code-block:: python
247
Andy Hayden4af71682015-06-17 15:42:43 -0700248 >>> from yapf.yapf_api import FormatFile # reformat a file
249
Andy Haydena00a6bf2015-06-15 18:47:41 -0700250 >>> print(open("foo.py").read()) # contents of file
251 a==b
252
253 >>> FormatFile("foo.py")
Andy Hayden4af71682015-06-17 15:42:43 -0700254 ('a == b\n', 'utf-8')
255
Bill Wendlingcfbb1242015-09-20 12:08:18 -0700256The ``in-place`` argument saves the reformatted code back to the file:
Andy Hayden4af71682015-06-17 15:42:43 -0700257
258.. code-block:: python
259
260 >>> FormatFile("foo.py", in_place=True)
261 (None, 'utf-8')
262
263 >>> print(open("foo.py").read()) # contents of file (now fixed)
264 a == b
265
Andy Haydena00a6bf2015-06-15 18:47:41 -0700266
Bill Wendlingf09121c2015-10-20 22:59:33 -0700267Knobs
268=====
269
270``ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT``
271 Align closing bracket with visual indentation.
272
273``BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF``
274 Insert a blank line before a 'def' or 'class' immediately nested
275 within another 'def' or 'class'.
276
277 For example:
278
Alfie Bowmand1ae8a82015-10-21 12:30:43 +0100279.. code-block:: python
Bill Wendlingf09121c2015-10-20 22:59:33 -0700280
281 class Foo:
282 # <------ this blank line
283 def method():
284 pass
285
286``COLUMN_LIMIT``
287 The column limit.
288
289``CONTINUATION_INDENT_WIDTH``
290 Indent width used for line continuations.
291
292``DEDENT_CLOSING_BRACKETS``
293 Put closing brackets on a separate line, dedented, if the bracketed
294 expression can't fit in a single line. Applies to all kinds of brackets,
295 including function definitions and calls.
296
297 For example:
298
Alfie Bowmand1ae8a82015-10-21 12:30:43 +0100299.. code-block:: python
Bill Wendlingf09121c2015-10-20 22:59:33 -0700300
301 config = {
302 'key1': 'value1',
303 'key2': 'value2',
304 } # <--- this bracket is dedented and on a separate line
305
306 time_series = self.remote_client.query_entity_counters(
307 entity='dev3246.region1',
308 key='dns.query_latency_tcp',
309 transform=Transformation.AVERAGE(window=timedelta(seconds=60)),
310 start_ts=now()-timedelta(days=3),
311 end_ts=now(),
312 ) # <--- this bracket is dedented and on a separate line
313
314``I18N_COMMENT``
315 The regex for an internationalization comment. The presence of this comment
316 stops reformatting of that line, because the comments are required to be
317 next to the string they translate.
318
319``I18N_FUNCTION_CALL``
320 The internationalization function call names. The presence of this
321 function stops reformattting on that line, because the string it has
322 cannot be moved away from the i18n comment.
323
324``INDENT_DICTIONARY_VALUE``
325 Indent the dictionary value if it cannot fit on the same line as the
326 dictionary key.
327
328 For example:
329
Alfie Bowmand1ae8a82015-10-21 12:30:43 +0100330.. code-block:: python
Bill Wendlingf09121c2015-10-20 22:59:33 -0700331
332 config = {
333 'key1':
334 'value1',
335 'key2': value1 +
336 value2,
337 }
338
339``INDENT_WIDTH``
340 The number of columns to use for indentation.
341
342``JOIN_MULTIPLE_LINES``
343 Join short lines into one line. E.g., single line ``if`` statements.
344
345``SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET``
346 Insert a space between the ending comma and closing bracket of a list,
347 etc.
348
349``SPACES_BEFORE_COMMENT``
350 The number of spaces required before a trailing comment.
351
352``SPLIT_BEFORE_BITWISE_OPERATOR``
353 Set to ``True`` to prefer splitting before ``&``, ``|`` or ``^`` rather
354 than after.
355
356``SPLIT_BEFORE_LOGICAL_OPERATOR``
357 Set to ``True`` to prefer splitting before ``and`` or ``or`` rather than
358 after.
359
360``SPLIT_BEFORE_NAMED_ASSIGNS``
361 Split named assignments onto individual lines.
362
363``SPLIT_PENALTY_AFTER_OPENING_BRACKET``
364 The penalty for splitting right after the opening bracket.
365
366``SPLIT_PENALTY_AFTER_UNARY_OPERATOR``
367 The penalty for splitting the line after a unary operator.
368
369``SPLIT_PENALTY_BITWISE_OPERATOR``
370 The penalty of splitting the line around the ``&``, ``|``, and ``^``
371 operators.
372
373``SPLIT_PENALTY_EXCESS_CHARACTER``
374 The penalty for characters over the column limit.
375
376``SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT``
377 The penalty incurred by adding a line split to the unwrapped line. The
378 more line splits added the higher the penalty.
379
380``SPLIT_PENALTY_IMPORT_NAMES``
381 The penalty of splitting a list of ``import as`` names.
382
383 For example:
384
Alfie Bowmand1ae8a82015-10-21 12:30:43 +0100385.. code-block:: python
Bill Wendlingf09121c2015-10-20 22:59:33 -0700386
387 from a_very_long_or_indented_module_name_yada_yad import (long_argument_1,
388 long_argument_2,
389 long_argument_3)
390
391 would reformat to something like:
392
Alfie Bowmand1ae8a82015-10-21 12:30:43 +0100393.. code-block:: python
Bill Wendlingf09121c2015-10-20 22:59:33 -0700394
395 from a_very_long_or_indented_module_name_yada_yad import (
396 long_argument_1, long_argument_2, long_argument_3)
397
398``SPLIT_PENALTY_LOGICAL_OPERATOR``
399 The penalty of splitting the line around the ``and`` and ``or`` operators.
400
401
Bill Wendling8fb9c482015-03-29 17:32:07 -0700402(Potentially) Frequently Asked Questions
403========================================
404
405Why does YAPF destroy my awesome formatting?
406--------------------------------------------
407
408YAPF tries very hard to get the formatting correct. But for some code, it won't
409be as good as hand-formatting. In particular, large data literals may become
410horribly disfigured under YAPF.
411
Diogo Moitinho de Almeida24458d02015-04-02 17:57:22 -0700412The reason for this is many-fold. But in essence YAPF is simply a tool to help
Bill Wendling8fb9c482015-03-29 17:32:07 -0700413with development. It will format things to coincide with the style guide, but
414that may not equate with readability.
415
416What can be done to alleviate this situation is to indicate regions YAPF should
417ignore when reformatting something:
418
419.. code-block:: python
420
421 # yapf: disable
422 FOO = {
423 # ... some very large, complex data literal.
424 }
425
426 BAR = [
427 # ... another large data literal.
428 ]
429 # yapf: enable
430
431You can also disable formatting for a single literal like this:
432
433.. code-block:: python
434
435 BAZ = {
Scott Sandersoneda4e262015-07-05 21:10:06 -0400436 (1, 2, 3, 4),
437 (5, 6, 7, 8),
438 (9, 10, 11, 12),
Bill Wendling8fb9c482015-03-29 17:32:07 -0700439 } # yapf: disable
440
Ɓukasz Langa94089872015-09-22 16:02:26 -0700441To preserve the nice dedented closing brackets, use the
442``dedent_closing_brackets`` in your style. Note that in this case all
443brackets, including function definitions and calls, are going to use
444that style. This provides consistency across the formatted codebase.
445
Bill Wendling7d623452015-03-18 13:36:07 -0700446Why Not Improve Existing Tools?
Bill Wendling8fb9c482015-03-29 17:32:07 -0700447-------------------------------
Bill Wendling7d623452015-03-18 13:36:07 -0700448
449We wanted to use clang-format's reformatting algorithm. It's very powerful and
450designed to come up with the best formatting possible. Existing tools were
451created with different goals in mind, and would require extensive modifications
452to convert to using clang-format's algorithm.
453
Bill Wendling7d623452015-03-18 13:36:07 -0700454Can I Use YAPF In My Program?
Bill Wendling8fb9c482015-03-29 17:32:07 -0700455-----------------------------
Bill Wendling7d623452015-03-18 13:36:07 -0700456
457Please do! YAPF was designed to be used as a library as well as a command line
458tool. This means that a tool or IDE plugin is free to use YAPF.
459
Bill Wendlingf09121c2015-10-20 22:59:33 -0700460
Bill Wendling7d623452015-03-18 13:36:07 -0700461Gory Details
462============
463
464Algorithm Design
465----------------
466
Eli Benderskyd08130d2015-03-19 05:20:46 -0700467The main data structure in YAPF is the ``UnwrappedLine`` object. It holds a list
468of ``FormatToken``\s, that we would want to place on a single line if there were
469no column limit. An exception being a comment in the middle of an expression
Bill Wendling7d623452015-03-18 13:36:07 -0700470statement will force the line to be formatted on more than one line. The
Eli Benderskyd08130d2015-03-19 05:20:46 -0700471formatter works on one ``UnwrappedLine`` object at a time.
Bill Wendling7d623452015-03-18 13:36:07 -0700472
Eli Benderskyd08130d2015-03-19 05:20:46 -0700473An ``UnwrappedLine`` typically won't affect the formatting of lines before or
474after it. There is a part of the algorithm that may join two or more
475``UnwrappedLine``\s into one line. For instance, an if-then statement with a
Bill Wendlingf5e50b62015-03-28 23:38:12 -0700476short body can be placed on a single line:
477
478.. code-block:: python
Bill Wendling7d623452015-03-18 13:36:07 -0700479
480 if a == 42: continue
481
482YAPF's formatting algorithm creates a weighted tree that acts as the solution
483space for the algorithm. Each node in the tree represents the result of a
484formatting decision --- i.e., whether to split or not to split before a token.
485Each formatting decision has a cost associated with it. Therefore, the cost is
486realized on the edge between two nodes. (In reality, the weighted tree doesn't
487have separate edge objects, so the cost resides on the nodes themselves.)
488
489For example, take the following Python code snippet. For the sake of this
490example, assume that line (1) violates the column limit restriction and needs to
491be reformatted.
492
Bill Wendlingfa22c892015-03-18 13:42:25 -0700493.. code-block:: python
Bill Wendling7d623452015-03-18 13:36:07 -0700494
Bill Wendlingfa22c892015-03-18 13:42:25 -0700495 def xxxxxxxxxxx(aaaaaaaaaaaa, bbbbbbbbb, cccccccc, dddddddd, eeeeee): # 1
496 pass # 2
Bill Wendling7d623452015-03-18 13:36:07 -0700497
498For line (1), the algorithm will build a tree where each node (a
Eli Benderskyd08130d2015-03-19 05:20:46 -0700499``FormattingDecisionState`` object) is the state of the line at that token given
500the decision to split before the token or not. Note: the ``FormatDecisionState``
501objects are copied by value so each node in the graph is unique and a change in
502one doesn't affect other nodes.
Bill Wendling7d623452015-03-18 13:36:07 -0700503
Bill Wendlingfa22c892015-03-18 13:42:25 -0700504Heuristics are used to determine the costs of splitting or not splitting.
505Because a node holds the state of the tree up to a token's insertion, it can
506easily determine if a splitting decision will violate one of the style
Bill Wendling7d623452015-03-18 13:36:07 -0700507requirements. For instance, the heuristic is able to apply an extra penalty to
508the edge when not splitting between the previous token and the one being added.
509
510There are some instances where we will never want to split the line, because
511doing so will always be detrimental (i.e., it will require a backslash-newline,
512which is very rarely desirable). For line (1), we will never want to split the
Eli Benderskyd08130d2015-03-19 05:20:46 -0700513first three tokens: ``def``, ``xxxxxxxxxxx``, and ``(``. Nor will we want to
514split between the ``)`` and the ``:`` at the end. These regions are said to be
515"unbreakable." This is reflected in the tree by there not being a "split"
Bill Wendling7d623452015-03-18 13:36:07 -0700516decision (left hand branch) within the unbreakable region.
517
518Now that we have the tree, we determine what the "best" formatting is by finding
519the path through the tree with the lowest cost.
520
521And that's it!