LinuxCommandLibrary

yapf

Format Python code for consistent style

TLDR

Display a diff of the changes that would be made, without making them (dry-run)

$ yapf [[-d|--diff]] [path/to/file]
copy

Format the file in-place and display a diff of the changes
$ yapf [[-d|--diff]] [[-i|--in-place]] [path/to/file]
copy

Recursively format all Python files in a directory, concurrently
$ yapf [[-r|--recursive]] [[-i|--in-place]] --style [pep8] [[-p|--parallel]] [path/to/directory]
copy

SYNOPSIS

yapf [options] [files] ...

PARAMETERS

--in-place
    Format the files in-place, overwriting the original files. Create backups if --backup is specified.

--recursive
    Recursively find Python files in the directories specified.

--exclude PATTERN
    Files matching this pattern will be excluded from formatting.

--diff
    Print a diff of the changes that would be made, without actually modifying the files.

--no-in-place
    Do not perform in-place formatting (default).

--style STYLE
    Specify formatting style. Possible values are: pep8, google, chromium, facebook, or a path to a .yapf or setup.cfg file.

--version
    Show program's version number and exit

--parallel
    Run yapf in parallel with cpu_count() processes.

DESCRIPTION

yapf is a Python code formatter developed by Google. It aims to format code according to the PEP 8 style guide (and some style variants chosen by the user) while minimizing changes to the original code's intent. Unlike some formatters that aggressively rewrite code, yapf strives to produce output that is both aesthetically pleasing and semantically equivalent to the input. It's configurable regarding formatting preferences like line length, indentation, and line splitting rules. Yapf leverages the 'libformat' library internally. It can be used both as a command-line tool and as a library within other Python applications for automatic code formatting. It also allows format checking.

CAVEATS

Yapf may not always produce perfectly formatted code in all edge cases. It's always a good idea to review the output after formatting, especially after significant code changes.

CONFIGURATION

Yapf can be configured using `.yapf` files, `setup.cfg` files, or command-line options. Configuration files allow specifying style settings such as line length, indentation width, and other formatting preferences. Command-line options override settings defined in configuration files.

The configuration files are searched for in the parent directories of the formatted file. This allows for project-specific or directory-specific formatting rules.

EXIT CODES

Yapf returns different exit codes to indicate the outcome of the formatting process:
0: Success with no changes.
1: Success with formatting changes.
2: An error occurred during formatting.

HISTORY

Yapf was created and open-sourced by Google. Its development was driven by the need for a consistent code formatting style across large Python codebases within the company. Its initial focus was on aligning with Google's internal Python style guidelines. The project has since evolved to include support for PEP 8 and other popular style conventions.

SEE ALSO

black(1), autopep8(1)

Copied to clipboard