- Notifications
You must be signed in to change notification settings - Fork897
A formatter for Python files
License
google/yapf
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
YAPF is a Python formatter based onclang-format
(developed by Daniel Jasper). In essence, the algorithm takes the code andcalculates the best formatting that conforms to the configured style. It takesaway a lot of the drudgery of maintaining your code.
The ultimate goal is that the code YAPF produces is as good as the code that aprogrammer would write if they were following the style guide.
NoteYAPF is not an official Google product (experimental or otherwise), it isjust code that happens to be owned by Google.
To install YAPF from PyPI:
$ pip install yapf
YAPF is still considered in "beta" stage, and the released version may changeoften; therefore, the best way to keep up-to-date with the latest developmentis to clone this repository or install directly from github:
$ pip install git+https://github.com/google/yapf.git
Note that if you intend to use YAPF as a command-line tool rather than as alibrary, installation is not necessary. YAPF supports being run as a directoryby the Python interpreter. If you cloned/unzipped YAPF intoDIR
, it'spossible to run:
$ PYTHONPATH=DIR python DIR/yapf [options] ...
YAPF is supported by multiple editors via community extensions or plugins. SeeEditor Support for more info.
YAPF supports Python 3.7+.
usage: yapf [-h] [-v] [-d | -i | -q] [-r | -l START-END] [-e PATTERN] [--style STYLE] [--style-help] [--no-local-style] [-p] [-m] [-vv] [files ...]Formatter for Python code.positional arguments: files reads from stdin when no files are specified.optional arguments: -h, --help show this help message and exit -v, --version show program's version number and exit -d, --diff print the diff for the fixed source -i, --in-place make changes to files in place -q, --quiet output nothing and set return value -r, --recursive run recursively over directories -l START-END, --lines START-END range of lines to reformat, one-based -e PATTERN, --exclude PATTERN patterns for files to exclude from formatting --style STYLE specify formatting style: either a style name (for example "pep8" or "google"), or the name of a file with style settings. The default is pep8 unless a .style.yapf or setup.cfg or pyproject.toml file located in the same directory as the source or one of its parent directories (for stdin, the current directory is used). --style-help show style settings and exit; this output can be saved to .style.yapf to make your settings permanent --no-local-style don't search for local style definition -p, --parallel run YAPF in parallel when formatting multiple files. -m, --print-modified print out file names of modified files -vv, --verbose print out file names while processing
Normally YAPF returns zero on successful program termination and non-zerootherwise.
If--diff
is supplied, YAPF returns zero when no changes were necessary,non-zero otherwise (including program error). You can use this in a CI workflowto test that code has been YAPF-formatted.
In addition to exclude patterns provided on commandline, YAPF looks foradditional patterns specified in a file named.yapfignore
orpyproject.toml
located in the working directory from which YAPF is invoked.
.yapfignore
's syntax is similar to UNIX's filename pattern matching:
* matches everything? matches any single character[seq] matches any character in seq[!seq] matches any character not in seq
Note that no entry should begin with./
.
If you usepyproject.toml
, exclude patterns are specified byignore_patterns
keyin[tool.yapfignore]
section. For example:
[tool.yapfignore]ignore_patterns = ["temp/**/*.py","temp2/*.py"]
The formatting style used by YAPF is configurable and there are many "knobs"that can be used to tune how YAPF does formatting. See thestyle.py
modulefor the full list.
To control the style, run YAPF with the--style
argument. It accepts one ofthe predefined styles (e.g.,pep8
orgoogle
), a path to a configurationfile that specifies the desired style, or a dictionary of key/value pairs.
The config file is a simple listing of (case-insensitive)key = value
pairswith a[style]
heading. For example:
[style]based_on_style = pep8spaces_before_comment = 4split_before_logical_operator = true
Thebased_on_style
setting determines which of the predefined styles thiscustom style is based on (think of it like subclassing). Fourstyles are predefined:
pep8
(default)google
(based off of theGoogle Python Style Guide)yapf
(for use with Google open source projects)facebook
See_STYLE_NAME_TO_FACTORY
instyle.py
for details.
It's also possible to do the same on the command line with a dictionary. Forexample:
--style='{based_on_style: pep8, indent_width: 2}'
This will take thepep8
base style and modify it to have two spaceindentations.
YAPF will search for the formatting style in the following manner:
- Specified on the command line
- In the
[style]
section of a.style.yapf
file in either the currentdirectory or one of its parent directories. - In the
[yapf]
section of asetup.cfg
file in either the currentdirectory or one of its parent directories. - In the
[tool.yapf]
section of apyproject.toml
file in either the currentdirectory or one of its parent directories. - In the
[style]
section of a~/.config/yapf/style
file in your homedirectory.
If none of those files are found, the default style PEP8 is used.
An example of the type of formatting that YAPF can do, it will take this uglycode:
x= {'a':37,'b':42,'c':927}y='hello ''world'z='hello '+'world'a='hello {}'.format('world')classfoo (object ):deff (self ):return37*-+2defg(self,x,y=42):returnydeff (a ) :return37+-+a[42-x :y**3]
and reformat it into:
x= {'a':37,'b':42,'c':927}y='hello ''world'z='hello '+'world'a='hello {}'.format('world')classfoo(object):deff(self):return37*-+2defg(self,x,y=42):returnydeff(a):return37+-+a[42-x:y**3]
The two main APIs for calling YAPF areFormatCode
andFormatFile
, theseshare several arguments which are described below:
>>>fromyapf.yapflib.yapf_apiimportFormatCode# reformat a string of code>>>formatted_code,changed=FormatCode("f ( a = 1, b = 2 )")>>>formatted_code'f(a=1, b=2)\n'>>>changedTrue
Astyle_config
argument: Either a style name or a path to a file thatcontains formatting style settings. If None is specified, use the default styleas set instyle.DEFAULT_STYLE_FACTORY
.
>>>FormatCode("def g():\n return True",style_config='pep8')[0]'def g():\n return True\n'
Alines
argument: A list of tuples of lines (ints), [start, end], that wewant to format. The lines are 1-based indexed. It can be used by third-partycode (e.g., IDEs) when reformatting a snippet of code rather than a whole file.
>>>FormatCode("def g( ):\n a=1\n b = 2\n return a==b",lines=[(1,1), (2,3)])[0]'def g():\n a = 1\n b = 2\n return a==b\n'
Aprint_diff
(bool): Instead of returning the reformatted source, return adiff that turns the formatted source into reformatted source.
>>> print(FormatCode("a==b", filename="foo.py", print_diff=True)[0])--- foo.py (original)+++ foo.py (reformatted)@@ -1 +1 @@-a==b+a == b
Note: thefilename
argument forFormatCode
is what is inserted into thediff, the default is<unknown>
.
FormatFile
returns reformatted code from the passed file along with its encoding:
>>>fromyapf.yapflib.yapf_apiimportFormatFile# reformat a file>>>print(open("foo.py").read())# contents of filea==b>>>reformatted_code,encoding,changed=FormatFile("foo.py")>>>formatted_code'a == b\n'>>>encoding'utf-8'>>>changedTrue
Thein_place
argument saves the reformatted code back to the file:
>>>FormatFile("foo.py",in_place=True)[:2](None,'utf-8')>>>print(open("foo.py").read())# contents of file (now fixed)a==b
Options:
usage: yapf-diff [-h] [-i] [-p NUM] [--regex PATTERN] [--iregex PATTERN][-v] [--style STYLE] [--binary BINARY]This script reads input from a unified diff and reformats all the changedlines. This is useful to reformat all the lines touched by a specific patch.Example usage for git/svn users: git diff -U0 --no-color --relative HEAD^ | yapf-diff -i svn diff --diff-cmd=diff -x-U0 | yapf-diff -p0 -iIt should be noted that the filename contained in the diff is usedunmodified to determine the source file to update. Users calling this scriptdirectly should be careful to ensure that the path in the diff is correctrelative to the current working directory.optional arguments: -h, --help show this help message and exit -i, --in-place apply edits to files instead of displaying a diff -p NUM, --prefix NUM strip the smallest prefix containing P slashes --regex PATTERN custom pattern selecting file paths to reformat (case sensitive, overrides -iregex) --iregex PATTERN custom pattern selecting file paths to reformat (case insensitive, overridden by -regex) -v, --verbose be more verbose, ineffective without -i --style STYLE specify formatting style: either a style name (for example "pep8" or "google"), or the name of a file with style settings. The default is pep8 unless a .style.yapf or setup.cfg or pyproject.toml file located in the same directory as the source or one of its parent directories (for stdin, the current directory is used). --binary BINARY location of binary to use for YAPF
- Python 3.12 –PEP 695 – Type Parameter Syntax –YAPF #1170
- Python 3.12 –PEP 701 – Syntactic formalization of f-strings –YAPF #1136
Align closing bracket with visual indentation.
Allow lambdas to be formatted on more than one line.
Allow dictionary keys to exist on multiple lines. For example:
x= { ('this is the first element of a tuple','this is the second element of a tuple'):value, }
Allow splitting before a default / named assignment in an argument list.
Allow splits before the dictionary value.
Let spacing indicate operator precedence. For example:
a=1*2+3/4b=1/2-3*4c= (1+2)* (3-4)d= (1-2)/ (3+4)e=1*2-3f=1+2+3+4
will be formatted as follows to indicate precedence:
a=1*2+3/4b=1/2-3*4c= (1+2)* (3-4)d= (1-2)/ (3+4)e=1*2-3f=1+2+3+4
Sets the number of desired blank lines surrounding top-level function andclass definitions. For example:
classFoo:pass# <------ having two blank lines here# <------ is the default settingclassBar:pass
Insert a blank line before a class-level docstring.
Insert a blank line before a module docstring.
Insert a blank line before a
def
orclass
immediately nested withinanotherdef
orclass
. For example:
classFoo:# <------ this blank linedefmethod():pass
Sets the number of desired blank lines between top-level imports andvariable definitions. Useful for compatibility with tools like isort.
Do not split consecutive brackets. Only relevant when
DEDENT_CLOSING_BRACKETS
orINDENT_CLOSING_BRACKETS
is set. For example:
call_func_that_takes_a_dict( {'key1':'value1','key2':'value2', } )
would reformat to:
call_func_that_takes_a_dict({'key1':'value1','key2':'value2', })
The column limit (or max line-length)
The style for continuation alignment. Possible values are:
SPACE
: Use spaces for continuation alignment. This is defaultbehavior.FIXED
: Use fixed number (CONTINUATION_INDENT_WIDTH
) of columns(i.e.CONTINUATION_INDENT_WIDTH
/INDENT_WIDTH
tabs orCONTINUATION_INDENT_WIDTH
spaces) for continuation alignment.VALIGN-RIGHT
: Vertically align continuation lines to multiple ofINDENT_WIDTH
columns. Slightly right (one tab or a few spaces) if cannotvertically align continuation lines with indent characters.
Indent width used for line continuations.
Put closing brackets on a separate line, dedented, if the bracketedexpression can't fit in a single line. Applies to all kinds of brackets,including function definitions and calls. For example:
config= {'key1':'value1','key2':'value2', }# <--- this bracket is dedented and on a separate linetime_series=self.remote_client.query_entity_counters(entity='dev3246.region1',key='dns.query_latency_tcp',transform=Transformation.AVERAGE(window=timedelta(seconds=60)),start_ts=now()-timedelta(days=3),end_ts=now(), )# <--- this bracket is dedented and on a separate line
Disable the heuristic which places each list element on a separate line ifthe list is comma-terminated.
Note: The behavior of this flag changed in v0.40.3. Before, if this flagwas true, we would split lists that contained a trailing comma or acomment. Now, we have a separate flag,
DISABLE_SPLIT_LIST_WITH_COMMENT
,that controls splitting when a list contains a comment. To get the oldbehavior, set both flags to true. More information inCHANGELOG.md.
Don't put every element on a new line within a list that containsinterstitial comments.
Without this flag (default):
[ a, b, # c]
With this flag:
[ a, b, # c]
This mirrors the behavior of clang-format and is useful for forming"logical groups" of elements in a list. It also works in functiondeclarations.
Place each dictionary entry onto its own line.
Respect
EACH_DICT_ENTRY_ON_SEPARATE_LINE
even if the line is shorter thanCOLUMN_LIMIT
.
The regex for an internationalization comment. The presence of this commentstops reformatting of that line, because the comments are required to benext to the string they translate.
The internationalization function call names. The presence of this functionstops reformatting on that line, because the string it has cannot be movedaway from the i18n comment.
Set to
True
to prefer indented blank lines rather than empty
Put closing brackets on a separate line, indented, if the bracketedexpression can't fit in a single line. Applies to all kinds of brackets,including function definitions and calls. For example:
config= {'key1':'value1','key2':'value2', }# <--- this bracket is indented and on a separate linetime_series=self.remote_client.query_entity_counters(entity='dev3246.region1',key='dns.query_latency_tcp',transform=Transformation.AVERAGE(window=timedelta(seconds=60)),start_ts=now()-timedelta(days=3),end_ts=now(), )# <--- this bracket is indented and on a separate line
Indent the dictionary value if it cannot fit on the same line as thedictionary key. For example:
config= {'key1':'value1','key2':value1+value2, }
The number of columns to use for indentation.
Join short lines into one line. E.g., single line
if
statements.
Do not include spaces around selected binary operators. For example:
1+2*3-4/5
will be formatted as follows when configured with
*
,/
:
1+2*3-4/5
Insert a space between the ending comma and closing bracket of a list, etc.
Use spaces inside brackets, braces, and parentheses. For example:
method_call(1 )my_dict[3 ][1 ][get_index(*args,**kwargs ) ]my_set= {1,2,3 }
Set to
True
to prefer spaces around the assignment operator for defaultor keyword arguments.
Adds a space after the opening '{' and before the ending '}' dict delimiters.
{1:2}
will be formatted as:
{1:2 }
Adds a space after the opening '[' and before the ending ']' list delimiters.
[1,2]
will be formatted as:
[1,2 ]
Set to
True
to prefer using spaces around**
.
Use spaces around the subscript / slice operator. For example:
my_list[1 :10 :2]
Adds a space after the opening '(' and before the ending ')' tuple delimiters.
(1,2,3)
will be formatted as:
(1,2,3 )
The number of spaces required before a trailing comment.This can be a single value (representing the number of spacesbefore each trailing comment) or list of values (representingalignment column values; trailing comments within a block willbe aligned to the first column value that is greater than the maximumline length within the block).
Note: Lists of values may need to be quoted in some contexts(eg. shells or editor config files).
For example, with
spaces_before_comment=5
:
1+1# Adding values
will be formatted as:
1+1# Adding values <-- 5 spaces between the end of the statement and comment
with
spaces_before_comment="15, 20"
:
1+1# Adding valuestwo+two# More addinglonger_statement# This is a longer statementshort# This is a shorter statementa_very_long_statement_that_extends_beyond_the_final_column# Commentshort# This is a shorter statement
will be formatted as:
1+1# Adding values <-- end of line comments in block aligned to col 15two+two# More addinglonger_statement# This is a longer statement <-- end of line comments in block aligned to col 20short# This is a shorter statementa_very_long_statement_that_extends_beyond_the_final_column# Comment <-- the end of line comments are aligned based on the line lengthshort# This is a shorter statement
If a comma separated list (
dict
,list
,tuple
, or functiondef
) ison a line that is too long, split such that each element is on a separateline.
Variation on
SPLIT_ALL_COMMA_SEPARATED_VALUES
in which, if asubexpression with a comma fits in its starting line, then thesubexpression is not split. This avoids splits like the one forb
in this code:
abcdef(aReallyLongThing:int,b: [Int,Int])
with the new knob this is split as:
abcdef(aReallyLongThing:int,b: [Int,Int])
Split before arguments if the argument list is terminated by a comma.
Set to
True
to prefer splitting before+
,-
,*
,/
,//
, or@
rather than after.
Set to
True
to prefer splitting before&
,|
or^
rather than after.
Split before the closing bracket if a
list
ordict
literal doesn't fiton a single line.
Split before a dictionary or set generator (
comp_for
). For example, notethe split before thefor
:
foo= {variable:'Hello world, have a nice day!'forvariableinbarifvariable!=42 }
Split before the
.
if we need to split a longer expression:
foo= ('This is a really long string: {}, {}, {}, {}'.format(a,b,c,d))
would reformat to something like:
foo= ('This is a really long string: {}, {}, {}, {}' .format(a,b,c,d))
Split after the opening paren which surrounds an expression if it doesn'tfit on a single line.
If an argument / parameter list is going to be split, then split before thefirst argument.
Set to
True
to prefer splitting beforeand
oror
rather than after.
Split named assignments onto individual lines.
For list comprehensions and generator expressions with multiple clauses(e.g multiple
for
calls,if
filter expressions) and which need to bereflowed, split each clause onto its own line. For example:
result= [a_var+b_varfora_varinxrange(1000)forb_varinxrange(1000)ifa_var%b_var]
would reformat to something like:
result= [a_var+b_varfora_varinxrange(1000)forb_varinxrange(1000)ifa_var%b_var]
The penalty for splitting right after the opening bracket.
The penalty for splitting the line after a unary operator.
The penalty of splitting the line around the
+
,-
,*
,/
,//
,%
,and@
operators.
The penalty for splitting right before an
if
expression.
The penalty of splitting the line around the
&
,|
, and^
operators.
The penalty for splitting a list comprehension or generator expression.
The penalty for characters over the column limit.
The penalty incurred by adding a line split to the logical line. The moreline splits added the higher the penalty.
The penalty of splitting a list of
import as
names. For example:
froma_very_long_or_indented_module_name_yada_yadimport (long_argument_1,long_argument_2,long_argument_3)
would reformat to something like:
froma_very_long_or_indented_module_name_yada_yadimport (long_argument_1,long_argument_2,long_argument_3)
The penalty of splitting the line around the
and
andor
operators.
Use the Tab character for indentation.
YAPF tries very hard to get the formatting correct. But for some code, it won'tbe as good as hand-formatting. In particular, large data literals may becomehorribly disfigured under YAPF.
The reasons for this are manyfold. In short, YAPF is simply a tool to helpwith development. It will format things to coincide with the style guide, butthat may not equate with readability.
What can be done to alleviate this situation is to indicate regions YAPF shouldignore when reformatting something:
# yapf: disableFOO= {# ... some very large, complex data literal.}BAR= [# ... another large data literal.]# yapf: enable
You can also disable formatting for a single literal like this:
BAZ= { (1,2,3,4), (5,6,7,8), (9,10,11,12),}# yapf: disable
To preserve the nice dedented closing brackets, use thededent_closing_brackets
in your style. Note that in this case allbrackets, including function definitions and calls, are going to usethat style. This provides consistency across the formatted codebase.
We wanted to use clang-format's reformatting algorithm. It's very powerful anddesigned to come up with the best formatting possible. Existing tools werecreated with different goals in mind, and would require extensive modificationsto convert to using clang-format's algorithm.
Please do! YAPF was designed to be used as a library as well as a command linetool. This means that a tool or IDE plugin is free to use YAPF.
YAPF tries very hard to be fully PEP 8 compliant. However, it is paramountto not risk altering the semantics of your code. Thus, YAPF tries to be assafe as possible and does not change the token stream(e.g., by adding parentheses).All these cases however, can be easily fixed manually. For instance,
frommy_packageimportmy_function_1,my_function_2,my_function_3,my_function_4,my_function_5FOO=my_variable_1+my_variable_2+my_variable_3+my_variable_4+my_variable_5+my_variable_6+my_variable_7+my_variable_8
won't be split, but you can easily get it right by just adding parentheses:
frommy_packageimport (my_function_1,my_function_2,my_function_3,my_function_4,my_function_5)FOO= (my_variable_1+my_variable_2+my_variable_3+my_variable_4+my_variable_5+my_variable_6+my_variable_7+my_variable_8)
The main data structure in YAPF is theLogicalLine
object. It holds a listofFormatToken
\s, that we would want to place on a single line if therewere no column limit. An exception being a comment in the middle of anexpression statement will force the line to be formatted on more than one line.The formatter works on oneLogicalLine
object at a time.
AnLogicalLine
typically won't affect the formatting of lines before orafter it. There is a part of the algorithm that may join two or moreLogicalLine
\s into one line. For instance, an if-then statement with ashort body can be placed on a single line:
ifa==42:continue
YAPF's formatting algorithm creates a weighted tree that acts as the solutionspace for the algorithm. Each node in the tree represents the result of aformatting decision --- i.e., whether to split or not to split before a token.Each formatting decision has a cost associated with it. Therefore, the cost isrealized on the edge between two nodes. (In reality, the weighted tree doesn'thave separate edge objects, so the cost resides on the nodes themselves.)
For example, take the following Python code snippet. For the sake of thisexample, assume that line (1) violates the column limit restriction and needs tobe reformatted.
defxxxxxxxxxxx(aaaaaaaaaaaa,bbbbbbbbb,cccccccc,dddddddd,eeeeee):# 1pass# 2
For line (1), the algorithm will build a tree where each node (aFormattingDecisionState
object) is the state of the line at that token giventhe decision to split before the token or not. Note: theFormatDecisionState
objects are copied by value so each node in the graph is unique and a change inone doesn't affect other nodes.
Heuristics are used to determine the costs of splitting or not splitting.Because a node holds the state of the tree up to a token's insertion, it caneasily determine if a splitting decision will violate one of the stylerequirements. For instance, the heuristic is able to apply an extra penalty tothe edge when not splitting between the previous token and the one being added.
There are some instances where we will never want to split the line, becausedoing so will always be detrimental (i.e., it will require a backslash-newline,which is very rarely desirable). For line (1), we will never want to split thefirst three tokens:def
,xxxxxxxxxxx
, and(
. Nor will we want tosplit between the)
and the:
at the end. These regions are said to be"unbreakable." This is reflected in the tree by there not being a "split"decision (left hand branch) within the unbreakable region.
Now that we have the tree, we determine what the "best" formatting is by findingthe path through the tree with the lowest cost.
And that's it!
About
A formatter for Python files