YAPF - Yet Another Python Formatter

0.43.0 · active · verified Thu Apr 09

YAPF (Yet Another Python Formatter) is an opinionated code formatter for Python, developed by Google. It automatically re-formats Python code to conform to the PEP 8 style guide and other configurable style conventions, aiming to improve code readability and consistency. The current version is 0.43.0, and it generally follows a stable release cadence with updates for Python version support and bug fixes.

Warnings

Install

Imports

Quickstart

Demonstrates how to use `FormatCode` to programmatically format a string of Python code, including an example of applying a specific style configuration.

from yapf.yapf_api import FormatCode

unformatted_code = """
def example_function(  arg1,  arg2 = None ):
    if arg1 :
        print(  "Hello", arg1 )
    else:
        return arg2
"""

formatted_code, changed = FormatCode(unformatted_code)

print("--- Original ---")
print(unformatted_code)
print("\n--- Formatted ---")
print(formatted_code)
print(f"\nChanged: {changed}")

# You can also pass style parameters
formatted_code_google, _ = FormatCode(unformatted_code, style_config='google')
print("\n--- Formatted (Google Style) ---")
print(formatted_code_google)

view raw JSON →