{"id":1795,"library":"yapf","title":"YAPF - Yet Another Python Formatter","description":"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.","status":"active","version":"0.43.0","language":"en","source_language":"en","source_url":"https://github.com/google/yapf","tags":["formatter","code formatting","style guide","PEP 8","python"],"install":[{"cmd":"pip install yapf","lang":"bash","label":"Install latest stable version"}],"dependencies":[],"imports":[{"note":"FormatCode is the primary function for programmatic formatting. Ensure you import it from the yapf_api submodule.","wrong":"from yapf import FormatCode","symbol":"FormatCode","correct":"from yapf.yapf_api import FormatCode"}],"quickstart":{"code":"from yapf.yapf_api import FormatCode\n\nunformatted_code = \"\"\"\ndef example_function(  arg1,  arg2 = None ):\n    if arg1 :\n        print(  \"Hello\", arg1 )\n    else:\n        return arg2\n\"\"\"\n\nformatted_code, changed = FormatCode(unformatted_code)\n\nprint(\"--- Original ---\")\nprint(unformatted_code)\nprint(\"\\n--- Formatted ---\")\nprint(formatted_code)\nprint(f\"\\nChanged: {changed}\")\n\n# You can also pass style parameters\nformatted_code_google, _ = FormatCode(unformatted_code, style_config='google')\nprint(\"\\n--- Formatted (Google Style) ---\")\nprint(formatted_code_google)","lang":"python","description":"Demonstrates how to use `FormatCode` to programmatically format a string of Python code, including an example of applying a specific style configuration."},"warnings":[{"fix":"Ensure your environment uses Python 3.7 or a later version. Upgrade your Python interpreter if necessary.","message":"Python 3.6 support was dropped in YAPF version 0.37.0. Subsequent versions (including 0.43.0) require Python 3.7 or newer.","severity":"breaking","affected_versions":">=0.37.0"},{"fix":"To modify files in-place, always use the `-i` or `--in-place` flag (e.g., `yapf -i file.py`).","message":"For CLI usage, files are not modified in-place by default. If you run `yapf file.py`, it will print the formatted code to stdout but leave the original file untouched.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Pass `style_config='google'` (or other predefined styles), or a dictionary of style options directly to `FormatCode`. For file-based configuration, load it manually and pass the dictionary, or invoke YAPF via its CLI which handles file discovery.","message":"Configuration for programmatic use (e.g., `FormatCode`) does not automatically discover `.style.yapf` or `pyproject.toml` files from the current working directory. You must explicitly pass `style_config` or other parameters.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid relying on YAPF's internal AST structures. If you need to manipulate ASTs, use `libcst` directly or ensure your integration is compatible with the new underlying library.","message":"Internal AST representation changed from `pytree` to `libcst` in version 0.40.0. While this is primarily an internal refactoring, users who relied on direct interaction with YAPF's internal AST objects might experience compatibility issues.","severity":"gotcha","affected_versions":">=0.40.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}