[
  {
    "path": ".coveragerc",
    "content": "[report]\nomit =\n  */__main__.py\n"
  },
  {
    "path": ".editorconfig",
    "content": "# EditorConfig is awesome: http://EditorConfig.org\n\n# top-most EditorConfig file\nroot = true\n\n# Unix-style newlines with a newline ending every file\n[*]\nend_of_line = lf\ninsert_final_newline = true\n\n# 2 space indentation\n[*.py]\nindent_style = space\nindent_size = 2\n"
  },
  {
    "path": ".flake8",
    "content": "[flake8]\nignore =\n    # 'toml' imported but unused\n    F401,\n    # closing bracket does not match visual indentation\n    E124,\n    # continuation line over-indented for hanging indent\n    E126,\n    # visually indented line with same indent as next logical line,\n    E129,\n    # line break before binary operator\n    W503,\n    # line break after binary operator\n    W504\n\nindent-size = 2\nmax-line-length = 80\n"
  },
  {
    "path": ".gitattributes",
    "content": ".python-version eol=lf\n"
  },
  {
    "path": ".github/dependabot.yml",
    "content": "version: 2\nupdates:\n\n  - package-ecosystem: \"github-actions\"\n    commit-message:\n      include: \"scope\"\n      prefix: \"Actions\"\n    directory: \"/\"\n    labels:\n      - \"enhancement\"\n    schedule:\n      interval: \"weekly\"\n"
  },
  {
    "path": ".github/workflows/ci.yml",
    "content": "# This workflow will install Python dependencies, run tests and lint with a variety of Python versions\n# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions\n\nname: Test with pytest\n\non:\n  pull_request:\n  push:\n\njobs:\n  build:\n    runs-on: ${{ matrix.os }}\n    strategy:\n      fail-fast: false\n      matrix:\n        python-version: [\"3.8\", \"3.11\", \"3.12\"]  # no particular need for 3.9 or 3.10\n        os: [macos-latest, ubuntu-latest, windows-latest]\n    steps:\n    - uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871  # v4.2.1\n    - name: Set up Python ${{ matrix.python-version }}\n      uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d  # v4.6.0\n      with:\n        python-version: ${{ matrix.python-version }}\n    - name: Upgrade pip\n      run: >-\n        python -m pip install\n        --upgrade\n        --disable-pip-version-check\n        pip\n    - name: Perform package installs\n      run: >-\n        pip install\n        .\n        pytest\n        pytest-cov\n    - name: Test with pytest\n      run: pytest\n"
  },
  {
    "path": ".github/workflows/pre-commit-autoupdate.yml",
    "content": "# Copyright (c) 2023 Sebastian Pipping <sebastian@pipping.org>\n# Licensed under the Apache License Version 2.0\n\nname: Keep pre-commit hooks up to date\n\non:\n  schedule:\n    - cron: '0 16 * * 5'  # Every Friday 4pm\n  workflow_dispatch:\n\n# NOTE: This will drop all permissions from GITHUB_TOKEN except metadata read,\n#       and then (re)add the ones listed below:\npermissions:\n  contents: write\n  pull-requests: write\n\njobs:\n  pre_commit_autoupdate:\n    name: Detect outdated pre-commit hooks\n    runs-on: ubuntu-22.04\n    steps:\n      - uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871  # v4.2.1\n\n      - name: Set up Python 3.11\n        uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d  # v5.1.0\n        with:\n          python-version: 3.11\n\n      - name: Install pre-commit\n        run: |-\n          pip install \\\n            --disable-pip-version-check \\\n            --no-warn-script-location \\\n            --user \\\n            pre-commit\n          echo \"PATH=${HOME}/.local/bin:${PATH}\" >> \"${GITHUB_ENV}\"\n\n      - name: Check for outdated hooks\n        run: |-\n          pre-commit autoupdate\n          git diff -- .pre-commit-config.yaml\n\n      - name: Create pull request from changes (if any)\n        id: create-pull-request\n        uses: peter-evans/create-pull-request@5e914681df9dc83aa4e4905692ca88beb2f9e91f  # v7.0.5\n        with:\n          author: 'pre-commit <pre-commit@tools.invalid>'\n          base: main\n          body: |-\n            For your consideration.\n\n            :warning: Please **CLOSE AND RE-OPEN** this pull request so that [further workflow runs get triggered](https://github.com/peter-evans/create-pull-request/blob/main/docs/concepts-guidelines.md#triggering-further-workflow-runs) for this pull request.\n          branch: precommit-autoupdate\n          commit-message: \"pre-commit: Autoupdate\"\n          delete-branch: true\n          draft: true\n          labels: enhancement\n          title: \"pre-commit: Autoupdate\"\n\n      - name: Log pull request URL\n        if: \"${{ steps.create-pull-request.outputs.pull-request-url }}\"\n        run: |\n          echo \"Pull request URL is: ${{ steps.create-pull-request.outputs.pull-request-url }}\"\n"
  },
  {
    "path": ".github/workflows/pre-commit.yml",
    "content": "# Copyright (c) 2023 Sebastian Pipping <sebastian@pipping.org>\n# Licensed under the Apache License Version 2.0\n\nname: Run pre-commit\n\n# Drop permissions to minimum for security\npermissions:\n  contents: read\n\non:\n  pull_request:\n  push:\n  schedule:\n    - cron: '0 2 * * 5'  # Every Friday at 2am\n  workflow_dispatch:\n\njobs:\n  pre_commit_run:\n    name: Run pre-commit\n    runs-on: ubuntu-22.04\n    steps:\n      - uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871  # v4.2.1\n\n      - uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d  # v5.1.0\n        with:\n          python-version: 3.11\n\n      - name: Install yapf (to be available to pre-commit)\n        run: |-\n          pip install \\\n            --disable-pip-version-check \\\n            --no-warn-script-location \\\n            --user \\\n            .\n          echo \"PATH=${HOME}/.local/bin:${PATH}\" >> \"${GITHUB_ENV}\"\n\n      - uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd  # v3.0.1\n"
  },
  {
    "path": ".gitignore",
    "content": "#==============================================================================#\n# This file specifies intentionally untracked files that git should ignore.\n# See: http://www.kernel.org/pub/software/scm/git/docs/gitignore.html\n#\n# This file is intentionally different from the output of `git svn show-ignore`,\n# as most of those are useless.\n#==============================================================================#\n\n#==============================================================================#\n# File extensions to be ignored anywhere in the tree.\n#==============================================================================#\n# Temp files created by most text editors.\n*~\n# Merge files created by git.\n*.orig\n# Compiled python.\n*.pyc\n*.pickle\n# vim swap files\n.*.sw?\n.sw?\n# OS X specific files.\n.DS_store\n\n#==============================================================================#\n# Files to ignore\n#==============================================================================#\n/.coverage\n\n# Directories to ignore (do not add trailing '/'s, they skip symlinks).\n#==============================================================================#\n/build\n/dist\n/.tox\n/yapf.egg-info\n\n# IDEs\n/.idea\n/.vscode/settings.json\n\n# Virtual Environment\n/.venv*/\n\n# Worktrees\n/.wt\n"
  },
  {
    "path": ".isort.cfg",
    "content": "[settings]\nforce_single_line=true\nknown_third_party=yapf_third_party\nknown_yapftests=yapftests\n\nsections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER,YAPFTESTS\n"
  },
  {
    "path": ".pre-commit-config.yaml",
    "content": "# File introduces automated checks triggered on git events\n# to enable run `pip install pre-commit && pre-commit install`\n\nrepos:\n  - repo: https://github.com/pycqa/isort\n    rev: 6.0.0\n    hooks:\n      - id: isort\n        name: isort (python)\n  - repo: local\n    hooks:\n      - id: yapf\n        name: yapf\n        language: python\n        entry: yapf\n        args: [-i]\n        types: [python]\n  - repo: https://github.com/pycqa/flake8\n    rev: 7.1.1\n    hooks:\n      - id: flake8\n  - repo: https://github.com/pre-commit/pre-commit-hooks\n    rev: v5.0.0\n    hooks:\n      - id: trailing-whitespace\n      - id: check-docstring-first\n      - id: check-added-large-files\n      - id: check-yaml\n      - id: debug-statements\n      - id: check-merge-conflict\n      - id: double-quote-string-fixer\n      - id: end-of-file-fixer\n  - repo: meta\n    hooks:\n      - id: check-hooks-apply\n      - id: check-useless-excludes\n"
  },
  {
    "path": ".pre-commit-hooks.yaml",
    "content": "# File configures YAPF to be used as a git hook with https://github.com/pre-commit/pre-commit\n\n- id: yapf\n  name: yapf\n  description: \"A formatter for Python files.\"\n  entry: yapf\n  args: [-i] #inplace\n  language: python\n  types: [python]\n\n- id: yapf-diff\n  name: yapf-diff\n  description: \"A formatter for Python files. (formats only changes included in commit)\"\n  always_run: true\n  language: python\n  pass_filenames: false\n  stages: [pre-commit]\n  entry: |\n    bash -c \"git diff -U0 --no-color --relative HEAD \\\n                  | yapf-diff \\\n                  | tee >(git apply --allow-empty -p0)\"\n"
  },
  {
    "path": ".python-version",
    "content": "3.7.9\n3.8.10\n3.9.13\n3.10.11\n3.11.5\n"
  },
  {
    "path": ".style.yapf",
    "content": "[style]\nbased_on_style = yapf\n"
  },
  {
    "path": ".vimrc",
    "content": "\" Force indentation styles for this directory\nautocmd FileType python set shiftwidth=2\nautocmd FileType python set tabstop=2\nautocmd FileType python set softtabstop=2\n"
  },
  {
    "path": ".vscode/extensions.json",
    "content": "{\n    \"recommendations\": [\n        \"eeyore.yapf\",\n        \"dangmai.workspace-default-settings\",\n        \"ms-python.flake8\",\n        \"ms-python.isort\",\n        \"ms-python.python\",\n    ],\n    // These are remarked as extenstions you should disable for this workspace.\n    // VSCode does not support disabling extensions via workspace config files.\n    \"unwantedRecommendations\": [\n        \"ms-python.black-formatter\",\n        \"ms-python.pylint\"\n    ]\n}\n"
  },
  {
    "path": ".vscode/settings.default.json",
    "content": "{\n    \"editor.codeActionsOnSave\": {\n        \"source.organizeImports\": true\n    },\n    \"files.insertFinalNewline\": true,\n    \"files.trimFinalNewlines\": true,\n    \"[python]\": {\n        \"diffEditor.ignoreTrimWhitespace\": false,\n        \"editor.defaultFormatter\": \"eeyore.yapf\",\n        \"editor.formatOnSaveMode\": \"file\",\n        \"editor.formatOnSave\": true,\n        \"editor.wordBasedSuggestions\": false,\n        \"files.trimTrailingWhitespace\": true,\n    },\n    \"python.analysis.extraPaths\": [\n        \"./third_party\"\n    ],\n    \"python.analysis.typeCheckingMode\": \"basic\",\n    \"python.languageServer\": \"Pylance\",\n    \"files.exclude\": {\n        \"**/*$py.class\": true\n    },\n    \"json.schemas\": [\n        {\n            \"fileMatch\": [\n                \"/.vscode/settings.default.json\"\n            ],\n            \"url\": \"vscode://schemas/settings/folder\"\n        }\n    ],\n    \"workspace-default-settings.runOnActivation\": true,\n    \"workspace-default-settings.jsonIndentation\": 4\n}\n"
  },
  {
    "path": "AUTHORS",
    "content": "# This is the official list of YAPF authors for copyright purposes.\n# This file is distinct from the CONTRIBUTORS files.\n# See the latter for an explanation.\n\n# Names should be added to this file as:\n# Name or Organization <email address>\n# The email address is not required for organizations.\n\nGoogle Inc.\n"
  },
  {
    "path": "CHANGELOG.md",
    "content": "# Change Log\n# All notable changes to this project will be documented in this file.\n# This project adheres to [Semantic Versioning](http://semver.org/).\n\n## (0.41.0) UNRELEASED\n### Added\n- New `DISABLE_SPLIT_LIST_WITH_COMMENT` flag.\n `DISABLE_SPLIT_LIST_WITH_COMMENT` is a new knob that changes the\n  behavior of splitting a list when a comment is present inside the list.\n\n  Before, we split a list containing a comment just like we split a list\n  containing a trailing comma: Each element goes on its own line (unless\n  `DISABLE_ENDING_COMMA_HEURISTIC` is true).\n\n  This new flag allows you to control the behavior of a list with a comment\n  *separately* from the behavior when the list contains a trailing comma.\n\n  This mirrors the behavior of clang-format, and is useful for e.g. forming\n  \"logical groups\" of elements in a list.\n\n  Without this flag:\n\n  ```\n  [\n    a,\n    b,  #\n    c\n  ]\n  ```\n\n  With this flag:\n\n  ```\n  [\n    a, b,  #\n    c\n  ]\n  ```\n\n  Before we had one flag that controlled two behaviors.\n\n    - `DISABLE_ENDING_COMMA_HEURISTIC=false` (default):\n      - Split a list that has a trailing comma.\n      - Split a list that contains a comment.\n    - `DISABLE_ENDING_COMMA_HEURISTIC=true`:\n      - Don't split on trailing comma.\n      - Don't split on comment.\n\n  Now we have two flags.\n\n    - `DISABLE_ENDING_COMMA_HEURISTIC=false` and `DISABLE_SPLIT_LIST_WITH_COMMENT=false` (default):\n      - Split a list that has a trailing comma.\n      - Split a list that contains a comment.\n      Behavior is unchanged from the default before.\n    - `DISABLE_ENDING_COMMA_HEURISTIC=true` and `DISABLE_SPLIT_LIST_WITH_COMMENT=false` :\n      - Don't split on trailing comma.\n      - Do split on comment.  **This is a change in behavior from before.**\n    - `DISABLE_ENDING_COMMA_HEURISTIC=false` and `DISABLE_SPLIT_LIST_WITH_COMMENT=true` :\n      - Split on trailing comma.\n      - Don't split on comment.\n    - `DISABLE_ENDING_COMMA_HEURISTIC=true` and `DISABLE_SPLIT_LIST_WITH_COMMENT=true` :\n      - Don't split on trailing comma.\n      - Don't split on comment.\n      **You used to get this behavior just by setting one flag, but now you have to set both.**\n\n  Note the behavioral change above; if you set\n  `DISABLE_ENDING_COMMA_HEURISTIC=true` and want to keep the old behavior, you\n  now also need to set `DISABLE_SPLIT_LIST_WITH_COMMENT=true`.\n### Changes\n- Remove dependency on importlib-metadata\n- Remove dependency on tomli when using >= py311\n- Format '.pyi' type sub files.\n### Fixed\n- Fix SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED for one-item named argument lists\n  by taking precedence over SPLIT_BEFORE_NAMED_ASSIGNS.\n- Fix SPLIT_ALL_COMMA_SEPARATED_VALUES and SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES\n  being too agressive for lambdas and unpacking.\n\n## [0.40.2] 2023-09-22\n### Changes\n- The verification module has been removed. NOTE: this changes the public APIs\n  by removing the \"verify\" parameter.\n- Changed FORCE_MULTILINE_DICT to override SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES.\n- Adopt pyproject.toml (PEP 517) for build system\n### Fixed\n- Do not treat variables named `match` as the match keyword.\n- Fix SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED for one-item argument lists.\n- Fix trailing backslash-newline on Windows when using stdin.\n\n## [0.40.1] 2023-06-20\n### Fixed\n- Corrected bad distribution v0.40.0 package.\n\n## [0.40.0] 2023-06-13 [YANKED - [#1107](https://github.com/google/yapf/issues/1107)]\n### Added\n- Support for Python 3.11\n- Add the `--print-modified` flag to print out file names of modified files when\n  running in in-place mode.\n### Changes\n- Replace the outdated and no-longer-supported lib2to3 with a fork of blib2to3,\n  Black's version of lib2to3.\n### Removed\n- Support for Python versions < 3.7 are no longer supported.\n\n## [0.33.0] 2023-04-18 [YANKED - [#1154](https://github.com/google/yapf/issues/1154)]\n### Added\n- Add a new Python parser to generate logical lines.\n- Added support for `# fmt: on` and `# fmt: off` pragmas.\n### Changes\n- Moved 'pytree' parsing tools into its own subdirectory.\n- Add support for Python 3.10.\n- Format generated dicts with respect to same rules as regular dicts\n- Generalized the ending comma heuristic to subscripts.\n- Supports \"pyproject.toml\" by default.\n### Fixed\n- Split line before all comparison operators.\n\n## [0.32.0] 2021-12-26\n### Added\n- Look at the 'pyproject.toml' file to see if it contains ignore file information\n  for YAPF.\n- New entry point `yapf_api.FormatTree` for formatting lib2to3 concrete\n  syntax trees.\n- Add CI via GitHub Actions.\n### Changes\n- Change tests to support \"pytest\".\n- Reformat so that \"flake8\" is happy.\n- Use GitHub Actions instead of Travis for CI.\n- Clean up the FormatToken interface to limit how much it relies upon the\n  pytree node object.\n- Rename \"unwrapped_line\" module to \"logical_line.\"\n- Rename \"UnwrappedLine\" class to \"LogicalLine.\"\n### Fixed\n- Added pyproject extra to install toml package as an optional dependency.\n- Enable `BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF` knob for \"pep8\" style, so\n  method definitions inside a class are surrounded by a single blank line as\n  prescribed by PEP8.\n- Fixed the '...' token to be spaced after a colon.\n\n## [0.31.0] 2021-03-14\n### Added\n- Renamed 'master' branch to 'main'.\n- Add 'BLANK_LINES_BETWEEN_TOP_LEVEL_IMPORTS_AND_VARIABLES' to support setting\n  a custom number of blank lines between top-level imports and variable\n  definitions.\n- Ignore end of line `# copybara:` directives when checking line length.\n- Look at the 'pyproject.toml' file to see if it contains style information for\n  YAPF.\n### Changed\n- Do not scan excluded directories. Prior versions would scan an excluded\n  folder then exclude its contents on a file by file basis. Preventing the\n  folder being scanned is faster.\n### Fixed\n- Exclude directories on Windows.\n\n## [0.30.0] 2020-04-23\n### Added\n- Added `SPACES_AROUND_LIST_DELIMITERS`, `SPACES_AROUND_DICT_DELIMITERS`,\n  and `SPACES_AROUND_TUPLE_DELIMITERS` to add spaces after the opening-\n  and before the closing-delimiters for lists, dicts, and tuples.\n- Adds `FORCE_MULTILINE_DICT` knob to ensure dictionaries always split,\n  even when shorter than the max line length.\n- New knob `SPACE_INSIDE_BRACKETS` to add spaces inside brackets, braces, and\n  parentheses.\n- New knob `SPACES_AROUND_SUBSCRIPT_COLON` to add spaces around the subscript /\n  slice operator.\n### Changed\n- Renamed \"chromium\" style to \"yapf\". Chromium will now use PEP-8 directly.\n- `CONTINUATION_ALIGN_STYLE` with `FIXED` or `VALIGN-RIGHT` now works with\n  space indentation.\n### Fixed\n- Honor a disable directive at the end of a multiline comment.\n- Don't require splitting before comments in a list when\n  `SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES` is set. The knob is meant for\n  values, not comments, which may be associated with the current line.\n- Don't over-indent a parameter list when not needed. But make sure it is\n  properly indented so that it doesn't collide with the lines afterwards.\n- Don't split between two-word comparison operators: \"is not\", \"not in\", etc.\n\n## [0.29.0] 2019-11-28\n### Added\n- Add the `--quiet` flag to suppress output. The return code is 1 if there are\n  changes, similarly to the `--diff` flag.\n- Add the `indent_closing_brackets` option. This is the same as the\n  `dedent_closing_brackets` option except the brackets are indented the same\n  as the previous line.\n### Changed\n- Collect a parameter list into a single object. This allows us to track how a\n  parameter list is formatted, keeping state along the way. This helps when\n  supporting Python 3 type annotations.\n- Catch and report `UnicodeDecodeError` exceptions.\n- Improved description of .yapfignore syntax.\n### Fixed\n- Format subscript lists so that splits are essentially free after a comma.\n- Don't add a space between a string and its subscript.\n- Extend discovery of '.style.yapf' & 'setup.cfg' files to search the root\n  directory as well.\n- Make sure we have parameters before we start calculating penalties for\n  splitting them.\n- Indicate if a class/function is nested to ensure blank lines when needed.\n- Fix extra indentation in async-for else statement.\n- A parameter list with no elements shouldn't count as exceeding the column\n  limit.\n- When splitting all comma separated values, don't treat the ending bracket as\n  special.\n- The \"no blank lines between nested classes or functions\" knob should only\n  apply to the first nested class or function, not all of them.\n\n## [0.28.0] 2019-07-11\n### Added\n- New knob `SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES` is a variation on\n  `SPLIT_ALL_COMMA_SEPARATED_VALUES` in which, if a subexpression with a comma\n  fits in its starting line, then the subexpression is not split (thus avoiding\n  unnecessary splits).\n### Changed\n- Set `INDENT_DICTIONARY_VALUE` for Google style.\n- Set `JOIN_MULTIPLE_LINES = False` for Google style.\n### Fixed\n- `BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=False` wasn't honored because the\n  number of newlines was erroneously calculated beforehand.\n- Lambda expressions shouldn't have an increased split penalty applied to the\n  'lambda' keyword. This prevents them from being properly formatted when they're\n  arguments to functions.\n- A comment with continuation markers (??) shouldn't mess with the lineno count.\n- Only emit unformatted if the \"disable long line\" is at the end of the line.\n  Otherwise we could mess up formatting for containers which have them\n  interspersed with code.\n- Fix a potential race condition by using the correct style for opening a file\n  which may not exist.\n\n## [0.27.0] 2019-04-07\n### Added\n- `SPLIT_BEFORE_ARITHMETIC_OPERATOR` splits before an arithmetic operator when\n  set. `SPLIT_PENALTY_ARITHMETIC_OPERATOR` allows you to set the split penalty\n  around arithmetic operators.\n### Changed\n- Catch lib2to3's \"TokenError\" exception and output a nicer message.\n### Fixed\n- Parse integer lists correctly, removing quotes if the list is within a\n  string.\n- Adjust the penalties of bitwise operands for '&' and '^', similar to '|'.\n- Avoid splitting after opening parens if SPLIT_BEFORE_FIRST_ARGUMENT is set\n  to False.\n- Adjust default SPLIT_PENALTY_AFTER_OPENING_BRACKET.\n- Re-enable removal of extra lines on the boundaries of formatted regions.\n- Adjust list splitting to avoid splitting before a dictionary element, because\n  those are likely to be split anyway. If we do split, it leads to horrible\n  looking code.\n- Dictionary arguments were broken in a recent version. It resulted in\n  unreadable formatting, where the remaining arguments were indented far more\n  than the dictionary. Fixed so that if the dictionary is the first argument in\n  a function call and doesn't fit on a single line, then it forces a split.\n- Improve the connectiveness between items in a list. This prevents random\n  splitting when it's not 100% necessary.\n- Don't remove a comment attached to a previous object just because it's part\n  of the \"prefix\" of a function/class node.\n\n## [0.26.0] 2019-02-08\n### Added\n- `ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS` allows us to split before\n  default / named assignments.\n- `ARITHMETIC_PRECEDENCE_INDICATION` removes spacing around binary operators\n  if they have higher precedence than other operators in the same expression.\n### Changed\n- `SPACES_BEFORE_COMMENT` can now be assigned to a specific value (standard\n  behavior) or a list of column values. When assigned to a list, trailing\n  comments will be horizontally aligned to the first column value within\n  the list that is greater than the maximum line length in the block.\n- Don't modify the vertical spacing of a line that has a comment \"pylint:\n  disable=line-too-long\". The line is expected to be too long.\n- improved `CONTINUATION_ALIGN_STYLE` to accept quoted or underline-separated\n  option value for passing option with command line arguments.\n### Fixed\n- When retrieving the opening bracket make sure that it's actually an opening\n  bracket.\n- Don't completely deny a lambda formatting if it goes over the column limit.\n  Split only if absolutely necessary.\n- Bump up penalty for splitting before a dot ('.').\n- Ignore pseudo tokens when calculating split penalties.\n- Increase the penalty for splitting before the first bit of a subscript.\n- Improve splitting before dictionary values. Look more closely to see if the\n  dictionary entry is a container. If so, then it's probably split over\n  multiple lines with the opening bracket on the same line as the key.\n  Therefore, we shouldn't enforce a split because of that.\n- Increase split penalty around exponent operator.\n- Correct spacing when using binary operators on strings with the\n  `NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS` option enabled.\n\n## [0.25.0] 2018-11-25\n### Added\n- Added `INDENT_BLANK_LINES` knob to select whether the blank lines are empty\n  or indented consistently with the current block.\n- Support additional file exclude patterns in .yapfignore file.\n### Fixed\n- Correctly determine if a scope is the last in line. It avoids a wrong\n  computation of the line end when determining if it must split after the\n  opening bracket with `DEDENT_CLOSING_BRACKETS` enabled.\n\n## [0.24.0] 2018-09-07\n### Added\n- Added 'SPLIT_BEFORE_DOT' knob to support \"builder style\" calls. The \"builder\n  style\" option didn't work as advertised. Lines would split after the dots,\n  not before them regardless of the penalties.\n### Changed\n- Support Python 3.7 in the tests. The old \"comp_for\" and \"comp_if\" nodes are\n  now \"old_comp_for\" and \"old_comp_if\" in lib2to3.\n### Fixed\n- Don't count inner function calls when marking arguments as named assignments.\n- Make sure that tuples and the like are formatted nicely if they all can't fit\n  on a single line. This is similar to how we format function calls within an\n  argument list.\n- Allow splitting in a subscript if it goes over the line limit.\n- Increase the split penalty for an if-expression.\n- Increase penalty for splitting in a subscript so that it's more likely to\n  split in a function call or other data literal.\n- Cloning a pytree node doesn't transfer its a annotations. Make sure we do\n  that so that we don't lose information.\n- Revert change that broke the \"no_spaces_around_binary_operators\" option.\n- The \"--style-help\" option would output string lists and sets in Python types.\n  If the output was used as a style, then it wouldn't parse those values\n  correctly.\n\n## [0.23.0] 2018-08-27\n### Added\n- `DISABLE_ENDING_COMMA_HEURISTIC` is a new knob to disable the heuristic which\n  splits a list onto separate lines if the list is comma-terminated.\n### Fixed\n- There's no need to increase N_TOKENS. In fact, it causes other things which\n  use lib2to3 to fail if called from YAPF.\n- Change the exception message instead of creating a new one that's just a\n  clone.\n- Make sure not to reformat when a line is disabled even if the --lines option\n  is specified.\n- The \"no spaces around operators\" flag wasn't correctly converting strings to\n  sets. Changed the regexp to handle it better.\n\n## [0.22.0] 2018-05-15\n### Added\n- The `BLANK_LINE_BEFORE_MODULE_DOCSTRING` knob adds a blank line before a\n  module's docstring.\n- The `SPLIT_ALL_COMMA_SEPARATED_VALUES` knob causes all lists, tuples, dicts\n  function defs, etc... to split on all values, instead of maximizing the\n  number of elements on each line, when not able to fit on a single line.\n### Changed\n- Improve the heuristic we use to determine when to split at the start of a\n  function call. First check whether or not all elements can fit in the space\n  without wrapping. If not, then we split.\n- Check all of the elements of a tuple. Similarly to how arguments are\n  analyzed. This allows tuples to be split more rationally.\n- Adjust splitting penalties around arithmetic operators so that the code can\n  flow more freely. The code must flow!\n- Try to meld an argument list's closing parenthesis to the last argument.\n### Fixed\n- Attempt to determine if long lambdas are allowed. This can be done on a\n  case-by-case basis with a \"pylint\" disable comment.\n- A comment before a decorator isn't part of the decorator's line.\n- Only force a new wrapped line after a comment in a decorator when it's the\n  first token in the decorator.\n\n## [0.21.0] 2018-03-18\n### Added\n- Introduce a new option of formatting multiline literals. Add\n  `SPLIT_BEFORE_CLOSING_BRACKET` knob to control whether closing bracket should\n  get their own line.\n- Added `CONTINUATION_ALIGN_STYLE` knob to choose continuation alignment style\n  when `USE_TABS` is enabled.\n- Add 'BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION' knob to control the number\n  of blank lines between top-level function and class definitions.\n### Fixed\n- Don't split ellipses.\n\n## [0.20.2] 2018-02-12\n### Changed\n- Improve the speed at which files are excluded by ignoring them earlier.\n- Allow dictionaries to stay on a single line if they only have one entry\n### Fixed\n- Use tabs when constructing a continuation line when `USE_TABS` is enabled.\n- A dictionary entry may not end in a colon, but may be an \"unpacking\"\n  operation: `**foo`. Take that into account and don't split after the\n  unpacking operator.\n\n## [0.20.1] 2018-01-13\n### Fixed\n- Don't treat 'None' as a keyword if calling a function on it, like '__ne__()'.\n- use_tabs=True always uses a single tab per indentation level; spaces are\n  used for aligning vertically after that.\n- Relax the split of a paren at the end of an if statement. With\n  `dedent_closing_brackets` option requires that it be able to split there.\n\n## [0.20.0] 2017-11-14\n### Added\n- Improve splitting of comprehensions and generators. Add\n  `SPLIT_PENALTY_COMPREHENSION` knob to control preference for keeping\n  comprehensions on a single line and `SPLIT_COMPLEX_COMPREHENSION` to enable\n  splitting each clause of complex comprehensions onto its own line.\n### Changed\n- Take into account a named function argument when determining if we should\n  split before the first argument in a function call.\n- Split before the first argument in a function call if the arguments contain a\n  dictionary that doesn't fit on a single line.\n- Improve splitting of elements in a tuple. We want to split if there's a\n  function call in the tuple that doesn't fit on the line.\n### Fixed\n- Enforce spaces between ellipses and keywords.\n- When calculating the split penalty for a \"trailer\", process the child nodes\n  afterwards because their penalties may change. For example if a list\n  comprehension is an argument.\n- Don't enforce a split before a comment after the opening of a container if it\n  doesn't it on the current line. We try hard not to move such comments around.\n- Use a TextIOWrapper when reading from stdin in Python3. This is necessary for\n  some encodings, like cp936, used on Windows.\n- Remove the penalty for a split before the first argument in a function call\n  where the only argument is a generator expression.\n\n## [0.19.0] 2017-10-14\n### Added\n- Added `SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN` that enforces a split\n  after the opening paren of an expression that's surrounded by parens.\n### Changed\n- Split before the ending bracket of a comma-terminated tuple / argument list\n  if it's not a single element tuple / arg list.\n### Fixed\n- Prefer to split after a comma in an argument list rather than in the middle\n  of an argument.\n- A non-multiline string may have newlines if it contains continuation markers\n  itself. Don't add a newline after the string when retaining the vertical\n  space.\n- Take into account the \"async\" keyword when determining if we must split\n  before the first argument.\n- Increase affinity for \"atom\" arguments in function calls. This helps prevent\n  lists from being separated when they don't need to be.\n- Don't place a dictionary argument on its own line if it's the last argument\n  in the function call where that function is part of a builder-style call.\n- Append the \"var arg\" type to a star in a star_expr.\n\n## [0.18.0] 2017-09-18\n### Added\n- Option `ALLOW_SPLIT_BEFORE_DICT_VALUE` allows a split before a value. If\n  False, then it won't be split even if it goes over the column limit.\n### Changed\n- Use spaces around the '=' in a typed name argument to align with 3.6 syntax.\n### Fixed\n- Allow semicolons if the line is disabled.\n- Fix issue where subsequent comments at decreasing levels of indentation\n  were improperly aligned and/or caused output with invalid syntax.\n- Fix issue where specifying a line range removed a needed line before a\n  comment.\n- Fix spacing between unary operators if one is 'not'.\n- Indent the dictionary value correctly if there's a multi-line key.\n- Don't remove needed spacing before a comment in a dict when in \"chromium\"\n  style.\n- Increase indent for continuation line with same indent as next logical line\n  with 'async with' statement.\n\n## [0.17.0] 2017-08-20\n### Added\n- Option `NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS` prevents adding spaces\n  around selected binary operators, in accordance with the current style guide.\n### Changed\n- Adjust blank lines on formatting boundaries when using the `--lines` option.\n- Return 1 if a diff changed the code. This is in line with how GNU diff acts.\n- Add `-vv` flag to print out file names as they are processed\n### Fixed\n- Corrected how `DEDENT_CLOSING_BRACKETS` and `COALESCE_BRACKETS` interacted.\n- Fix return value to return a boolean.\n- Correct vim plugin not to clobber edited code if yapf returns an error.\n- Ensured comma-terminated tuples with multiple elements are split onto separate lines.\n\n## [0.16.3] 2017-07-13\n### Changed\n- Add filename information to a ParseError exception.\n### Fixed\n- A token that ends in a continuation marker may have more than one newline in\n  it, thus changing its \"lineno\" value. This can happen if multiple\n  continuation markers are used with no intervening tokens. Adjust the line\n  number to account for the lines covered by those markers.\n- Make sure to split after a comment even for \"pseudo\" parentheses.\n\n## [0.16.2] 2017-05-19\n### Fixed\n- Treat expansion operators ('*', '**') in a similar way to function calls to\n  avoid splitting directly after the opening parenthesis.\n- Increase the penalty for splitting after the start of a tuple.\n- Increase penalty for excess characters.\n- Check that we have enough children before trying to access them all.\n- Remove trailing whitespaces from comments.\n- Split before a function call in a list if the full list isn't able to fit on\n  a single line.\n- Trying not to split around the '=' of a named assign.\n- Changed split before the first argument behavior to ignore compound\n  statements like if and while, but not function declarations.\n- Changed coalesce brackets not to line split before closing bracket.\n\n## [0.16.1] 2017-03-22\n### Changed\n- Improved performance of cloning the format decision state object. This\n  improved the time in one *large* case from 273.485s to 234.652s.\n- Relax the requirement that a named argument needs to be on one line. Going\n  over the column limit is more of an issue to pylint than putting named args\n  on multiple lines.\n- Don't make splitting penalty decisions based on the original formatting. This\n  can and does lead to non-stable formatting, where yapf will reformat the same\n  code in different ways.\n### Fixed\n- Ensure splitting of arguments if there's a named assign present.\n- Prefer to coalesce opening brackets if it's not at the beginning of a\n  function call.\n- Prefer not to squish all of the elements in a function call over to the\n  right-hand side. Split the arguments instead.\n- We need to split a dictionary value if the first element is a comment anyway,\n  so don't force the split here. It's forced elsewhere.\n- Ensure tabs are used for continued indentation when USE_TABS is True.\n\n## [0.16.0] 2017-02-05\n### Added\n- The `EACH_DICT_ENTRY_ON_SEPARATE_LINE` knob indicates that each dictionary\n  entry should be in separate lines if the full dictionary isn't able to fit on\n  a single line.\n- The `SPLIT_BEFORE_DICT_SET_GENERATOR` knob splits before the `for` part of a\n  dictionary/set generator.\n- The `BLANK_LINE_BEFORE_CLASS_DOCSTRING` knob adds a blank line before a\n  class's docstring.\n- The `ALLOW_MULTILINE_DICTIONARY_KEYS` knob allows dictionary keys to span\n  more than one line.\n### Fixed\n- Split before all entries in a dict/set or list maker when comma-terminated,\n  even if there's only one entry.\n- Will now try to set O_BINARY mode on stdout under Windows and Python 2.\n- Avoid unneeded newline transformation when writing formatted code to\n  output on (affects only Python 2)\n\n## [0.15.2] 2017-01-29\n### Fixed\n- Don't perform a global split when a named assign is part of a function call\n  which itself is an argument to a function call. I.e., don't cause 'a' to\n  split here:\n\n      func(a, b, c, d(x, y, z=42))\n- Allow splitting inside a subscript if it's a logical or bitwise operating.\n  This should keep the subscript mostly contiguous otherwise.\n\n## [0.15.1] 2017-01-21\n### Fixed\n- Don't insert a space between a type hint and the '=' sign.\n- The '@' operator can be used in Python 3 for matrix multiplication. Give the\n  '@' in the decorator a DECORATOR subtype to distinguish it.\n- Encourage the formatter to split at the beginning of an argument list instead\n  of in the middle. Especially if the middle is an empty parameter list. This\n  adjusts the affinity of binary and comparison operators. In particular, the\n  \"not in\" and other such operators don't want to have a split after it (or\n  before it) if at all possible.\n\n## [0.15.0] 2017-01-12\n### Added\n- Keep type annotations intact as much as possible. Don't try to split the over\n  multiple lines.\n### Fixed\n- When determining if each element in a dictionary can fit on a single line, we\n  are skipping dictionary entries. However, we need to ignore comments in our\n  calculations and implicitly concatenated strings, which are already placed on\n  separate lines.\n- Allow text before a \"pylint\" comment.\n- Also allow text before a \"yapf: (disable|enable)\" comment.\n\n## [0.14.0] 2016-11-21\n### Added\n- formatting can be run in parallel using the \"-p\" / \"--parallel\" flags.\n### Fixed\n- \"not in\" and \"is not\" should be subtyped as binary operators.\n- A non-Node dictionary value may have a comment before it. In those cases, we\n  want to avoid encompassing only the comment in pseudo parens. So we include\n  the actual value as well.\n- Adjust calculation so that pseudo-parentheses don't count towards the total\n  line length.\n- Don't count a dictionary entry as not fitting on a single line in a\n  dictionary.\n- Don't count pseudo-parentheses in the length of the line.\n\n## [0.13.2] 2016-10-22\n### Fixed\n- REGRESSION: A comment may have a prefix with newlines in it. When calculating\n  the prefix indent, we cannot take the newlines into account. Otherwise, the\n  comment will be misplaced causing the code to fail.\n\n## [0.13.1] 2016-10-17\n### Fixed\n- Correct emitting a diff that was accidentally removed.\n\n## [0.13.0] 2016-10-16\n### Added\n- Added support to retain the original line endings of the source code.\n\n### Fixed\n- Functions or classes with comments before them were reformatting the comments\n  even if the code was supposed to be ignored by the formatter. We now don't\n  adjust the whitespace before a function's comment if the comment is a\n  \"disabled\" line. We also don't count \"# yapf: {disable|enable}\" as a disabled\n  line, which seems logical.\n- It's not really more readable to split before a dictionary value if it's part\n  of a dictionary comprehension.\n- Enforce two blank lines after a function or class definition, even before a\n  comment. (But not between a decorator and a comment.) This is related to PEP8\n  error E305.\n- Remove O(n^2) algorithm from the line disabling logic.\n\n## [0.12.2] 2016-10-09\n### Fixed\n- If `style.SetGlobalStyle(<create pre-defined style>)` was called and then\n  `yapf_api.FormatCode` was called, the style set by the first call would be\n  lost, because it would return the style created by `DEFAULT_STYLE_FACTORY`,\n  which is set to PEP8 by default. Fix this by making the first call set which\n  factory we call as the \"default\" style.\n- Don't force a split before non-function call arguments.\n- A dictionary being used as an argument to a function call and which can exist\n  on a single line shouldn't be split.\n- Don't rely upon the original line break to determine if we should split\n  before the elements in a container. Especially split if there's a comment in\n  the container.\n- Don't add spaces between star and args in a lambda expression.\n- If a nested data structure terminates in a comma, then split before the first\n  element, but only if there's more than one element in the list.\n\n## [0.12.1] 2016-10-02\n### Changed\n- Dictionary values will be placed on the same line as the key if *all* of the\n  elements in the dictionary can be placed on one line. Otherwise, the\n  dictionary values will be placed on the next line.\n\n### Fixed\n- Prefer to split before a terminating r-paren in an argument list if the line\n  would otherwise go over the column limit.\n- Split before the first key in a dictionary if the dictionary cannot fit on a\n  single line.\n- Don't count \"pylint\" comments when determining if the line goes over the\n  column limit.\n- Don't count the argument list of a lambda as a named assign in a function\n  call.\n\n## [0.12.0] 2016-09-25\n### Added\n- Support formatting of typed names. Typed names are formatted a similar way to\n  how named arguments are formatted, except that there's a space after the\n  colon.\n- Add a knob, 'SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN', to allow adding spaces\n  around the assign operator on default or named assigns.\n\n## Changed\n- Turn \"verification\" off by default for external APIs.\n- If a function call in an argument list won't fit on the current line but will\n  fit on a line by itself, then split before the call so that it won't be split\n  up unnecessarily.\n\n## Fixed\n- Don't add space after power operator if the next operator's a unary operator.\n\n## [0.11.1] 2016-08-17\n### Changed\n- Issue #228: Return exit code 0 on success, regardless of whether files were\n  changed.  (Previously, 0 meant success with no files\n  modified, and 2 meant success with at least one file modified.)\n\n### Fixed\n- Enforce splitting each element in a dictionary if comma terminated.\n- It's okay to split in the middle of a dotted name if the whole expression is\n  going to go over the column limit.\n- Asynchronous functions were going missing if they were preceded by a comment\n  (a what? exactly). The asynchronous function processing wasn't taking the\n  comment into account and thus skipping the whole function.\n- The splitting of arguments when comma terminated had a conflict. The split\n  penalty of the closing bracket was set to the maximum, but it shouldn't be if\n  the closing bracket is preceded by a comma.\n\n## [0.11.0] 2016-07-17\n### Added\n- The COALESCE_BRACKETS knob prevents splitting consecutive brackets when\n  DEDENT_CLOSING_BRACKETS is set.\n- Don't count \"pylint\" directives as exceeding the column limit.\n\n### Changed\n- We split all of the arguments to a function call if there's a named argument.\n  In this case, we want to split after the opening bracket too. This makes\n  things look a bit better.\n\n### Fixed\n- When retaining format of a multiline string with Chromium style, make sure\n  that the multiline string doesn't mess up where the following comma ends up.\n- Correct for when 'lib2to3' smooshes comments together into the same DEDENT\n  node.\n\n## [0.10.0] 2016-06-14\n### Added\n- Add a knob, 'USE_TABS', to allow using tabs for indentation.\n\n### Changed\n- Performance enhancements.\n\n### Fixed\n- Don't split an import list if it's not surrounded by parentheses.\n\n## [0.9.0] 2016-05-29\n### Added\n- Added a knob (SPLIT_PENALTY_BEFORE_IF_EXPR) to adjust the split penalty\n  before an if expression. This allows the user to place a list comprehension\n  all on one line.\n- Added a knob (SPLIT_BEFORE_FIRST_ARGUMENT) that encourages splitting before\n  the first element of a list of arguments or parameters if they are going to\n  be split anyway.\n- Added a knob (SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED) splits arguments to a\n  function if the list is terminated by a comma.\n\n### Fixed\n- Don't split before a first element list argument as we would before a first\n  element function call.\n- Don't penalize when we must split a line.\n- Allow splitting before the single argument in a function call.\n\n## [0.8.2] 2016-05-21\n### Fixed\n- Prefer not to split after the opening of a subscript.\n- Don't add space before the 'await' keyword if it's preceded by an opening\n  paren.\n- When we're setting the split penalty for a continuous list, we don't want to\n  mistake a comment at the end of that list as part of the list.\n- When calculating blank lines, don't assume the last seen object was a class\n  or function when we're in a class or function.\n- Don't count the closing scope when determining if the current scope is the\n  last scope on the line.\n\n## [0.8.1] 2016-05-18\n### Fixed\n- 'SPLIT_BEFORE_LOGICAL_OPERATOR' wasn't working correctly. The penalty was\n  being set incorrectly when it was part of a larger construct.\n- Don't separate a keyword, like \"await\", from a left paren.\n- Don't rely upon the original tokens' line number to determine if we should\n  perform splitting in Facebook mode. The line number isn't the line number of\n  the reformatted token, but the line number where it was in the original code.\n  Instead, we need to carefully determine if the line is liabel to be split and\n  act accordingly.\n\n## [0.8.0] 2016-05-10\n### Added\n- Add a knob, 'SPACES_AROUND_POWER_OPERATOR', to allow adding spaces around the\n  power operator.\n\n### Fixed\n- There shouldn't be a space between a decorator and an intervening comment.\n- If we split before a bitwise operator, then we assume that the programmer\n  knows what they're doing, more or less, and so we enforce a split before said\n  operator if one exists in the original program.\n- Strengthen the bond between a keyword and value argument.\n- Don't add a blank line after a multiline string.\n- If the \"for\" part of a list comprehension can exist on the starting line\n  without going over the column limit, then let it remain there.\n\n## [0.7.1] 2016-04-21\n### Fixed\n- Don't rewrite the file if there are no changes.\n- Ensure the proper number of blank lines before an async function.\n- Split after a bitwise operator when in PEP 8 mode.\n- Retain the splitting within a dictionary data literal between the key and\n  value.\n- Try to keep short function calls all on one line even if they're part of a\n  larger series of tokens. This stops us from splitting too much.\n\n## [0.7.0] 2016-04-09\n### Added\n- Support for Python 3.5.\n- Add 'ALLOW_MULTILINE_LAMBDAS' which allows lambdas to be formatted onto\n  multiple lines.\n\n### Fixed\n- Lessen penalty for splitting before a dictionary keyword.\n- Formatting of trailing comments on disabled formatting lines.\n- Disable / enable formatting at end of multi-line comment.\n\n## [0.6.3] 2016-03-06\n### Changed\n- Documentation updated.\n\n### Fixed\n- Fix spacing of multiline comments when formatting is disabled.\n\n## [0.6.2] 2015-11-01\n### Changed\n- Look at the 'setup.cfg' file to see if it contains style information for\n  YAPF.\n- Look at the '~/.config/yapf/style' file to see if it contains global style\n  information for YAPF.\n\n### Fixed\n- Make lists that can fit on one line more likely to stay together.\n- Correct formatting of '*args' and '**kwargs' when there are default values in\n  the argument list.\n\n## [0.6.1] 2015-10-24\n### Fixed\n- Make sure to align comments in data literals correctly. Also make sure we\n  don't count a \"#.\" in a string as an i18n comment.\n- Retain proper vertical spacing before comments in a data literal.\n- Make sure that continuations from a compound statement are distinguished from\n  the succeeding line.\n- Ignore preceding comments when calculating what is a \"dictionary maker\".\n- Add a small penalty for splitting before a closing bracket.\n- Ensure that a space is enforced after we remove a pseudo-paren that's between\n  two names, keywords, numbers, etc.\n- Increase the penalty for splitting after a pseudo-paren. This could lead to\n  less readable code in some circumstances.\n\n## [0.6.0] 2015-10-18\n### Added\n- Add knob to indent the dictionary value if there is a split before it.\n\n### Changed\n- No longer check that a file is a \"Python\" file unless the '--recursive' flag\n  is specified.\n- No longer allow the user to specify a directory unless the '--recursive' flag\n  is specified.\n\n### Fixed\n- When determining if we should split a dictionary's value to a new line, use\n  the longest entry instead of the total dictionary's length. This allows the\n  formatter to reformat the dictionary in a more consistent manner.\n- Improve how list comprehensions are formatted. Make splitting dependent upon\n  whether the \"comp_for\" or \"comp_if\" goes over the column limit.\n- Don't over indent if expression hanging indents if we expect to dedent the\n  closing bracket.\n- Improve splitting heuristic when the first argument to a function call is\n  itself a function call with arguments. In cases like this, the remaining\n  arguments to the function call would look badly aligned, even though they are\n  technically correct (the best kind of correct!).\n- Improve splitting heuristic more so that if the first argument to a function\n  call is a data literal that will go over the column limit, then we want to\n  split before it.\n- Remove spaces around '**' operator.\n- Retain formatting of comments in the middle of an expression.\n- Don't add a newline to an empty file.\n- Over indent a function's parameter list if it's not distinguished from the\n  body of the function.\n\n## [0.5.0] 2015-10-11\n### Added\n- Add option to exclude files/directories from formatting.\n- Add a knob to control whether import names are split after the first '('.\n\n### Fixed\n- Indent the continuation of an if-then statement when it's not distinguished\n  from the body of the if-then.\n- Allow for sensible splitting of array indices where appropriate.\n- Prefer to not split before the ending bracket of an atom. This produces\n  better code in most cases.\n- Corrected how horizontal spaces were presevered in a disabled region.\n\n## [0.4.0] 2015-10-07\n### Added\n- Support for dedenting closing brackets, \"facebook\" style.\n\n### Fixed\n- Formatting of tokens after a multiline string didn't retain their horizontal\n  spacing.\n\n## [0.3.1] 2015-09-30\n### Fixed\n- Format closing scope bracket correctly when indentation size changes.\n\n## [0.3.0] 2015-09-20\n### Added\n- Return a 2 if the source changed, 1 on error, and 0 for no change.\n\n### Fixed\n- Make sure we format if the \"lines\" specified are in the middle of a\n  statement.\n\n## [0.2.9] - 2015-09-13\n### Fixed\n- Formatting of multiple files. It was halting after formatting the first file.\n\n## [0.2.8] - 2015-09-12\n### Added\n- Return a non-zero exit code if the source was changed.\n- Add bitwise operator splitting penalty and prefer to split before bitwise\n  operators.\n\n### Fixed\n- Retain vertical spacing between disabled and enabled lines.\n- Split only at start of named assign.\n- Retain comment position when formatting is disabled.\n- Honor splitting before or after logical ops.\n"
  },
  {
    "path": "CONTRIBUTING.md",
    "content": "# How to Contribute\n\nWant to contribute? Great! First, read this page (including the small print at\nthe end).\n\n## Before you contribute\n\nBefore we can use your code, you must sign the [Google Individual Contributor\nLicense Agreement](https://developers.google.com/open-source/cla/individual?csw=1)\n(CLA), which you can do online. The CLA is necessary mainly because you own the\ncopyright to your changes, even after your contribution becomes part of our\ncodebase, so we need your permission to use and distribute your code. We also\nneed to be sure of various other things—for instance that you'll tell us if you\nknow that your code infringes on other people's patents. You don't have to sign\nthe CLA until after you've submitted your code for review and a member has\napproved it, but you must do it before we can put your code into our codebase.\nBefore you start working on a larger contribution, you should get in touch with\nus first through the issue tracker with your idea so that we can help out and\npossibly guide you. Coordinating up front makes it much easier to avoid\nfrustration later on.\n\n## Code reviews\n\nAll submissions, including submissions by project members, require review. We\nuse Github pull requests for this purpose.\n\n## YAPF coding style\n\nYAPF follows the [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html)\nwith two exceptions:\n\n- 2 spaces for indentation rather than 4.\n- CamelCase for function and method names rather than `snake_case`.\n\nThe rationale for this is that YAPF was initially developed at Google where\nthese two exceptions are still part of the internal Python style guide.\n\n## Getting started\nYAPF supports using tox 3 for creating a local dev environment, testing, and\nbuilding redistributables. See [HACKING.md](HACKING.md) for more info.\n\n```bash\n$ pipx run --spec='tox<4' tox --devenv .venv\n```\n\n## Small print\n\nContributions made by corporations are covered by a different agreement than\nthe one above, the Software Grant and Corporate Contributor License Agreement.\n"
  },
  {
    "path": "CONTRIBUTORS",
    "content": "# People who have agreed to one of the CLAs and can contribute patches.\n# The AUTHORS file lists the copyright holders; this file\n# lists people.  For example, Google employees are listed here\n# but not in AUTHORS, because Google holds the copyright.\n#\n# https://developers.google.com/open-source/cla/individual\n# https://developers.google.com/open-source/cla/corporate\n#\n# Names should be added to this file as:\n#     Name <email address>\n\nBill Wendling <morbo@google.com>\nEli Bendersky <eliben@google.com>\nSam Clegg <sbc@google.com>\nŁukasz Langa <ambv@fb.com>\nOleg Butuzov <butuzov@made.ua>\nMauricio Herrera Cuadra <mauricio@arareko.net>\nKyle Gottfried <kyle.gottfried@outlook.com>\n"
  },
  {
    "path": "EDITOR SUPPORT.md",
    "content": "# Using YAPF with your editor\n\nYAPF is supported by multiple editors via community extensions or plugins.\n\n- [IntelliJ/PyCharm](#intellijpycharm)\n- [IPython](#ipython)\n- [VSCode](#vscode)\n\n## IntelliJ/PyCharm\n\nUse the `File Watchers` plugin to run YAPF against a file when you perform a save.\n\n1.  Install the [File Watchers](https://www.jetbrains.com/help/idea/using-file-watchers.html) Plugin\n1.  Add the following `.idea/watcherTasks.xml` to your project. If you already have this file just add the `TaskOptions` section from below. This example uses Windows and a virtual environment, modify the `program` option as appropriate.\n    ```xml\n    <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n    <project version=\"4\">\n        <component name=\"ProjectTasksOptions\">\n            <TaskOptions isEnabled=\"true\">\n                <option name=\"arguments\" value=\"-i $FilePathRelativeToProjectRoot$\" />\n                <option name=\"checkSyntaxErrors\" value=\"true\" />\n                <option name=\"description\" />\n                <option name=\"exitCodeBehavior\" value=\"ERROR\" />\n                <option name=\"fileExtension\" value=\"py\" />\n                <option name=\"immediateSync\" value=\"true\" />\n                <option name=\"name\" value=\"yapf\" />\n                <option name=\"output\" value=\"\" />\n                <option name=\"outputFilters\">\n                    <array />\n                </option>\n                <option name=\"outputFromStdout\" value=\"false\" />\n                <option name=\"program\" value=\"$PROJECT_DIR$/.venv/Scripts/yapf.exe\" />\n                <option name=\"runOnExternalChanges\" value=\"true\" />\n                <option name=\"scopeName\" value=\"Project Files\" />\n                <option name=\"trackOnlyRoot\" value=\"false\" />\n                <option name=\"workingDir\" value=\"$Projectpath$\" />\n                <envs />\n            </TaskOptions>\n        </component>\n    </project>\n    ```\n\n## IPython\n\nIPython supports formatting lines automatically when you press the `<Enter>` button to submit the current code block.\n\nMake sure that the YAPF module is available to the IPython runtime:\n\n```shell\npip install ipython yapf\n```\n\npipx example:\n\n```shell\npipx install ipython\npipx inject ipython yapf\n```\n\nAdd following to `~/.ipython/profile_default/ipython_config.py`:\n\n```python\nc.TerminalInteractiveShell.autoformatter = 'yapf'\n```\n\n## VSCode\n\nVSCode has deprecated support for YAPF in its official Python extension [in favor of dedicated formatter extensions](https://github.com/microsoft/vscode-python/wiki/Migration-to-Python-Tools-Extensions).\n\n1. Install EeyoreLee's [yapf](https://marketplace.visualstudio.com/items?itemName=eeyore.yapf) extension.\n1. Install the yapf package from pip.\n   ```\n   pip install yapf\n   ```\n1. Add the following to VSCode's `settings.json`:\n   ```jsonc\n   \"[python]\": {\n       \"editor.formatOnSaveMode\": \"file\",\n       \"editor.formatOnSave\": true,\n       \"editor.defaultFormatter\": \"eeyore.yapf\"  # choose this extension\n   },\n   ```\n"
  },
  {
    "path": "HACKING.md",
    "content": "## Running YAPF on itself\n\n- To run YAPF on all of YAPF:\n\n```bash\n$ pipx run --spec=${PWD} --no-cache yapf -m -i -r yapf/ yapftests/ third_party/\n```\n\n- To run YAPF on just the files changed in the current git branch:\n\n```bash\n$ pipx run --spec=${PWD} --no-cache yapf -m -i $(git diff --name-only @{upstream})\n```\n\n## Testing and building redistributables locally\n\nYAPF uses tox 3 to test against multiple python versions and to build redistributables.\n\nTox will opportunistically use pyenv environments when available.\nTo configure pyenv run the following in bash:\n\n```bash\n$ xargs -t -n1 pyenv install  < .python-version\n```\n\nTest against all supported Python versions that are currently installed:\n```bash\n$ pipx run --spec='tox<4' tox\n```\n\nBuild and test the sdist and wheel against your default Python environment. The redistributables will be in the `dist` directory.\n```bash\n$ pipx run --spec='tox<4' tox -e bdist_wheel -e sdist\n```\n\n## Releasing a new version\n\n1. Install all expected pyenv environements\n    ```bash\n    $ xargs -t -n1 pyenv install  < .python-version\n    ```\n\n1. Run tests against Python 3.7 - 3.11 with\n    ```bash\n    $ pipx run --spec='tox<4' tox\n    ```\n\n1. Bump version in `yapf/_version.py`.\n\n1. Build and test redistributables\n\n    ```bash\n    $ pipx run --spec='tox<4' tox -e bdist_wheel -e sdist\n    ```\n\n1. Check that it looks OK.\n   1. Install it onto a virtualenv,\n   1. run tests, and\n   1. run yapf as a tool.\n\n1. Push to PyPI:\n\n    ```bash\n    $ pipx run twine upload dist/*\n    ```\n\n1. Test in a clean virtualenv that 'pip install yapf' works with the new\n  version.\n\n1. Commit the version bump and add tag with:\n\n    ```bash\n    $ git tag v$(VERSION_NUM)\n    $ git push --tags\n    ```\n"
  },
  {
    "path": "LICENSE",
    "content": "\n                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n\n   APPENDIX: How to apply the Apache License to your work.\n\n      To apply the Apache License to your work, attach the following\n      boilerplate notice, with the fields enclosed by brackets \"[]\"\n      replaced with your own identifying information. (Don't include\n      the brackets!)  The text should be enclosed in the appropriate\n      comment syntax for the file format. We also recommend that a\n      file or class name and description of purpose be included on the\n      same \"printed page\" as the copyright notice for easier\n      identification within third-party archives.\n\n   Copyright [yyyy] [name of copyright owner]\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n"
  },
  {
    "path": "MANIFEST.in",
    "content": "include HACKING.md LICENSE AUTHORS CHANGELOG.md CONTRIBUTING.md CONTRIBUTORS\ninclude .coveragerc .editorconfig .flake8 plugins/README.md\ninclude plugins/vim/autoload/yapf.vim plugins/vim/plugin/yapf.vim pylintrc\ninclude .style.yapf tox.ini .travis.yml .vimrc\n"
  },
  {
    "path": "README.md",
    "content": "# YAPF\n\n<p align=\"center\">\n<a href=\"https://badge.fury.io/py/yapf\"><img alt=\"PyPI Version\" src=\"https://badge.fury.io/py/yapf.svg\"></a>\n<a href=\"https://github.com/google/yapf/actions/workflows/ci.yml\"><img alt=\"Build Status\" src=\"https://github.com/google/yapf/actions/workflows/ci.yml/badge.svg\"></a>\n<a href=\"https://github.com/google/yapf/actions/workflows/pre-commit.yml\"><img alt=\"Actions Status\" src=\"https://github.com/google/yapf/actions/workflows/pre-commit.yml/badge.svg\"></a>\n<a href=\"https://coveralls.io/github/google/yapf?branch=main\"><img alt=\"Coverage Status\" src=\"https://coveralls.io/repos/github/google/yapf/badge.svg?branch=main\"></a>\n</p>\n\n\n## Introduction\n\nYAPF is a Python formatter based on [`clang-format`](https://clang.llvm.org/docs/ClangFormat.html)\n(developed by Daniel Jasper). In essence, the algorithm takes the code and\ncalculates the best formatting that conforms to the configured style. It takes\naway a lot of the drudgery of maintaining your code.\n\nThe ultimate goal is that the code YAPF produces is as good as the code that a\nprogrammer would write if they were following the style guide.\n\n> **Note**\n> YAPF is not an official Google product (experimental or otherwise), it is\n> just code that happens to be owned by Google.\n\n\n## Installation\n\nTo install YAPF from PyPI:\n\n```bash\n$ pip install yapf\n```\n\nYAPF is still considered in \"beta\" stage, and the released version may change\noften; therefore, the best way to keep up-to-date with the latest development\nis to clone this repository or install directly from github:\n\n```bash\n$ pip install git+https://github.com/google/yapf.git\n```\n\nNote that if you intend to use YAPF as a command-line tool rather than as a\nlibrary, installation is not necessary. YAPF supports being run as a directory\nby the Python interpreter. If you cloned/unzipped YAPF into `DIR`, it's\npossible to run:\n\n```bash\n$ PYTHONPATH=DIR python DIR/yapf [options] ...\n```\n\n## Using YAPF within your favorite editor\nYAPF is supported by multiple editors via community extensions or plugins. See [Editor Support](EDITOR%20SUPPORT.md) for more info.\n\n## Required Python versions\n\nYAPF supports Python 3.7+.\n\n\n## Usage\n\n```console\nusage: yapf [-h] [-v] [-d | -i | -q] [-r | -l START-END] [-e PATTERN]\n            [--style STYLE] [--style-help] [--no-local-style] [-p] [-m] [-vv]\n            [files ...]\n\nFormatter for Python code.\n\npositional arguments:\n  files                 reads from stdin when no files are specified.\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -v, --version         show program's version number and exit\n  -d, --diff            print the diff for the fixed source\n  -i, --in-place        make changes to files in place\n  -q, --quiet           output nothing and set return value\n  -r, --recursive       run recursively over directories\n  -l START-END, --lines START-END\n                        range of lines to reformat, one-based\n  -e PATTERN, --exclude PATTERN\n                        patterns for files to exclude from formatting\n  --style STYLE         specify formatting style: either a style name (for\n                        example \"pep8\" or \"google\"), or the name of a file\n                        with style settings. The default is pep8 unless a\n                        .style.yapf or setup.cfg or pyproject.toml file\n                        located in the same directory as the source or one of\n                        its parent directories (for stdin, the current\n                        directory is used).\n  --style-help          show style settings and exit; this output can be saved\n                        to .style.yapf to make your settings permanent\n  --no-local-style      don't search for local style definition\n  -p, --parallel        run YAPF in parallel when formatting multiple files.\n  -m, --print-modified  print out file names of modified files\n  -vv, --verbose        print out file names while processing\n```\n\n\n### Return Codes\n\nNormally YAPF returns zero on successful program termination and non-zero\notherwise.\n\nIf `--diff` is supplied, YAPF returns zero when no changes were necessary,\nnon-zero otherwise (including program error). You can use this in a CI workflow\nto test that code has been YAPF-formatted.\n\n### Excluding files from formatting (.yapfignore or pyproject.toml)\n\nIn addition to exclude patterns provided on commandline, YAPF looks for\nadditional patterns specified in a file named `.yapfignore` or `pyproject.toml`\nlocated in the working directory from which YAPF is invoked.\n\n`.yapfignore`'s syntax is similar to UNIX's filename pattern matching:\n\n```\n*       matches everything\n?       matches any single character\n[seq]   matches any character in seq\n[!seq]  matches any character not in seq\n```\n\nNote that no entry should begin with `./`.\n\nIf you use `pyproject.toml`, exclude patterns are specified by `ignore_patterns` key\nin `[tool.yapfignore]` section. For example:\n\n```ini\n[tool.yapfignore]\nignore_patterns = [\n  \"temp/**/*.py\",\n  \"temp2/*.py\"\n]\n```\n\n\nFormatting style\n================\n\nThe formatting style used by YAPF is configurable and there are many \"knobs\"\nthat can be used to tune how YAPF does formatting. See the `style.py` module\nfor the full list.\n\nTo control the style, run YAPF with the `--style` argument. It accepts one of\nthe predefined styles (e.g., `pep8` or `google`), a path to a configuration\nfile that specifies the desired style, or a dictionary of key/value pairs.\n\nThe config file is a simple listing of (case-insensitive) `key = value` pairs\nwith a `[style]` heading. For example:\n\n```ini\n[style]\nbased_on_style = pep8\nspaces_before_comment = 4\nsplit_before_logical_operator = true\n```\n\nThe `based_on_style` setting determines which of the predefined styles this\ncustom style is based on (think of it like subclassing). Four\nstyles are predefined:\n\n- `pep8` (default)\n- `google` (based off of the [Google Python Style Guide](https://github.com/google/styleguide/blob/gh-pages/pyguide.md))\n- `yapf` (for use with Google open source projects)\n- `facebook`\n\nSee `_STYLE_NAME_TO_FACTORY` in [`style.py`](https://github.com/google/yapf/blob/main/yapf/yapflib/style.py) for details.\n\nIt's also possible to do the same on the command line with a dictionary. For\nexample:\n\n```bash\n--style='{based_on_style: pep8, indent_width: 2}'\n```\n\nThis will take the `pep8` base style and modify it to have two space\nindentations.\n\nYAPF will search for the formatting style in the following manner:\n\n1. Specified on the command line\n2. In the `[style]` section of a `.style.yapf` file in either the current\n   directory or one of its parent directories.\n3. In the `[yapf]` section of a `setup.cfg` file in either the current\n   directory or one of its parent directories.\n4. In the `[tool.yapf]` section of a `pyproject.toml` file in either the current\n   directory or one of its parent directories.\n5. In the `[style]` section of a `~/.config/yapf/style` file in your home\n   directory.\n\nIf none of those files are found, the default style PEP8 is used.\n\n\nExample\n=======\n\nAn example of the type of formatting that YAPF can do, it will take this ugly\ncode:\n\n```python\nx = {  'a':37,'b':42,\n\n'c':927}\n\ny = 'hello ''world'\nz = 'hello '+'world'\na = 'hello {}'.format('world')\nclass foo  (     object  ):\n  def f    (self   ):\n    return       37*-+2\n  def g(self, x,y=42):\n      return y\ndef f  (   a ) :\n  return      37+-+a[42-x :  y**3]\n```\n\nand reformat it into:\n\n```python\nx = {'a': 37, 'b': 42, 'c': 927}\n\ny = 'hello ' 'world'\nz = 'hello ' + 'world'\na = 'hello {}'.format('world')\n\n\nclass foo(object):\n    def f(self):\n        return 37 * -+2\n\n    def g(self, x, y=42):\n        return y\n\n\ndef f(a):\n    return 37 + -+a[42 - x:y**3]\n```\n\n\n## Example as a module\n\nThe two main APIs for calling YAPF are `FormatCode` and `FormatFile`, these\nshare several arguments which are described below:\n\n```python\n>>> from yapf.yapflib.yapf_api import FormatCode  # reformat a string of code\n\n>>> formatted_code, changed = FormatCode(\"f ( a = 1, b = 2 )\")\n>>> formatted_code\n'f(a=1, b=2)\\n'\n>>> changed\nTrue\n```\n\nA `style_config` argument: Either a style name or a path to a file that\ncontains formatting style settings. If None is specified, use the default style\nas set in `style.DEFAULT_STYLE_FACTORY`.\n\n```python\n>>> FormatCode(\"def g():\\n  return True\", style_config='pep8')[0]\n'def g():\\n    return True\\n'\n```\n\nA `lines` argument: A list of tuples of lines (ints), [start, end], that we\nwant to format. The lines are 1-based indexed. It can be used by third-party\ncode (e.g., IDEs) when reformatting a snippet of code rather than a whole file.\n\n```python\n>>> FormatCode(\"def g( ):\\n    a=1\\n    b = 2\\n    return a==b\", lines=[(1, 1), (2, 3)])[0]\n'def g():\\n    a = 1\\n    b = 2\\n    return a==b\\n'\n```\n\nA `print_diff` (bool): Instead of returning the reformatted source, return a\ndiff that turns the formatted source into reformatted source.\n\n```diff\n>>> print(FormatCode(\"a==b\", filename=\"foo.py\", print_diff=True)[0])\n--- foo.py (original)\n+++ foo.py (reformatted)\n@@ -1 +1 @@\n-a==b\n+a == b\n```\n\nNote: the `filename` argument for `FormatCode` is what is inserted into the\ndiff, the default is `<unknown>`.\n\n`FormatFile` returns reformatted code from the passed file along with its encoding:\n\n```python\n>>> from yapf.yapflib.yapf_api import FormatFile  # reformat a file\n\n>>> print(open(\"foo.py\").read())  # contents of file\na==b\n\n>>> reformatted_code, encoding, changed = FormatFile(\"foo.py\")\n>>> formatted_code\n'a == b\\n'\n>>> encoding\n'utf-8'\n>>> changed\nTrue\n```\n\nThe `in_place` argument saves the reformatted code back to the file:\n\n```python\n>>> FormatFile(\"foo.py\", in_place=True)[:2]\n(None, 'utf-8')\n\n>>> print(open(\"foo.py\").read())  # contents of file (now fixed)\na == b\n```\n\n\n## Formatting diffs\n\nOptions:\n\n```console\nusage: yapf-diff [-h] [-i] [-p NUM] [--regex PATTERN] [--iregex PATTERN][-v]\n                 [--style STYLE] [--binary BINARY]\n\nThis script reads input from a unified diff and reformats all the changed\nlines. This is useful to reformat all the lines touched by a specific patch.\nExample usage for git/svn users:\n\n  git diff -U0 --no-color --relative HEAD^ | yapf-diff -i\n  svn diff --diff-cmd=diff -x-U0 | yapf-diff -p0 -i\n\nIt should be noted that the filename contained in the diff is used\nunmodified to determine the source file to update. Users calling this script\ndirectly should be careful to ensure that the path in the diff is correct\nrelative to the current working directory.\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -i, --in-place        apply edits to files instead of displaying a diff\n  -p NUM, --prefix NUM  strip the smallest prefix containing P slashes\n  --regex PATTERN       custom pattern selecting file paths to reformat\n                        (case sensitive, overrides -iregex)\n  --iregex PATTERN      custom pattern selecting file paths to reformat\n                        (case insensitive, overridden by -regex)\n  -v, --verbose         be more verbose, ineffective without -i\n  --style STYLE         specify formatting style: either a style name (for\n                        example \"pep8\" or \"google\"), or the name of a file\n                        with style settings. The default is pep8 unless a\n                        .style.yapf or setup.cfg or pyproject.toml file\n                        located in the same directory as the source or one of\n                        its parent directories (for stdin, the current\n                        directory is used).\n  --binary BINARY       location of binary to use for YAPF\n```\n\n## Python features not yet supported\n* Python 3.12 – [PEP 695 – Type Parameter Syntax](https://peps.python.org/pep-0695/) – [YAPF #1170](https://github.com/google/yapf/issues/1170)\n* Python 3.12 – [PEP 701 – Syntactic formalization of f-strings](https://peps.python.org/pep-0701/) – [YAPF #1136](https://github.com/google/yapf/issues/1136)\n\n## Knobs\n\n#### `ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT`\n\n>    Align closing bracket with visual indentation.\n\n#### `ALLOW_MULTILINE_LAMBDAS`\n\n>    Allow lambdas to be formatted on more than one line.\n\n#### `ALLOW_MULTILINE_DICTIONARY_KEYS`\n\n>    Allow dictionary keys to exist on multiple lines. For example:\n\n```python\n    x = {\n        ('this is the first element of a tuple',\n         'this is the second element of a tuple'):\n             value,\n    }\n```\n\n#### `ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS`\n\n>    Allow splitting before a default / named assignment in an argument list.\n\n#### `ALLOW_SPLIT_BEFORE_DICT_VALUE`\n\n>    Allow splits before the dictionary value.\n\n#### `ARITHMETIC_PRECEDENCE_INDICATION`\n\n>    Let spacing indicate operator precedence. For example:\n\n```python\n    a = 1 * 2 + 3 / 4\n    b = 1 / 2 - 3 * 4\n    c = (1 + 2) * (3 - 4)\n    d = (1 - 2) / (3 + 4)\n    e = 1 * 2 - 3\n    f = 1 + 2 + 3 + 4\n```\n\n>    will be formatted as follows to indicate precedence:\n\n```python\n    a = 1*2 + 3/4\n    b = 1/2 - 3*4\n    c = (1+2) * (3-4)\n    d = (1-2) / (3+4)\n    e = 1*2 - 3\n    f = 1 + 2 + 3 + 4\n```\n\n#### `BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION`\n\n>    Sets the number of desired blank lines surrounding top-level function and\n>    class definitions. For example:\n\n```python\n    class Foo:\n        pass\n                       # <------ having two blank lines here\n                       # <------ is the default setting\n    class Bar:\n        pass\n```\n\n#### `BLANK_LINE_BEFORE_CLASS_DOCSTRING`\n\n>    Insert a blank line before a class-level docstring.\n\n#### `BLANK_LINE_BEFORE_MODULE_DOCSTRING`\n\n>    Insert a blank line before a module docstring.\n\n#### `BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF`\n\n>    Insert a blank line before a `def` or `class` immediately nested within\n>    another `def` or `class`. For example:\n\n```python\n    class Foo:\n                       # <------ this blank line\n        def method():\n            pass\n```\n\n#### `BLANK_LINES_BETWEEN_TOP_LEVEL_IMPORTS_AND_VARIABLES`\n\n>    Sets the number of desired blank lines between top-level imports and\n>    variable definitions. Useful for compatibility with tools like isort.\n\n#### `COALESCE_BRACKETS`\n\n>    Do not split consecutive brackets. Only relevant when\n>    `DEDENT_CLOSING_BRACKETS` or `INDENT_CLOSING_BRACKETS` is set. For example:\n\n```python\n    call_func_that_takes_a_dict(\n        {\n            'key1': 'value1',\n            'key2': 'value2',\n        }\n    )\n```\n\n>    would reformat to:\n\n```python\n    call_func_that_takes_a_dict({\n        'key1': 'value1',\n        'key2': 'value2',\n    })\n```\n\n#### `COLUMN_LIMIT`\n\n>    The column limit (or max line-length)\n\n#### `CONTINUATION_ALIGN_STYLE`\n\n>    The style for continuation alignment. Possible values are:\n\n>    - `SPACE`: Use spaces for continuation alignment. This is default\n>      behavior.\n>    - `FIXED`: Use fixed number (`CONTINUATION_INDENT_WIDTH`) of columns\n>      (i.e. `CONTINUATION_INDENT_WIDTH`/`INDENT_WIDTH` tabs or\n>      `CONTINUATION_INDENT_WIDTH` spaces) for continuation alignment.\n>    - `VALIGN-RIGHT`: Vertically align continuation lines to multiple of\n>      `INDENT_WIDTH` columns. Slightly right (one tab or a few spaces) if cannot\n>      vertically align continuation lines with indent characters.\n\n#### `CONTINUATION_INDENT_WIDTH`\n\n>    Indent width used for line continuations.\n\n#### `DEDENT_CLOSING_BRACKETS`\n\n>    Put closing brackets on a separate line, dedented, if the bracketed\n>    expression can't fit in a single line. Applies to all kinds of brackets,\n>    including function definitions and calls. For example:\n\n```python\n    config = {\n        'key1': 'value1',\n        'key2': 'value2',\n    }  # <--- this bracket is dedented and on a separate line\n\n    time_series = self.remote_client.query_entity_counters(\n        entity='dev3246.region1',\n        key='dns.query_latency_tcp',\n        transform=Transformation.AVERAGE(window=timedelta(seconds=60)),\n        start_ts=now()-timedelta(days=3),\n        end_ts=now(),\n    )  # <--- this bracket is dedented and on a separate line\n```\n\n#### `DISABLE_ENDING_COMMA_HEURISTIC`\n\n>    Disable the heuristic which places each list element on a separate line if\n>    the list is comma-terminated.\n>\n>    Note: The behavior of this flag changed in v0.40.3.  Before, if this flag\n>    was true, we would split lists that contained a trailing comma or a\n>    comment.  Now, we have a separate flag, `DISABLE_SPLIT_LIST_WITH_COMMENT`,\n>    that controls splitting when a list contains a comment.  To get the old\n>    behavior, set both flags to true.  More information in\n>    [CHANGELOG.md](CHANGELOG.md#new-disable_split_list_with_comment-flag).\n\n#### `DISABLE_SPLIT_LIST_WITH_COMMENT`\n\n>    Don't put every element on a new line within a list that contains\n>    interstitial comments.\n>\n>    Without this flag (default):\n>\n>    ```\n>    [\n>      a,\n>      b,  #\n>      c\n>    ]\n>    ```\n>\n>    With this flag:\n>\n>    ```\n>    [\n>      a, b,  #\n>      c\n>    ]\n>    ```\n>\n>    This mirrors the behavior of clang-format and is useful for forming\n>    \"logical groups\" of elements in a list.  It also works in function\n>    declarations.\n\n#### `EACH_DICT_ENTRY_ON_SEPARATE_LINE`\n\n>    Place each dictionary entry onto its own line.\n\n#### `FORCE_MULTILINE_DICT`\n\n>    Respect `EACH_DICT_ENTRY_ON_SEPARATE_LINE` even if the line is shorter than\n>    `COLUMN_LIMIT`.\n\n#### `I18N_COMMENT`\n\n>    The regex for an internationalization comment. The presence of this comment\n>    stops reformatting of that line, because the comments are required to be\n>    next to the string they translate.\n\n#### `I18N_FUNCTION_CALL`\n\n>    The internationalization function call names. The presence of this function\n>    stops reformatting on that line, because the string it has cannot be moved\n>    away from the i18n comment.\n\n#### `INDENT_BLANK_LINES`\n\n>    Set to `True` to prefer indented blank lines rather than empty\n\n#### `INDENT_CLOSING_BRACKETS`\n\n>    Put closing brackets on a separate line, indented, if the bracketed\n>    expression can't fit in a single line. Applies to all kinds of brackets,\n>    including function definitions and calls. For example:\n\n```python\n    config = {\n        'key1': 'value1',\n        'key2': 'value2',\n        }  # <--- this bracket is indented and on a separate line\n\n    time_series = self.remote_client.query_entity_counters(\n        entity='dev3246.region1',\n        key='dns.query_latency_tcp',\n        transform=Transformation.AVERAGE(window=timedelta(seconds=60)),\n        start_ts=now()-timedelta(days=3),\n        end_ts=now(),\n        )  # <--- this bracket is indented and on a separate line\n```\n\n#### `INDENT_DICTIONARY_VALUE`\n\n>    Indent the dictionary value if it cannot fit on the same line as the\n>    dictionary key. For example:\n\n```python\n    config = {\n        'key1':\n            'value1',\n        'key2': value1 +\n                value2,\n    }\n```\n\n#### `INDENT_WIDTH`\n\n>    The number of columns to use for indentation.\n\n#### `JOIN_MULTIPLE_LINES`\n\n>    Join short lines into one line. E.g., single line `if` statements.\n\n#### `NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS`\n\n>    Do not include spaces around selected binary operators. For example:\n\n```python\n    1 + 2 * 3 - 4 / 5\n```\n\n>    will be formatted as follows when configured with `*`, `/`:\n\n```python\n    1 + 2*3 - 4/5\n```\n\n#### `SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET`\n\n>    Insert a space between the ending comma and closing bracket of a list, etc.\n\n#### `SPACE_INSIDE_BRACKETS`\n\n    Use spaces inside brackets, braces, and parentheses.  For example:\n\n```python\n        method_call( 1 )\n        my_dict[ 3 ][ 1 ][ get_index( *args, **kwargs ) ]\n        my_set = { 1, 2, 3 }\n```\n\n#### `SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN`\n\n>    Set to `True` to prefer spaces around the assignment operator for default\n>    or keyword arguments.\n\n#### `SPACES_AROUND_DICT_DELIMITERS`\n\n>    Adds a space after the opening '{' and before the ending '}' dict delimiters.\n\n```python\n        {1: 2}\n```\n\n>    will be formatted as:\n\n```python\n        { 1: 2 }\n```\n\n#### `SPACES_AROUND_LIST_DELIMITERS`\n\n>    Adds a space after the opening '[' and before the ending ']' list delimiters.\n\n```python\n    [1, 2]\n```\n\n>    will be formatted as:\n\n```python\n    [ 1, 2 ]\n```\n\n#### `SPACES_AROUND_POWER_OPERATOR`\n\n>    Set to `True` to prefer using spaces around `**`.\n\n#### `SPACES_AROUND_SUBSCRIPT_COLON`\n\n>    Use spaces around the subscript / slice operator.  For example:\n\n```python\n    my_list[1 : 10 : 2]\n```\n\n##### `SPACES_AROUND_TUPLE_DELIMITERS`\n\n>    Adds a space after the opening '(' and before the ending ')' tuple delimiters.\n\n```python\n    (1, 2, 3)\n```\n\n>    will be formatted as:\n\n```python\n    ( 1, 2, 3 )\n```\n\n#### `SPACES_BEFORE_COMMENT`\n\n>    The number of spaces required before a trailing comment.\n>    This can be a single value (representing the number of spaces\n>    before each trailing comment) or list of values (representing\n>    alignment column values; trailing comments within a block will\n>    be aligned to the first column value that is greater than the maximum\n>    line length within the block).\n\n> **Note:** Lists of values may need to be quoted in some contexts\n> (eg. shells or editor config files).\n\n>    For example, with `spaces_before_comment=5`:\n\n```python\n    1 + 1 # Adding values\n```\n\n>    will be formatted as:\n\n```python\n    1 + 1     # Adding values <-- 5 spaces between the end of the statement and comment\n```\n\n>    with `spaces_before_comment=\"15, 20\"`:\n\n```python\n    1 + 1 # Adding values\n    two + two # More adding\n\n    longer_statement # This is a longer statement\n    short # This is a shorter statement\n\n    a_very_long_statement_that_extends_beyond_the_final_column # Comment\n    short # This is a shorter statement\n```\n\n>    will be formatted as:\n\n```python\n    1 + 1          # Adding values <-- end of line comments in block aligned to col 15\n    two + two      # More adding\n\n    longer_statement    # This is a longer statement <-- end of line comments in block aligned to col 20\n    short               # This is a shorter statement\n\n    a_very_long_statement_that_extends_beyond_the_final_column  # Comment <-- the end of line comments are aligned based on the line length\n    short                                                       # This is a shorter statement\n```\n\n#### `SPLIT_ALL_COMMA_SEPARATED_VALUES`\n\n>    If a comma separated list (`dict`, `list`, `tuple`, or function `def`) is\n>    on a line that is too long, split such that each element is on a separate\n>    line.\n\n#### `SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES`\n\n>    Variation on `SPLIT_ALL_COMMA_SEPARATED_VALUES` in which, if a\n>    subexpression with a comma fits in its starting line, then the\n>    subexpression is not split. This avoids splits like the one for\n>    `b` in this code:\n\n```python\n    abcdef(\n        aReallyLongThing: int,\n        b: [Int,\n            Int])\n```\n\n>    with the new knob this is split as:\n\n```python\n    abcdef(\n        aReallyLongThing: int,\n        b: [Int, Int])\n```\n\n#### `SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED`\n\n>    Split before arguments if the argument list is terminated by a comma.\n\n#### `SPLIT_BEFORE_ARITHMETIC_OPERATOR`\n\n>    Set to `True` to prefer splitting before `+`, `-`, `*`, `/`, `//`, or `@`\n>    rather than after.\n\n#### `SPLIT_BEFORE_BITWISE_OPERATOR`\n\n>    Set to `True` to prefer splitting before `&`, `|` or `^` rather than after.\n\n#### `SPLIT_BEFORE_CLOSING_BRACKET`\n\n>    Split before the closing bracket if a `list` or `dict` literal doesn't fit\n>    on a single line.\n\n#### `SPLIT_BEFORE_DICT_SET_GENERATOR`\n\n>    Split before a dictionary or set generator (`comp_for`). For example, note\n>    the split before the `for`:\n\n```python\n    foo = {\n        variable: 'Hello world, have a nice day!'\n        for variable in bar if variable != 42\n    }\n```\n\n#### `SPLIT_BEFORE_DOT`\n\n>    Split before the `.` if we need to split a longer expression:\n\n```python\n    foo = ('This is a really long string: {}, {}, {}, {}'.format(a, b, c, d))\n```\n\n>    would reformat to something like:\n\n```python\n    foo = ('This is a really long string: {}, {}, {}, {}'\n           .format(a, b, c, d))\n```\n\n#### `SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN`\n\n>    Split after the opening paren which surrounds an expression if it doesn't\n>    fit on a single line.\n\n#### `SPLIT_BEFORE_FIRST_ARGUMENT`\n\n>    If an argument / parameter list is going to be split, then split before the\n>    first argument.\n\n#### `SPLIT_BEFORE_LOGICAL_OPERATOR`\n\n>    Set to `True` to prefer splitting before `and` or `or` rather than after.\n\n#### `SPLIT_BEFORE_NAMED_ASSIGNS`\n\n>    Split named assignments onto individual lines.\n\n#### `SPLIT_COMPLEX_COMPREHENSION`\n\n>    For list comprehensions and generator expressions with multiple clauses\n>    (e.g multiple `for` calls, `if` filter expressions) and which need to be\n>    reflowed, split each clause onto its own line. For example:\n\n```python\n    result = [\n        a_var + b_var for a_var in xrange(1000) for b_var in xrange(1000)\n        if a_var % b_var]\n```\n\n>    would reformat to something like:\n\n```python\n    result = [\n        a_var + b_var\n        for a_var in xrange(1000)\n        for b_var in xrange(1000)\n        if a_var % b_var]\n```\n\n#### `SPLIT_PENALTY_AFTER_OPENING_BRACKET`\n\n>    The penalty for splitting right after the opening bracket.\n\n#### `SPLIT_PENALTY_AFTER_UNARY_OPERATOR`\n\n>    The penalty for splitting the line after a unary operator.\n\n#### `SPLIT_PENALTY_ARITHMETIC_OPERATOR`\n\n>    The penalty of splitting the line around the `+`, `-`, `*`, `/`, `//`, `%`,\n>    and `@` operators.\n\n#### `SPLIT_PENALTY_BEFORE_IF_EXPR`\n\n>    The penalty for splitting right before an `if` expression.\n\n#### `SPLIT_PENALTY_BITWISE_OPERATOR`\n\n>    The penalty of splitting the line around the `&`, `|`, and `^` operators.\n\n#### `SPLIT_PENALTY_COMPREHENSION`\n\n>    The penalty for splitting a list comprehension or generator expression.\n\n#### `SPLIT_PENALTY_EXCESS_CHARACTER`\n\n>    The penalty for characters over the column limit.\n\n#### `SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT`\n\n>    The penalty incurred by adding a line split to the logical line. The more\n>    line splits added the higher the penalty.\n\n#### `SPLIT_PENALTY_IMPORT_NAMES`\n\n>    The penalty of splitting a list of `import as` names. For example:\n\n```python\n    from a_very_long_or_indented_module_name_yada_yad import (long_argument_1,\n                                                              long_argument_2,\n                                                              long_argument_3)\n```\n\n>    would reformat to something like:\n\n```python\n    from a_very_long_or_indented_module_name_yada_yad import (\n        long_argument_1, long_argument_2, long_argument_3)\n```\n\n#### `SPLIT_PENALTY_LOGICAL_OPERATOR`\n\n>    The penalty of splitting the line around the `and` and `or` operators.\n\n#### `USE_TABS`\n\n>    Use the Tab character for indentation.\n\n\n## (Potentially) Frequently Asked Questions\n\n### Why does YAPF destroy my awesome formatting?\n\nYAPF tries very hard to get the formatting correct. But for some code, it won't\nbe as good as hand-formatting. In particular, large data literals may become\nhorribly disfigured under YAPF.\n\nThe reasons for this are manyfold. In short, YAPF is simply a tool to help\nwith development. It will format things to coincide with the style guide, but\nthat may not equate with readability.\n\nWhat can be done to alleviate this situation is to indicate regions YAPF should\nignore when reformatting something:\n\n```python\n# yapf: disable\nFOO = {\n    # ... some very large, complex data literal.\n}\n\nBAR = [\n    # ... another large data literal.\n]\n# yapf: enable\n```\n\nYou can also disable formatting for a single literal like this:\n\n```python\nBAZ = {\n    (1, 2, 3, 4),\n    (5, 6, 7, 8),\n    (9, 10, 11, 12),\n}  # yapf: disable\n```\n\nTo preserve the nice dedented closing brackets, use the\n`dedent_closing_brackets` in your style. Note that in this case all\nbrackets, including function definitions and calls, are going to use\nthat style.  This provides consistency across the formatted codebase.\n\n### Why Not Improve Existing Tools?\n\nWe wanted to use clang-format's reformatting algorithm. It's very powerful and\ndesigned to come up with the best formatting possible. Existing tools were\ncreated with different goals in mind, and would require extensive modifications\nto convert to using clang-format's algorithm.\n\n### Can I Use YAPF In My Program?\n\nPlease do! YAPF was designed to be used as a library as well as a command line\ntool. This means that a tool or IDE plugin is free to use YAPF.\n\n### I still get non-PEP8 compliant code! Why?\n\nYAPF tries very hard to be fully PEP 8 compliant. However, it is paramount\nto not risk altering the semantics of your code. Thus, YAPF tries to be as\nsafe as possible and does not change the token stream\n(e.g., by adding parentheses).\nAll these cases however, can be easily fixed manually. For instance,\n\n```python\nfrom my_package import my_function_1, my_function_2, my_function_3, my_function_4, my_function_5\n\nFOO = my_variable_1 + my_variable_2 + my_variable_3 + my_variable_4 + my_variable_5 + my_variable_6 + my_variable_7 + my_variable_8\n```\n\nwon't be split, but you can easily get it right by just adding parentheses:\n\n```python\nfrom my_package import (my_function_1, my_function_2, my_function_3,\n                        my_function_4, my_function_5)\n\nFOO = (my_variable_1 + my_variable_2 + my_variable_3 + my_variable_4 +\n       my_variable_5 + my_variable_6 + my_variable_7 + my_variable_8)\n```\n\n\n## Gory Details\n\n### Algorithm Design\n\nThe main data structure in YAPF is the `LogicalLine` object. It holds a list\nof `FormatToken`\\s, that we would want to place on a single line if there\nwere no column limit. An exception being a comment in the middle of an\nexpression statement will force the line to be formatted on more than one line.\nThe formatter works on one `LogicalLine` object at a time.\n\nAn `LogicalLine` typically won't affect the formatting of lines before or\nafter it. There is a part of the algorithm that may join two or more\n`LogicalLine`\\s into one line. For instance, an if-then statement with a\nshort body can be placed on a single line:\n\n```python\nif a == 42: continue\n```\n\nYAPF's formatting algorithm creates a weighted tree that acts as the solution\nspace for the algorithm. Each node in the tree represents the result of a\nformatting decision --- i.e., whether to split or not to split before a token.\nEach formatting decision has a cost associated with it. Therefore, the cost is\nrealized on the edge between two nodes. (In reality, the weighted tree doesn't\nhave separate edge objects, so the cost resides on the nodes themselves.)\n\nFor example, take the following Python code snippet. For the sake of this\nexample, assume that line (1) violates the column limit restriction and needs to\nbe reformatted.\n\n```python\ndef xxxxxxxxxxx(aaaaaaaaaaaa, bbbbbbbbb, cccccccc, dddddddd, eeeeee):  # 1\n    pass                                                               # 2\n```\n\nFor line (1), the algorithm will build a tree where each node (a\n`FormattingDecisionState` object) is the state of the line at that token given\nthe decision to split before the token or not. Note: the `FormatDecisionState`\nobjects are copied by value so each node in the graph is unique and a change in\none doesn't affect other nodes.\n\nHeuristics are used to determine the costs of splitting or not splitting.\nBecause a node holds the state of the tree up to a token's insertion, it can\neasily determine if a splitting decision will violate one of the style\nrequirements. For instance, the heuristic is able to apply an extra penalty to\nthe edge when not splitting between the previous token and the one being added.\n\nThere are some instances where we will never want to split the line, because\ndoing so will always be detrimental (i.e., it will require a backslash-newline,\nwhich is very rarely desirable). For line (1), we will never want to split the\nfirst three tokens: `def`, `xxxxxxxxxxx`, and `(`. Nor will we want to\nsplit between the `)` and the `:` at the end. These regions are said to be\n\"unbreakable.\" This is reflected in the tree by there not being a \"split\"\ndecision (left hand branch) within the unbreakable region.\n\nNow that we have the tree, we determine what the \"best\" formatting is by finding\nthe path through the tree with the lowest cost.\n\nAnd that's it!\n"
  },
  {
    "path": "plugins/README.md",
    "content": "# IDE Plugins\n\n## Emacs\n\nThe `Emacs` plugin is maintained separately. Installation directions can be\nfound here: https://github.com/paetzke/py-yapf.el\n\n\n## Vim\n\nThe `vim` plugin allows you to reformat a range of code. Copy `plugin` and\n`autoload` directories into your `~/.vim` or use `:packadd` in Vim 8. Or use\na plugin manager like Plug or Vundle:\n\n```vim\n\" Plug\nPlug 'google/yapf', { 'rtp': 'plugins/vim', 'for': 'python' }\n\n\" Vundle\nPlugin 'google/yapf', { 'rtp': 'plugins/vim' }\n```\n\nYou can add key bindings in the `.vimrc` file:\n\n```vim\nmap <C-Y> :call yapf#YAPF()<cr>\nimap <C-Y> <c-o>:call yapf#YAPF()<cr>\n```\n\nAlternatively, you can call the command `YAPF`. If you omit the range, it will\nreformat the whole buffer.\n\nexample:\n\n```vim\n:YAPF       \" formats whole buffer\n:'<,'>YAPF  \" formats lines selected in visual mode\n```\n\n\n## Sublime Text\n\nThe `Sublime Text` plugin is also maintained separately. It is compatible with\nboth Sublime Text 2 and 3.\n\nThe plugin can be easily installed by using *Sublime Package Control*. Check\nthe project page of the plugin for more information: https://github.com/jason-kane/PyYapf\n\n\n## git Pre-Commit Hook\n\nThe `git` pre-commit hook automatically formats your Python files before they\nare committed to your local repository. Any changes `yapf` makes to the files\nwill stay unstaged so that you can diff them manually.\n\nTo install, simply download the raw file and copy it into your git hooks\ndirectory:\n\n```bash\n# From the root of your git project.\n$ curl -o pre-commit.sh https://raw.githubusercontent.com/google/yapf/main/plugins/pre-commit.sh\n$ chmod a+x pre-commit.sh\n$ mv pre-commit.sh .git/hooks/pre-commit\n```\n\n\n## Textmate 2\n\nPlugin for `Textmate 2` requires `yapf` Python package installed on your\nsystem:\n\n```bash\n$ pip install yapf\n```\n\nAlso, you will need to activate `Python` bundle from `Preferences > Bundles`.\n\nFinally, create a `~/Library/Application Support/TextMate/Bundles/Python.tmbundle/Commands/YAPF.tmCommand`\nfile with the following content:\n\n```xml\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n  <key>beforeRunningCommand</key>\n  <string>saveActiveFile</string>\n  <key>command</key>\n  <string>#!/bin/bash\n\nTPY=${TM_PYTHON:-python}\n\n\"$TPY\" \"/usr/local/bin/yapf\" \"$TM_FILEPATH\"</string>\n  <key>input</key>\n  <string>document</string>\n  <key>name</key>\n  <string>YAPF</string>\n  <key>scope</key>\n  <string>source.python</string>\n  <key>uuid</key>\n  <string>297D5A82-2616-4950-9905-BD2D1C94D2D4</string>\n</dict>\n</plist>\n```\n\nYou will see a new menu item `Bundles > Python > YAPF`.\n"
  },
  {
    "path": "plugins/pre-commit.sh",
    "content": "#!/usr/bin/env bash\n\n# Git pre-commit hook to check staged Python files for formatting issues with\n# yapf.\n#\n# INSTALLING: Copy this script into `.git/hooks/pre-commit`, and mark it as\n# executable.\n#\n# This requires that yapf is installed and runnable in the environment running\n# the pre-commit hook.\n#\n# When running, this first checks for unstaged changes to staged files, and if\n# there are any, it will exit with an error. Files with unstaged changes will be\n# printed.\n#\n# If all staged files have no unstaged changes, it will run yapf against them,\n# leaving the formatting changes unstaged. Changed files will be printed.\n#\n# BUGS: This does not leave staged changes alone when used with the -a flag to\n# git commit, due to the fact that git stages ALL unstaged files when that flag\n# is used.\n\n# Find all staged Python files, and exit early if there aren't any.\nPYTHON_FILES=()\nwhile IFS=$'\\n' read -r line; do PYTHON_FILES+=(\"$line\"); done \\\n  < <(git diff --name-only --cached --diff-filter=AM | grep --color=never '.py$')\nif [ ${#PYTHON_FILES[@]} -eq 0 ]; then\n  exit 0\nfi\n\n########## PIP VERSION #############\n# Verify that yapf is installed; if not, warn and exit.\nif ! command -v yapf >/dev/null; then\n  echo 'yapf not on path; can not format. Please install yapf:'\n  echo '    pip install yapf'\n  exit 2\nfi\n######### END PIP VERSION ##########\n\n########## PIPENV VERSION ##########\n# if ! pipenv run yapf --version 2>/dev/null 2>&1; then\n#   echo 'yapf not on path; can not format. Please install yapf:'\n#   echo '    pipenv install yapf'\n#   exit 2\n# fi\n###### END PIPENV VERSION ##########\n\n\n# Check for unstaged changes to files in the index.\nCHANGED_FILES=()\nwhile IFS=$'\\n' read -r line; do CHANGED_FILES+=(\"$line\"); done \\\n  < <(git diff --name-only \"${PYTHON_FILES[@]}\")\nif [ ${#CHANGED_FILES[@]} -gt 0 ]; then\n  echo 'You have unstaged changes to some files in your commit; skipping '\n  echo 'auto-format. Please stage, stash, or revert these changes. You may '\n  echo 'find `git stash -k` helpful here.'\n  echo 'Files with unstaged changes:' \"${CHANGED_FILES[@]}\"\n  exit 1\nfi\n\n# Format all staged files, then exit with an error code if any have uncommitted\n# changes.\necho 'Formatting staged Python files . . .'\n\n########## PIP VERSION #############\nyapf -i -r \"${PYTHON_FILES[@]}\"\n######### END PIP VERSION ##########\n\n########## PIPENV VERSION ##########\n# pipenv run yapf -i -r \"${PYTHON_FILES[@]}\"\n###### END PIPENV VERSION ##########\n\n\nCHANGED_FILES=()\nwhile IFS=$'\\n' read -r line; do CHANGED_FILES+=(\"$line\"); done \\\n  < <(git diff --name-only \"${PYTHON_FILES[@]}\")\nif [ ${#CHANGED_FILES[@]} -gt 0 ]; then\n  echo 'Reformatted staged files. Please review and stage the changes.'\n  echo 'Files updated: ' \"${CHANGED_FILES[@]}\"\n  exit 1\nelse\n  exit 0\nfi\n"
  },
  {
    "path": "plugins/vim/autoload/yapf.vim",
    "content": "\" Copyright 2015 Google Inc. All Rights Reserved.\n\"\n\" Licensed under the Apache License, Version 2.0 (the \"License\");\n\" you may not use this file except in compliance with the License.\n\" You may obtain a copy of the License at\n\"\n\"     http://www.apache.org/licenses/LICENSE-2.0\n\"\n\" Unless required by applicable law or agreed to in writing, software\n\" distributed under the License is distributed on an \"AS IS\" BASIS,\n\" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n\" See the License for the specific language governing permissions and\n\" limitations under the License.\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\" VIM Autoload script for YAPF support\n\"\n\" Place this script in your ~/.vim/autoload directory. You can add accessors to\n\" ~/.vimrc, e.g.:\n\"\n\"    map <C-Y> :call yapf#YAPF()<cr>\n\"    imap <C-Y> <c-o>:call yapf#YAPF()<cr>\n\"\nfunction! yapf#YAPF() range\n  \" Determine range to format.\n  let l:line_ranges = a:firstline . '-' . a:lastline\n  let l:cmd = 'yapf --lines=' . l:line_ranges\n\n  \" Call YAPF with the current buffer\n  if exists('*systemlist')\n    let l:formatted_text = systemlist(l:cmd, join(getline(1, '$'), \"\\n\") . \"\\n\")\n  else\n    let l:formatted_text =\n        \\ split(system(l:cmd, join(getline(1, '$'), \"\\n\") . \"\\n\"), \"\\n\")\n  endif\n\n  if v:shell_error\n    echohl ErrorMsg\n    echomsg printf('\"%s\" returned error: %s', l:cmd, l:formatted_text[-1])\n    echohl None\n    return\n  endif\n\n  \" Update the buffer.\n  execute '1,' . string(line('$')) . 'delete'\n  call setline(1, l:formatted_text)\n\n  \" Reset cursor to first line of the formatted range.\n  call cursor(a:firstline, 1)\nendfunction\n"
  },
  {
    "path": "plugins/vim/plugin/yapf.vim",
    "content": "\" Copyright 2015 Google Inc. All Rights Reserved.\n\"\n\" Licensed under the Apache License, Version 2.0 (the \"License\");\n\" you may not use this file except in compliance with the License.\n\" You may obtain a copy of the License at\n\"\n\"     http://www.apache.org/licenses/LICENSE-2.0\n\"\n\" Unless required by applicable law or agreed to in writing, software\n\" distributed under the License is distributed on an \"AS IS\" BASIS,\n\" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n\" See the License for the specific language governing permissions and\n\" limitations under the License.\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\" VIM command for YAPF support\n\"\n\" Place this script in your ~/.vim/plugin directory. You can call the\n\" command YAPF. If you omit the range, it will reformat the whole\n\" buffer.\n\"\n\" example:\n\"   :YAPF       \" formats whole buffer\n\"   :'<,'>YAPF  \" formats lines selected in visual mode\n\ncommand! -range=% YAPF <line1>,<line2>call yapf#YAPF()\n"
  },
  {
    "path": "pylintrc",
    "content": "[MASTER]\n\n# Specify a configuration file.\n#rcfile=\n\n# Python code to execute, usually for sys.path manipulation such as\n# pygtk.require().\n#init-hook=\n\n# Add files or directories to the blacklist. They should be base names, not\n# paths.\nignore=CVS\n\n# Pickle collected data for later comparisons.\npersistent=yes\n\n# List of plugins (as comma separated values of python modules names) to load,\n# usually to register additional checkers.\nload-plugins=\n\n# Use multiple processes to speed up Pylint.\njobs=1\n\n# Allow loading of arbitrary C extensions. Extensions are imported into the\n# active Python interpreter and may run arbitrary code.\nunsafe-load-any-extension=no\n\n# A comma-separated list of package or module names from where C extensions may\n# be loaded. Extensions are loading into the active Python interpreter and may\n# run arbitrary code\nextension-pkg-whitelist=\n\n# Allow optimization of some AST trees. This will activate a peephole AST\n# optimizer, which will apply various small optimizations. For instance, it can\n# be used to obtain the result of joining multiple strings with the addition\n# operator. Joining a lot of strings can lead to a maximum recursion error in\n# Pylint and this flag can prevent that. It has one side effect, the resulting\n# AST will be different than the one from reality.\noptimize-ast=no\n\n\n[MESSAGES CONTROL]\n\n# Only show warnings with the listed confidence levels. Leave empty to show\n# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED\nconfidence=\n\n# Enable the message, report, category or checker with the given id(s). You can\n# either give multiple identifier separated by comma (,) or put this option\n# multiple time (only on the command line, not in the configuration file where\n# it should appear only once). See also the \"--disable\" option for examples.\n#enable=\n\n# Disable the message, report, category or checker with the given id(s). You\n# can either give multiple identifiers separated by comma (,) or put this\n# option multiple times (only on the command line, not in the configuration\n# file where it should appear only once).You can also use \"--disable=all\" to\n# disable everything first and then re-enable specific checks. For example, if\n# you want to run only the similarities checker, you can use \"--disable=all\n# --enable=similarities\". If you want to run only the classes checker, but have\n# no Warning level messages displayed, use\"--disable=all --enable=classes\n# --disable=W\"\ndisable=\n    # disabled by me,\n    locally-disabled,\n    missing-docstring,\n    fixme,\n    # disabled by default,\n    import-star-module-level,\n    old-octal-literal,\n    oct-method,\n    print-statement,\n    unpacking-in-except,\n    parameter-unpacking,\n    backtick,\n    old-raise-syntax,\n    old-ne-operator,\n    long-suffix,\n    dict-view-method,\n    dict-iter-method,\n    metaclass-assignment,\n    next-method-called,\n    raising-string,\n    indexing-exception,\n    raw_input-builtin,\n    long-builtin,\n    file-builtin,\n    execfile-builtin,\n    coerce-builtin,\n    cmp-builtin,\n    buffer-builtin,\n    basestring-builtin,\n    apply-builtin,\n    filter-builtin-not-iterating,\n    using-cmp-argument,\n    useless-suppression,\n    range-builtin-not-iterating,\n    suppressed-message,\n    no-absolute-import,\n    old-division,\n    cmp-method,\n    reload-builtin,\n    zip-builtin-not-iterating,\n    intern-builtin,\n    unichr-builtin,\n    reduce-builtin,\n    standarderror-builtin,\n    unicode-builtin,\n    xrange-builtin,\n    coerce-method,\n    delslice-method,\n    getslice-method,\n    setslice-method,\n    input-builtin,\n    round-builtin,\n    hex-method,\n    nonzero-method,\n    map-builtin-not-iterating,\n\n\n[REPORTS]\n\n# Set the output format. Available formats are text, parseable, colorized, msvs\n# (visual studio) and html. You can also give a reporter class, eg\n# mypackage.mymodule.MyReporterClass.\noutput-format=text\n\n# Put messages in a separate file for each module / package specified on the\n# command line instead of printing them on stdout. Reports (if any) will be\n# written in a file name \"pylint_global.[txt|html]\".\nfiles-output=no\n\n# Tells whether to display a full report or only the messages\nreports=yes\n\n# Python expression which should return a note less than 10 (10 is the highest\n# note). You have access to the variables errors warning, statement which\n# respectively contain the number of errors / warnings messages and the total\n# number of statements analyzed. This is used by the global evaluation report\n# (RP0004).\nevaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)\n\n# Template used to display messages. This is a python new-style format string\n# used to format the message information. See doc for all details\n#msg-template=\n\n\n[FORMAT]\n\n# Maximum number of characters on a single line.\nmax-line-length=100\n\n# Regexp for a line that is allowed to be longer than the limit.\nignore-long-lines=^\\s*(# )?<?https?://\\S+>?$\n\n# Allow the body of an if to be on the same line as the test if there is no\n# else.\nsingle-line-if-stmt=no\n\n# List of optional constructs for which whitespace checking is disabled. `dict-\n# separator` is used to allow tabulation in dicts, etc.: {1  : 1,\\n222: 2}.\n# `trailing-comma` allows a space between comma and closing bracket: (a, ).\n# `empty-line` allows space-only lines.\nno-space-check=trailing-comma,dict-separator\n\n# Maximum number of lines in a module\nmax-module-lines=1000\n\n# String used as indentation unit. This is usually \"    \" (4 spaces) or \"\\t\" (1\n# tab).\nindent-string='  '\n\n# Number of spaces of indent required inside a hanging  or continued line.\nindent-after-paren=4\n\n# Expected format of line ending, e.g. empty (any line ending), LF or CRLF.\nexpected-line-ending-format=\n\n\n[SPELLING]\n\n# Spelling dictionary name. Available dictionaries: none. To make it working\n# install python-enchant package.\nspelling-dict=\n\n# List of comma separated words that should not be checked.\nspelling-ignore-words=\n\n# A path to a file that contains private dictionary; one word per line.\nspelling-private-dict-file=\n\n# Tells whether to store unknown words to indicated private dictionary in\n# --spelling-private-dict-file option instead of raising a message.\nspelling-store-unknown-words=no\n\n\n[LOGGING]\n\n# Logging modules to check that the string format arguments are in logging\n# function parameter format\nlogging-modules=logging\n\n\n[BASIC]\n\n# List of builtins function names that should not be used, separated by a comma\nbad-functions=map,filter,input\n\n# Good variable names which should always be accepted, separated by a comma\ngood-names=i,e,s,_,fd,fp\n\n# Bad variable names which should always be refused, separated by a comma\nbad-names=foo,bar,baz,toto,tutu,tata\n\n# Colon-delimited sets of names that determine each other's naming style when\n# the name regexes allow several styles.\nname-group=\n\n# Include a hint for the correct naming format with invalid-name\ninclude-naming-hint=no\n\n# Regular expression matching correct function names\n# original:\n#function-rgx=[a-z_][a-z0-9_]{2,30}$\nfunction-rgx=[a-zA-Z_][a-zA-Z0-9_]{2,40}$\n\n# Naming hint for function names\nfunction-name-hint=[a-z_][a-z0-9_]{2,30}$\n\n# Regular expression matching correct variable names\nvariable-rgx=[a-z_][a-z0-9_]{2,30}$\n\n# Naming hint for variable names\nvariable-name-hint=[a-z_][a-z0-9_]{2,30}$\n\n# Regular expression matching correct constant names\n# original:\n#const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$\nconst-rgx=(([a-zA-Z_][a-zA-Z0-9_]*)|(__.*__))$\n\n# Naming hint for constant names\nconst-name-hint=(([A-Z_][A-Z0-9_]*)|(__.*__))$\n\n# Regular expression matching correct attribute names\nattr-rgx=[a-z_][a-z0-9_]{2,30}$\n\n# Naming hint for attribute names\nattr-name-hint=[a-z_][a-z0-9_]{2,30}$\n\n# Regular expression matching correct argument names\nargument-rgx=[a-z_][a-z0-9_]{2,30}$\n\n# Naming hint for argument names\nargument-name-hint=[a-z_][a-z0-9_]{2,30}$\n\n# Regular expression matching correct class attribute names\n# original:\n#class-attribute-rgx=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$\nclass-attribute-rgx=([A-Za-z_][A-Za-z0-9_]{2,40}|(__.*__))$\n\n# Naming hint for class attribute names\nclass-attribute-name-hint=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$\n\n# Regular expression matching correct inline iteration names\ninlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$\n\n# Naming hint for inline iteration names\ninlinevar-name-hint=[A-Za-z_][A-Za-z0-9_]*$\n\n# Regular expression matching correct class names\n# original:\n#class-rgx=[A-Z_][a-zA-Z0-9]+$\nclass-rgx=[a-zA-Z_][a-zA-Z0-9]+$\n\n# Naming hint for class names\nclass-name-hint=[A-Z_][a-zA-Z0-9]+$\n\n# Regular expression matching correct module names\nmodule-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$\n\n# Naming hint for module names\nmodule-name-hint=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$\n\n# Regular expression matching correct method names\n# original:\n#method-rgx=[a-z_][a-z0-9_]{2,30}$\nmethod-rgx=[a-zA-Z_][a-zA-Z0-9_]{2,40}$\n\n# Naming hint for method names\nmethod-name-hint=[a-z_][a-z0-9_]{2,30}$\n\n# Regular expression which should only match function or class names that do\n# not require a docstring.\nno-docstring-rgx=^_\n\n# Minimum line length for functions/classes that require docstrings, shorter\n# ones are exempt.\ndocstring-min-length=-1\n\n\n[ELIF]\n\n# Maximum number of nested blocks for function / method body\nmax-nested-blocks=5\n\n\n[SIMILARITIES]\n\n# Minimum lines number of a similarity.\nmin-similarity-lines=4\n\n# Ignore comments when computing similarities.\nignore-comments=yes\n\n# Ignore docstrings when computing similarities.\nignore-docstrings=yes\n\n# Ignore imports when computing similarities.\nignore-imports=no\n\n\n[TYPECHECK]\n\n# Tells whether missing members accessed in mixin class should be ignored. A\n# mixin class is detected if its name ends with \"mixin\" (case insensitive).\nignore-mixin-members=yes\n\n# List of module names for which member attributes should not be checked\n# (useful for modules/projects where namespaces are manipulated during runtime\n# and thus existing member attributes cannot be deduced by static analysis. It\n# supports qualified module names, as well as Unix pattern matching.\nignored-modules=\n\n# List of classes names for which member attributes should not be checked\n# (useful for classes with attributes dynamically set). This supports can work\n# with qualified names.\nignored-classes=\n\n# List of members which are set dynamically and missed by pylint inference\n# system, and so shouldn't trigger E1101 when accessed. Python regular\n# expressions are accepted.\ngenerated-members=\n\n\n[MISCELLANEOUS]\n\n# List of note tags to take in consideration, separated by a comma.\nnotes=FIXME,XXX,TODO\n\n\n[VARIABLES]\n\n# Tells whether we should check for unused import in __init__ files.\ninit-import=no\n\n# A regular expression matching the name of dummy variables (i.e. expectedly\n# not used).\ndummy-variables-rgx=_$|dummy\n\n# List of additional names supposed to be defined in builtins. Remember that\n# you should avoid to define new builtins when possible.\nadditional-builtins=\n\n# List of strings which can identify a callback function by name. A callback\n# name must start or end with one of those strings.\ncallbacks=cb_,_cb\n\n\n[CLASSES]\n\n# List of method names used to declare (i.e. assign) instance attributes.\ndefining-attr-methods=__init__,__new__,setUp\n\n# List of valid names for the first argument in a class method.\nvalid-classmethod-first-arg=cls\n\n# List of valid names for the first argument in a metaclass class method.\nvalid-metaclass-classmethod-first-arg=mcs\n\n# List of member names, which should be excluded from the protected access\n# warning.\nexclude-protected=_asdict,_fields,_replace,_source,_make\n\n\n[DESIGN]\n\n# Maximum number of arguments for function / method\nmax-args=5\n\n# Argument names that match this expression will be ignored. Default to name\n# with leading underscore\nignored-argument-names=_.*\n\n# Maximum number of locals for function / method body\nmax-locals=15\n\n# Maximum number of return / yield for function / method body\nmax-returns=6\n\n# Maximum number of branch for function / method body\nmax-branches=12\n\n# Maximum number of statements in function / method body\nmax-statements=50\n\n# Maximum number of parents for a class (see R0901).\nmax-parents=7\n\n# Maximum number of attributes for a class (see R0902).\nmax-attributes=7\n\n# Minimum number of public methods for a class (see R0903).\nmin-public-methods=2\n\n# Maximum number of public methods for a class (see R0904).\nmax-public-methods=20\n\n# Maximum number of boolean expressions in a if statement\nmax-bool-expr=5\n\n\n[IMPORTS]\n\n# Deprecated modules which should not be used, separated by a comma\ndeprecated-modules=regsub,TERMIOS,Bastion,rexec\n\n# Create a graph of every (i.e. internal and external) dependencies in the\n# given file (report RP0402 must not be disabled)\nimport-graph=\n\n# Create a graph of external dependencies in the given file (report RP0402 must\n# not be disabled)\next-import-graph=\n\n# Create a graph of internal dependencies in the given file (report RP0402 must\n# not be disabled)\nint-import-graph=\n\n\n[EXCEPTIONS]\n\n# Exceptions that will emit a warning when being caught. Defaults to\n# \"Exception\"\novergeneral-exceptions=Exception\n"
  },
  {
    "path": "pyproject.toml",
    "content": "[build-system]\nrequires = [\"setuptools>=58.5.0\"]\nbuild-backend = \"setuptools.build_meta\"\n\n[project]\nname = \"yapf\"\ndescription = \"A formatter for Python code\"\nauthors = [{ name = \"Google Inc.\" }]\nmaintainers = [{ name = \"Bill Wendling\", email = \"morbo@google.com\" }]\ndynamic = [\"version\"]\nlicense = { file = \"LICENSE\" }\nreadme = \"README.md\"\nrequires-python = \">=3.7\"\nclassifiers = [\n    'Development Status :: 4 - Beta',\n    'Environment :: Console',\n    'Intended Audience :: Developers',\n    'License :: OSI Approved :: Apache Software License',\n    'Operating System :: OS Independent',\n    'Programming Language :: Python',\n    'Programming Language :: Python :: 3 :: Only',\n    'Programming Language :: Python :: 3.7',\n    'Programming Language :: Python :: 3.8',\n    'Programming Language :: Python :: 3.9',\n    'Programming Language :: Python :: 3.10',\n    'Programming Language :: Python :: 3.11',\n    'Topic :: Software Development :: Libraries :: Python Modules',\n    'Topic :: Software Development :: Quality Assurance',\n]\ndependencies = ['platformdirs>=3.5.1', 'tomli>=2.0.1; python_version<\"3.11\"']\n\n[project.scripts]\nyapf = \"yapf:run_main\"\nyapf-diff = \"yapf_third_party.yapf_diff.yapf_diff:main\"\n\n[project.urls]\n# https://daniel.feldroy.com/posts/2023-08-pypi-project-urls-cheatsheet\nHome = 'https://github.com/google/yapf'\nChangelog = 'https://github.com/google/yapf/blob/main/CHANGELOG.md'\nDocs = 'https://github.com/google/yapf/blob/main/README.md#yapf'\nIssues = 'https://github.com/google/yapf/issues'\n\n[tool.distutils.bdist_wheel]\npython_tag = \"py3\"\n\n[tool.setuptools]\ninclude-package-data = true\npackage-dir = { yapf_third_party = 'third_party/yapf_third_party' }\n\n[tool.setuptools.dynamic]\nversion = { attr = \"yapf._version.__version__\" }\n\n[tool.setuptools.packages.find]\nwhere = [\".\", 'third_party']\ninclude = [\"yapf*\", 'yapftests*']\n\n[tool.setuptools.package-data]\nyapf_third_party = [\n    'yapf_diff/LICENSE',\n    '_ylib2to3/Grammar.txt',\n    '_ylib2to3/PatternGrammar.txt',\n    '_ylib2to3/LICENSE',\n]\n"
  },
  {
    "path": "third_party/__init__.py",
    "content": ""
  },
  {
    "path": "third_party/yapf_third_party/__init__.py",
    "content": ""
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/Grammar.txt",
    "content": "# Grammar for 2to3. This grammar supports Python 2.x and 3.x.\n\n# NOTE WELL: You should also follow all the steps listed at\n# https://devguide.python.org/grammar/\n\n# Start symbols for the grammar:\n#\tfile_input is a module or sequence of commands read from an input file;\n#\tsingle_input is a single interactive statement;\n#\teval_input is the input for the eval() and input() functions.\n# NB: compound_stmt in single_input is followed by extra NEWLINE!\nfile_input: (NEWLINE | stmt)* ENDMARKER\nsingle_input: NEWLINE | simple_stmt | compound_stmt NEWLINE\neval_input: testlist NEWLINE* ENDMARKER\n\ndecorator: '@' namedexpr_test NEWLINE\ndecorators: decorator+\ndecorated: decorators (classdef | funcdef | async_funcdef)\nasync_funcdef: ASYNC funcdef\nfuncdef: 'def' NAME parameters ['->' test] ':' suite\nparameters: '(' [typedargslist] ')'\n\n# The following definition for typedarglist is equivalent to this set of rules:\n#\n#     arguments = argument (',' argument)*\n#     argument = tfpdef ['=' test]\n#     kwargs = '**' tname [',']\n#     args = '*' [tname_star]\n#     kwonly_kwargs = (',' argument)* [',' [kwargs]]\n#     args_kwonly_kwargs = args kwonly_kwargs | kwargs\n#     poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]\n#     typedargslist_no_posonly  = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs\n#     typedarglist = arguments ',' '/' [',' [typedargslist_no_posonly]])|(typedargslist_no_posonly)\"\n#\n# It needs to be fully expanded to allow our LL(1) parser to work on it.\n\ntypedargslist: tfpdef ['=' test] (',' tfpdef ['=' test])* ',' '/' [\n                     ',' [((tfpdef ['=' test] ',')* ('*' [tname_star] (',' tname ['=' test])*\n                            [',' ['**' tname [',']]] | '**' tname [','])\n                     | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])]\n                ] | ((tfpdef ['=' test] ',')* ('*' [tname_star] (',' tname ['=' test])*\n                     [',' ['**' tname [',']]] | '**' tname [','])\n                     | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])\n\ntname: NAME [':' test]\ntname_star: NAME [':' (test|star_expr)]\ntfpdef: tname | '(' tfplist ')'\ntfplist: tfpdef (',' tfpdef)* [',']\n\n# The following definition for varargslist is equivalent to this set of rules:\n#\n#     arguments = argument (',' argument )*\n#     argument = vfpdef ['=' test]\n#     kwargs = '**' vname [',']\n#     args = '*' [vname]\n#     kwonly_kwargs = (',' argument )* [',' [kwargs]]\n#     args_kwonly_kwargs = args kwonly_kwargs | kwargs\n#     poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]\n#     vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs\n#     varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | (vararglist_no_posonly)\n#\n# It needs to be fully expanded to allow our LL(1) parser to work on it.\n\nvarargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [\n                     ((vfpdef ['=' test] ',')* ('*' [vname] (',' vname ['=' test])*\n                            [',' ['**' vname [',']]] | '**' vname [','])\n                            | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])\n                     ]] | ((vfpdef ['=' test] ',')*\n                     ('*' [vname] (',' vname ['=' test])*  [',' ['**' vname [',']]]| '**' vname [','])\n                     | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])\n\nvname: NAME\nvfpdef: vname | '(' vfplist ')'\nvfplist: vfpdef (',' vfpdef)* [',']\n\nstmt: simple_stmt | compound_stmt\nsimple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE\nsmall_stmt: (expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt |\n             import_stmt | global_stmt | exec_stmt | assert_stmt)\nexpr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |\n                     ('=' (yield_expr|testlist_star_expr))*)\nannassign: ':' test ['=' (yield_expr|testlist_star_expr)]\ntestlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']\naugassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |\n            '<<=' | '>>=' | '**=' | '//=')\n# For normal and annotated assignments, additional restrictions enforced by the interpreter\nprint_stmt: 'print' ( [ test (',' test)* [','] ] |\n                      '>>' test [ (',' test)+ [','] ] )\ndel_stmt: 'del' exprlist\npass_stmt: 'pass'\nflow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt\nbreak_stmt: 'break'\ncontinue_stmt: 'continue'\nreturn_stmt: 'return' [testlist_star_expr]\nyield_stmt: yield_expr\nraise_stmt: 'raise' [test ['from' test | ',' test [',' test]]]\nimport_stmt: import_name | import_from\nimport_name: 'import' dotted_as_names\nimport_from: ('from' ('.'* dotted_name | '.'+)\n              'import' ('*' | '(' import_as_names ')' | import_as_names))\nimport_as_name: NAME ['as' NAME]\ndotted_as_name: dotted_name ['as' NAME]\nimport_as_names: import_as_name (',' import_as_name)* [',']\ndotted_as_names: dotted_as_name (',' dotted_as_name)*\ndotted_name: NAME ('.' NAME)*\nglobal_stmt: ('global' | 'nonlocal') NAME (',' NAME)*\nexec_stmt: 'exec' expr ['in' test [',' test]]\nassert_stmt: 'assert' test [',' test]\n\ncompound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt | match_stmt\nasync_stmt: ASYNC (funcdef | with_stmt | for_stmt)\nif_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite]\nwhile_stmt: 'while' namedexpr_test ':' suite ['else' ':' suite]\nfor_stmt: 'for' exprlist 'in' testlist_star_expr ':' suite ['else' ':' suite]\ntry_stmt: ('try' ':' suite\n           ((except_clause ':' suite)+\n\t    ['else' ':' suite]\n\t    ['finally' ':' suite] |\n\t   'finally' ':' suite))\nwith_stmt: 'with' asexpr_test (',' asexpr_test)*  ':' suite\n\n# NB compile.c makes sure that the default except clause is last\nexcept_clause: 'except' ['*'] [test [(',' | 'as') test]]\nsuite: simple_stmt | NEWLINE INDENT stmt+ DEDENT\n\n# Backward compatibility cruft to support:\n# [ x for x in lambda: True, lambda: False if x() ]\n# even while also allowing:\n# lambda x: 5 if x else 2\n# (But not a mix of the two)\ntestlist_safe: old_test [(',' old_test)+ [',']]\nold_test: or_test | old_lambdef\nold_lambdef: 'lambda' [varargslist] ':' old_test\n\nnamedexpr_test: asexpr_test [':=' asexpr_test]\n\n# This is actually not a real rule, though since the parser is very\n# limited in terms of the strategy about match/case rules, we are inserting\n# a virtual case (<expr> as <expr>) as a valid expression. Unless a better\n# approach is thought, the only side effect of this seem to be just allowing\n# more stuff to be parser (which would fail on the ast).\nasexpr_test: test ['as' test]\n\ntest: or_test ['if' or_test 'else' test] | lambdef\nor_test: and_test ('or' and_test)*\nand_test: not_test ('and' not_test)*\nnot_test: 'not' not_test | comparison\ncomparison: expr (comp_op expr)*\ncomp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'\nstar_expr: '*' expr\nexpr: xor_expr ('|' xor_expr)*\nxor_expr: and_expr ('^' and_expr)*\nand_expr: shift_expr ('&' shift_expr)*\nshift_expr: arith_expr (('<<'|'>>') arith_expr)*\narith_expr: term (('+'|'-') term)*\nterm: factor (('*'|'@'|'/'|'%'|'//') factor)*\nfactor: ('+'|'-'|'~') factor | power\npower: [AWAIT] atom trailer* ['**' factor]\natom: ('(' [yield_expr|testlist_gexp] ')' |\n       '[' [listmaker] ']' |\n       '{' [dictsetmaker] '}' |\n       '`' testlist1 '`' |\n       NAME | NUMBER | STRING+ | '.' '.' '.')\nlistmaker: (namedexpr_test|star_expr) ( old_comp_for | (',' (namedexpr_test|star_expr))* [','] )\ntestlist_gexp: (namedexpr_test|star_expr) ( old_comp_for | (',' (namedexpr_test|star_expr))* [','] )\nlambdef: 'lambda' [varargslist] ':' test\ntrailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME\nsubscriptlist: (subscript|star_expr) (',' (subscript|star_expr))* [',']\nsubscript: test [':=' test] | [test] ':' [test] [sliceop]\nsliceop: ':' [test]\nexprlist: (expr|star_expr) (',' (expr|star_expr))* [',']\ntestlist: test (',' test)* [',']\ndictsetmaker: ( ((test ':' asexpr_test | '**' expr)\n                 (comp_for | (',' (test ':' asexpr_test | '**' expr))* [','])) |\n                ((test [':=' test] | star_expr)\n\t\t (comp_for | (',' (test [':=' test] | star_expr))* [','])) )\n\nclassdef: 'class' NAME ['(' [arglist] ')'] ':' suite\n\narglist: argument (',' argument)* [',']\n\n# \"test '=' test\" is really \"keyword '=' test\", but we have no such token.\n# These need to be in a single rule to avoid grammar that is ambiguous\n# to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,\n# we explicitly match '*' here, too, to give it proper precedence.\n# Illegal combinations and orderings are blocked in ast.c:\n# multiple (test comp_for) arguments are blocked; keyword unpackings\n# that precede iterable unpackings are blocked; etc.\nargument: ( test [comp_for] |\n            test ':=' test [comp_for] |\n            test 'as' test |\n            test '=' asexpr_test |\n\t    '**' test |\n            '*' test )\n\ncomp_iter: comp_for | comp_if\ncomp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]\ncomp_if: 'if' old_test [comp_iter]\n\n# As noted above, testlist_safe extends the syntax allowed in list\n# comprehensions and generators. We can't use it indiscriminately in all\n# derivations using a comp_for-like pattern because the testlist_safe derivation\n# contains comma which clashes with trailing comma in arglist.\n#\n# This was an issue because the parser would not follow the correct derivation\n# when parsing syntactically valid Python code. Since testlist_safe was created\n# specifically to handle list comprehensions and generator expressions enclosed\n# with parentheses, it's safe to only use it in those. That avoids the issue; we\n# can parse code like set(x for x in [],).\n#\n# The syntax supported by this set of rules is not a valid Python 3 syntax,\n# hence the prefix \"old\".\n#\n# See https://bugs.python.org/issue27494\nold_comp_iter: old_comp_for | old_comp_if\nold_comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [old_comp_iter]\nold_comp_if: 'if' old_test [old_comp_iter]\n\ntestlist1: test (',' test)*\n\n# not used in grammar, but may appear in \"node\" passed from Parser to Compiler\nencoding_decl: NAME\n\nyield_expr: 'yield' [yield_arg]\nyield_arg: 'from' test | testlist_star_expr\n\n\n# 3.10 match statement definition\n\n# PS: normally the grammar is much much more restricted, but\n# at this moment for not trying to bother much with encoding the\n# exact same DSL in a LL(1) parser, we will just accept an expression\n# and let the ast.parse() step of the safe mode to reject invalid\n# grammar.\n\n# The reason why it is more restricted is that, patterns are some\n# sort of a DSL (more advanced than our LHS on assignments, but\n# still in a very limited python subset). They are not really\n# expressions, but who cares. If we can parse them, that is enough\n# to reformat them.\n\nmatch_stmt: \"match\" subject_expr ':' NEWLINE INDENT case_block+ DEDENT\n\n# This is more permissive than the actual version. For example it\n# accepts `match *something:`, even though single-item starred expressions\n# are forbidden.\nsubject_expr: (namedexpr_test|star_expr) (',' (namedexpr_test|star_expr))* [',']\n\n# cases\ncase_block: \"case\" patterns [guard] ':' suite\nguard: 'if' namedexpr_test\npatterns: pattern (',' pattern)* [',']\npattern: (expr|star_expr) ['as' expr]\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/LICENSE",
    "content": "A. HISTORY OF THE SOFTWARE\n==========================\n\nPython was created in the early 1990s by Guido van Rossum at Stichting\nMathematisch Centrum (CWI, see https://www.cwi.nl) in the Netherlands\nas a successor of a language called ABC.  Guido remains Python's\nprincipal author, although it includes many contributions from others.\n\nIn 1995, Guido continued his work on Python at the Corporation for\nNational Research Initiatives (CNRI, see https://www.cnri.reston.va.us)\nin Reston, Virginia where he released several versions of the\nsoftware.\n\nIn May 2000, Guido and the Python core development team moved to\nBeOpen.com to form the BeOpen PythonLabs team.  In October of the same\nyear, the PythonLabs team moved to Digital Creations, which became\nZope Corporation.  In 2001, the Python Software Foundation (PSF, see\nhttps://www.python.org/psf/) was formed, a non-profit organization\ncreated specifically to own Python-related Intellectual Property.\nZope Corporation was a sponsoring member of the PSF.\n\nAll Python releases are Open Source (see https://opensource.org for\nthe Open Source Definition).  Historically, most, but not all, Python\nreleases have also been GPL-compatible; the table below summarizes\nthe various releases.\n\n    Release         Derived     Year        Owner       GPL-\n                    from                                compatible? (1)\n\n    0.9.0 thru 1.2              1991-1995   CWI         yes\n    1.3 thru 1.5.2  1.2         1995-1999   CNRI        yes\n    1.6             1.5.2       2000        CNRI        no\n    2.0             1.6         2000        BeOpen.com  no\n    1.6.1           1.6         2001        CNRI        yes (2)\n    2.1             2.0+1.6.1   2001        PSF         no\n    2.0.1           2.0+1.6.1   2001        PSF         yes\n    2.1.1           2.1+2.0.1   2001        PSF         yes\n    2.1.2           2.1.1       2002        PSF         yes\n    2.1.3           2.1.2       2002        PSF         yes\n    2.2 and above   2.1.1       2001-now    PSF         yes\n\nFootnotes:\n\n(1) GPL-compatible doesn't mean that we're distributing Python under\n    the GPL.  All Python licenses, unlike the GPL, let you distribute\n    a modified version without making your changes open source.  The\n    GPL-compatible licenses make it possible to combine Python with\n    other software that is released under the GPL; the others don't.\n\n(2) According to Richard Stallman, 1.6.1 is not GPL-compatible,\n    because its license has a choice of law clause.  According to\n    CNRI, however, Stallman's lawyer has told CNRI's lawyer that 1.6.1\n    is \"not incompatible\" with the GPL.\n\nThanks to the many outside volunteers who have worked under Guido's\ndirection to make these releases possible.\n\n\nB. TERMS AND CONDITIONS FOR ACCESSING OR OTHERWISE USING PYTHON\n===============================================================\n\nPYTHON SOFTWARE FOUNDATION LICENSE VERSION 2\n--------------------------------------------\n\n1. This LICENSE AGREEMENT is between the Python Software Foundation\n(\"PSF\"), and the Individual or Organization (\"Licensee\") accessing and\notherwise using this software (\"Python\") in source or binary form and\nits associated documentation.\n\n2. Subject to the terms and conditions of this License Agreement, PSF hereby\ngrants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,\nanalyze, test, perform and/or display publicly, prepare derivative works,\ndistribute, and otherwise use Python alone or in any derivative version,\nprovided, however, that PSF's License Agreement and PSF's notice of copyright,\ni.e., \"Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,\n2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Python Software Foundation; All\nRights Reserved\" are retained in Python alone or in any derivative version\nprepared by Licensee.\n\n3. In the event Licensee prepares a derivative work that is based on\nor incorporates Python or any part thereof, and wants to make\nthe derivative work available to others as provided herein, then\nLicensee hereby agrees to include in any such work a brief summary of\nthe changes made to Python.\n\n4. PSF is making Python available to Licensee on an \"AS IS\"\nbasis.  PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR\nIMPLIED.  BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND\nDISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS\nFOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT\nINFRINGE ANY THIRD PARTY RIGHTS.\n\n5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON\nFOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS\nA RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON,\nOR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.\n\n6. This License Agreement will automatically terminate upon a material\nbreach of its terms and conditions.\n\n7. Nothing in this License Agreement shall be deemed to create any\nrelationship of agency, partnership, or joint venture between PSF and\nLicensee.  This License Agreement does not grant permission to use PSF\ntrademarks or trade name in a trademark sense to endorse or promote\nproducts or services of Licensee, or any third party.\n\n8. By copying, installing or otherwise using Python, Licensee\nagrees to be bound by the terms and conditions of this License\nAgreement.\n\n\nBEOPEN.COM LICENSE AGREEMENT FOR PYTHON 2.0\n-------------------------------------------\n\nBEOPEN PYTHON OPEN SOURCE LICENSE AGREEMENT VERSION 1\n\n1. This LICENSE AGREEMENT is between BeOpen.com (\"BeOpen\"), having an\noffice at 160 Saratoga Avenue, Santa Clara, CA 95051, and the\nIndividual or Organization (\"Licensee\") accessing and otherwise using\nthis software in source or binary form and its associated\ndocumentation (\"the Software\").\n\n2. Subject to the terms and conditions of this BeOpen Python License\nAgreement, BeOpen hereby grants Licensee a non-exclusive,\nroyalty-free, world-wide license to reproduce, analyze, test, perform\nand/or display publicly, prepare derivative works, distribute, and\notherwise use the Software alone or in any derivative version,\nprovided, however, that the BeOpen Python License is retained in the\nSoftware, alone or in any derivative version prepared by Licensee.\n\n3. BeOpen is making the Software available to Licensee on an \"AS IS\"\nbasis.  BEOPEN MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR\nIMPLIED.  BY WAY OF EXAMPLE, BUT NOT LIMITATION, BEOPEN MAKES NO AND\nDISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS\nFOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE WILL NOT\nINFRINGE ANY THIRD PARTY RIGHTS.\n\n4. BEOPEN SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF THE\nSOFTWARE FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS\nAS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE, OR ANY\nDERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.\n\n5. This License Agreement will automatically terminate upon a material\nbreach of its terms and conditions.\n\n6. This License Agreement shall be governed by and interpreted in all\nrespects by the law of the State of California, excluding conflict of\nlaw provisions.  Nothing in this License Agreement shall be deemed to\ncreate any relationship of agency, partnership, or joint venture\nbetween BeOpen and Licensee.  This License Agreement does not grant\npermission to use BeOpen trademarks or trade names in a trademark\nsense to endorse or promote products or services of Licensee, or any\nthird party.  As an exception, the \"BeOpen Python\" logos available at\nhttp://www.pythonlabs.com/logos.html may be used according to the\npermissions granted on that web page.\n\n7. By copying, installing or otherwise using the software, Licensee\nagrees to be bound by the terms and conditions of this License\nAgreement.\n\n\nCNRI LICENSE AGREEMENT FOR PYTHON 1.6.1\n---------------------------------------\n\n1. This LICENSE AGREEMENT is between the Corporation for National\nResearch Initiatives, having an office at 1895 Preston White Drive,\nReston, VA 20191 (\"CNRI\"), and the Individual or Organization\n(\"Licensee\") accessing and otherwise using Python 1.6.1 software in\nsource or binary form and its associated documentation.\n\n2. Subject to the terms and conditions of this License Agreement, CNRI\nhereby grants Licensee a nonexclusive, royalty-free, world-wide\nlicense to reproduce, analyze, test, perform and/or display publicly,\nprepare derivative works, distribute, and otherwise use Python 1.6.1\nalone or in any derivative version, provided, however, that CNRI's\nLicense Agreement and CNRI's notice of copyright, i.e., \"Copyright (c)\n1995-2001 Corporation for National Research Initiatives; All Rights\nReserved\" are retained in Python 1.6.1 alone or in any derivative\nversion prepared by Licensee.  Alternately, in lieu of CNRI's License\nAgreement, Licensee may substitute the following text (omitting the\nquotes): \"Python 1.6.1 is made available subject to the terms and\nconditions in CNRI's License Agreement.  This Agreement together with\nPython 1.6.1 may be located on the Internet using the following\nunique, persistent identifier (known as a handle): 1895.22/1013.  This\nAgreement may also be obtained from a proxy server on the Internet\nusing the following URL: http://hdl.handle.net/1895.22/1013\".\n\n3. In the event Licensee prepares a derivative work that is based on\nor incorporates Python 1.6.1 or any part thereof, and wants to make\nthe derivative work available to others as provided herein, then\nLicensee hereby agrees to include in any such work a brief summary of\nthe changes made to Python 1.6.1.\n\n4. CNRI is making Python 1.6.1 available to Licensee on an \"AS IS\"\nbasis.  CNRI MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR\nIMPLIED.  BY WAY OF EXAMPLE, BUT NOT LIMITATION, CNRI MAKES NO AND\nDISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS\nFOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 1.6.1 WILL NOT\nINFRINGE ANY THIRD PARTY RIGHTS.\n\n5. CNRI SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON\n1.6.1 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS\nA RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 1.6.1,\nOR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.\n\n6. This License Agreement will automatically terminate upon a material\nbreach of its terms and conditions.\n\n7. This License Agreement shall be governed by the federal\nintellectual property law of the United States, including without\nlimitation the federal copyright law, and, to the extent such\nU.S. federal law does not apply, by the law of the Commonwealth of\nVirginia, excluding Virginia's conflict of law provisions.\nNotwithstanding the foregoing, with regard to derivative works based\non Python 1.6.1 that incorporate non-separable material that was\npreviously distributed under the GNU General Public License (GPL), the\nlaw of the Commonwealth of Virginia shall govern this License\nAgreement only as to issues arising under or with respect to\nParagraphs 4, 5, and 7 of this License Agreement.  Nothing in this\nLicense Agreement shall be deemed to create any relationship of\nagency, partnership, or joint venture between CNRI and Licensee.  This\nLicense Agreement does not grant permission to use CNRI trademarks or\ntrade name in a trademark sense to endorse or promote products or\nservices of Licensee, or any third party.\n\n8. By clicking on the \"ACCEPT\" button where indicated, or by copying,\ninstalling or otherwise using Python 1.6.1, Licensee agrees to be\nbound by the terms and conditions of this License Agreement.\n\n        ACCEPT\n\n\nCWI LICENSE AGREEMENT FOR PYTHON 0.9.0 THROUGH 1.2\n--------------------------------------------------\n\nCopyright (c) 1991 - 1995, Stichting Mathematisch Centrum Amsterdam,\nThe Netherlands.  All rights reserved.\n\nPermission to use, copy, modify, and distribute this software and its\ndocumentation for any purpose and without fee is hereby granted,\nprovided that the above copyright notice appear in all copies and that\nboth that copyright notice and this permission notice appear in\nsupporting documentation, and that the name of Stichting Mathematisch\nCentrum or CWI not be used in advertising or publicity pertaining to\ndistribution of the software without specific, written prior\npermission.\n\nSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO\nTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND\nFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE\nFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\nWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\nACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT\nOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/PatternGrammar.txt",
    "content": "# Copyright 2006 Google, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\n# A grammar to describe tree matching patterns.\n# Not shown here:\n# - 'TOKEN' stands for any token (leaf node)\n# - 'any' stands for any node (leaf or interior)\n# With 'any' we can still specify the sub-structure.\n\n# The start symbol is 'Matcher'.\n\nMatcher: Alternatives ENDMARKER\n\nAlternatives: Alternative ('|' Alternative)*\n\nAlternative: (Unit | NegatedUnit)+\n\nUnit: [NAME '='] ( STRING [Repeater]\n                 | NAME [Details] [Repeater]\n                 | '(' Alternatives ')' [Repeater]\n                 | '[' Alternatives ']'\n\t\t )\n\nNegatedUnit: 'not' (STRING | NAME [Details] | '(' Alternatives ')')\n\nRepeater: '*' | '+' | '{' NUMBER [',' NUMBER] '}'\n\nDetails: '<' Alternatives '>'\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/README.rst",
    "content": "A fork of python's lib2to3 with select features backported from black's blib2to3.\n\nReasons for forking:\n\n- black's fork of lib2to3 already considers newer features like Structured Pattern matching\n- lib2to3 itself is deprecated and no longer getting support\n\nMaintenance moving forward:\n- Most changes moving forward should only have to be done to the grammar files in this project.\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/__init__.py",
    "content": "\"\"\"fork of python's lib2to3 with some backports from black's blib2to3\"\"\"\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/fixer_base.py",
    "content": "# Copyright 2006 Google, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\"\"\"Base class for fixers (optional, but recommended).\"\"\"\n\n# Python imports\nimport itertools\n\nfrom . import pygram\nfrom .fixer_util import does_tree_import\n# Local imports\nfrom .patcomp import PatternCompiler\n\n\nclass BaseFix(object):\n  \"\"\"Optional base class for fixers.\n\n  The subclass name must be FixFooBar where FooBar is the result of\n  removing underscores and capitalizing the words of the fix name.\n  For example, the class name for a fixer named 'has_key' should be\n  FixHasKey.\n  \"\"\"\n\n  PATTERN = None  # Most subclasses should override with a string literal\n  pattern = None  # Compiled pattern, set by compile_pattern()\n  pattern_tree = None  # Tree representation of the pattern\n  options = None  # Options object passed to initializer\n  filename = None  # The filename (set by set_filename)\n  numbers = itertools.count(1)  # For new_name()\n  used_names = set()  # A set of all used NAMEs\n  order = 'post'  # Does the fixer prefer pre- or post-order traversal\n  explicit = False  # Is this ignored by refactor.py -f all?\n  run_order = 5  # Fixers will be sorted by run order before execution\n  # Lower numbers will be run first.\n  _accept_type = None  # [Advanced and not public] This tells RefactoringTool\n  # which node type to accept when there's not a pattern.\n\n  keep_line_order = False  # For the bottom matcher: match with the\n  # original line order\n  BM_compatible = False  # Compatibility with the bottom matching\n  # module; every fixer should set this\n  # manually\n\n  # Shortcut for access to Python grammar symbols\n  syms = pygram.python_symbols\n\n  def __init__(self, options, log):\n    \"\"\"Initializer.  Subclass may override.\n\n    Args:\n        options: a dict containing the options passed to RefactoringTool\n        that could be used to customize the fixer through the command line.\n        log: a list to append warnings and other messages to.\n    \"\"\"\n    self.options = options\n    self.log = log\n    self.compile_pattern()\n\n  def compile_pattern(self):\n    \"\"\"Compiles self.PATTERN into self.pattern.\n\n    Subclass may override if it doesn't want to use\n    self.{pattern,PATTERN} in .match().\n    \"\"\"\n    if self.PATTERN is not None:\n      PC = PatternCompiler()\n      self.pattern, self.pattern_tree = PC.compile_pattern(\n          self.PATTERN, with_tree=True)\n\n  def set_filename(self, filename):\n    \"\"\"Set the filename.\n\n    The main refactoring tool should call this.\n    \"\"\"\n    self.filename = filename\n\n  def match(self, node):\n    \"\"\"Returns match for a given parse tree node.\n\n    Should return a true or false object (not necessarily a bool).\n    It may return a non-empty dict of matching sub-nodes as\n    returned by a matching pattern.\n\n    Subclass may override.\n    \"\"\"\n    results = {'node': node}\n    return self.pattern.match(node, results) and results\n\n  def transform(self, node, results):\n    \"\"\"Returns the transformation for a given parse tree node.\n\n    Args:\n        node: the root of the parse tree that matched the fixer.\n        results: a dict mapping symbolic names to part of the match.\n\n    Returns:\n        None, or a node that is a modified copy of the\n        argument node.  The node argument may also be modified in-place to\n        effect the same change.\n\n    Subclass *must* override.\n    \"\"\"\n    raise NotImplementedError()\n\n  def new_name(self, template='xxx_todo_changeme'):\n    \"\"\"Return a string suitable for use as an identifier\n\n    The new name is guaranteed not to conflict with other identifiers.\n    \"\"\"\n    name = template\n    while name in self.used_names:\n      name = template + str(next(self.numbers))\n    self.used_names.add(name)\n    return name\n\n  def log_message(self, message):\n    if self.first_log:\n      self.first_log = False\n      self.log.append('### In file %s ###' % self.filename)\n    self.log.append(message)\n\n  def cannot_convert(self, node, reason=None):\n    \"\"\"Warn the user that a given chunk of code is not valid Python 3,\n       but that it cannot be converted automatically.\n\n    First argument is the top-level node for the code in question.\n    Optional second argument is why it can't be converted.\n    \"\"\"\n    lineno = node.get_lineno()\n    for_output = node.clone()\n    for_output.prefix = ''\n    msg = 'Line %d: could not convert: %s'\n    self.log_message(msg % (lineno, for_output))\n    if reason:\n      self.log_message(reason)\n\n  def warning(self, node, reason):\n    \"\"\"Used for warning the user about possible uncertainty in the translation.\n\n    First argument is the top-level node for the code in question.\n    Optional second argument is why it can't be converted.\n    \"\"\"\n    lineno = node.get_lineno()\n    self.log_message('Line %d: %s' % (lineno, reason))\n\n  def start_tree(self, tree, filename):\n    \"\"\"Some fixers need to maintain tree-wide state.\n\n    This method is called once, at the start of tree fix-up.\n\n    tree - the root node of the tree to be processed.\n    filename - the name of the file the tree came from.\n    \"\"\"\n    self.used_names = tree.used_names\n    self.set_filename(filename)\n    self.numbers = itertools.count(1)\n    self.first_log = True\n\n  def finish_tree(self, tree, filename):\n    \"\"\"Some fixers need to maintain tree-wide state.\n\n    This method is called once, at the conclusion of tree fix-up.\n\n    tree - the root node of the tree to be processed.\n    filename - the name of the file the tree came from.\n    \"\"\"\n    pass\n\n\nclass ConditionalFix(BaseFix):\n  \"\"\" Base class for fixers which not execute if an import is found. \"\"\"\n\n  # This is the name of the import which, if found, will cause the test to be\n  # skipped.\n  skip_on = None\n\n  def start_tree(self, *args):\n    super(ConditionalFix, self).start_tree(*args)\n    self._should_skip = None\n\n  def should_skip(self, node):\n    if self._should_skip is not None:\n      return self._should_skip\n    pkg = self.skip_on.split('.')\n    name = pkg[-1]\n    pkg = '.'.join(pkg[:-1])\n    self._should_skip = does_tree_import(pkg, name, node)\n    return self._should_skip\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/fixer_util.py",
    "content": "\"\"\"Utility functions, node construction macros, etc.\"\"\"\n# Author: Collin Winter\n\nfrom . import patcomp\n# Local imports\nfrom .pgen2 import token\nfrom .pygram import python_symbols as syms\nfrom .pytree import Leaf\nfrom .pytree import Node\n\n###########################################################\n# Common node-construction \"macros\"\n###########################################################\n\n\ndef KeywordArg(keyword, value):\n  return Node(syms.argument, [keyword, Leaf(token.EQUAL, '='), value])\n\n\ndef LParen():\n  return Leaf(token.LPAR, '(')\n\n\ndef RParen():\n  return Leaf(token.RPAR, ')')\n\n\ndef Assign(target, source):\n  \"\"\"Build an assignment statement\"\"\"\n  if not isinstance(target, list):\n    target = [target]\n  if not isinstance(source, list):\n    source.prefix = ' '\n    source = [source]\n\n  return Node(syms.atom, target + [Leaf(token.EQUAL, '=', prefix=' ')] + source)\n\n\ndef Name(name, prefix=None):\n  \"\"\"Return a NAME leaf\"\"\"\n  return Leaf(token.NAME, name, prefix=prefix)\n\n\ndef Attr(obj, attr):\n  \"\"\"A node tuple for obj.attr\"\"\"\n  return [obj, Node(syms.trailer, [Dot(), attr])]\n\n\ndef Comma():\n  \"\"\"A comma leaf\"\"\"\n  return Leaf(token.COMMA, ',')\n\n\ndef Dot():\n  \"\"\"A period (.) leaf\"\"\"\n  return Leaf(token.DOT, '.')\n\n\ndef ArgList(args, lparen=LParen(), rparen=RParen()):\n  \"\"\"A parenthesised argument list, used by Call()\"\"\"\n  node = Node(syms.trailer, [lparen.clone(), rparen.clone()])\n  if args:\n    node.insert_child(1, Node(syms.arglist, args))\n  return node\n\n\ndef Call(func_name, args=None, prefix=None):\n  \"\"\"A function call\"\"\"\n  node = Node(syms.power, [func_name, ArgList(args)])\n  if prefix is not None:\n    node.prefix = prefix\n  return node\n\n\ndef Newline():\n  \"\"\"A newline literal\"\"\"\n  return Leaf(token.NEWLINE, '\\n')\n\n\ndef BlankLine():\n  \"\"\"A blank line\"\"\"\n  return Leaf(token.NEWLINE, '')\n\n\ndef Number(n, prefix=None):\n  return Leaf(token.NUMBER, n, prefix=prefix)\n\n\ndef Subscript(index_node):\n  \"\"\"A numeric or string subscript\"\"\"\n  return Node(syms.trailer,\n              [Leaf(token.LBRACE, '['), index_node,\n               Leaf(token.RBRACE, ']')])\n\n\ndef String(string, prefix=None):\n  \"\"\"A string leaf\"\"\"\n  return Leaf(token.STRING, string, prefix=prefix)\n\n\ndef ListComp(xp, fp, it, test=None):\n  \"\"\"A list comprehension of the form [xp for fp in it if test].\n\n  If test is None, the \"if test\" part is omitted.\n  \"\"\"\n  xp.prefix = ''\n  fp.prefix = ' '\n  it.prefix = ' '\n  for_leaf = Leaf(token.NAME, 'for')\n  for_leaf.prefix = ' '\n  in_leaf = Leaf(token.NAME, 'in')\n  in_leaf.prefix = ' '\n  inner_args = [for_leaf, fp, in_leaf, it]\n  if test:\n    test.prefix = ' '\n    if_leaf = Leaf(token.NAME, 'if')\n    if_leaf.prefix = ' '\n    inner_args.append(Node(syms.comp_if, [if_leaf, test]))\n  inner = Node(syms.listmaker, [xp, Node(syms.comp_for, inner_args)])\n  return Node(syms.atom,\n              [Leaf(token.LBRACE, '['), inner,\n               Leaf(token.RBRACE, ']')])\n\n\ndef FromImport(package_name, name_leafs):\n  \"\"\" Return an import statement in the form:\n\n       from package import name_leafs\n  \"\"\"\n  # XXX: May not handle dotted imports properly (eg, package_name='foo.bar')\n  # #assert package_name == '.' or '.' not in package_name, \"FromImport has \"\\\n  #       \"not been tested with dotted package names -- use at your own \"\\\n  #       \"peril!\"\n\n  for leaf in name_leafs:\n    # Pull the leaves out of their old tree\n    leaf.remove()\n\n  children = [\n      Leaf(token.NAME, 'from'),\n      Leaf(token.NAME, package_name, prefix=' '),\n      Leaf(token.NAME, 'import', prefix=' '),\n      Node(syms.import_as_names, name_leafs)\n  ]\n  imp = Node(syms.import_from, children)\n  return imp\n\n\ndef ImportAndCall(node, results, names):\n  \"\"\"Returns an import statement and calls a method of the module:\n\n      import module\n      module.name()\n  \"\"\"\n  obj = results['obj'].clone()\n  if obj.type == syms.arglist:\n    newarglist = obj.clone()\n  else:\n    newarglist = Node(syms.arglist, [obj.clone()])\n  after = results['after']\n  if after:\n    after = [n.clone() for n in after]\n  new = Node(\n      syms.power,\n      Attr(Name(names[0]), Name(names[1])) + [\n          Node(syms.trailer,\n               [results['lpar'].clone(), newarglist, results['rpar'].clone()])\n      ] + after)\n  new.prefix = node.prefix\n  return new\n\n\n###########################################################\n# Determine whether a node represents a given literal\n###########################################################\n\n\ndef is_tuple(node):\n  \"\"\"Does the node represent a tuple literal?\"\"\"\n  if isinstance(node, Node) and node.children == [LParen(), RParen()]:\n    return True\n  return (isinstance(node, Node) and len(node.children) == 3 and\n          isinstance(node.children[0], Leaf) and\n          isinstance(node.children[1], Node) and\n          isinstance(node.children[2], Leaf) and\n          node.children[0].value == '(' and node.children[2].value == ')')\n\n\ndef is_list(node):\n  \"\"\"Does the node represent a list literal?\"\"\"\n  return (isinstance(node, Node) and len(node.children) > 1 and\n          isinstance(node.children[0], Leaf) and\n          isinstance(node.children[-1], Leaf) and\n          node.children[0].value == '[' and node.children[-1].value == ']')\n\n\n###########################################################\n# Misc\n###########################################################\n\n\ndef parenthesize(node):\n  return Node(syms.atom, [LParen(), node, RParen()])\n\n\nconsuming_calls = {\n    'sorted', 'list', 'set', 'any', 'all', 'tuple', 'sum', 'min', 'max',\n    'enumerate'\n}\n\n\ndef attr_chain(obj, attr):\n  \"\"\"Follow an attribute chain.\n\n  If you have a chain of objects where a.foo -> b, b.foo-> c, etc, use this to\n  iterate over all objects in the chain. Iteration is terminated by getattr(x,\n  attr) is None.\n\n  Args:\n      obj: the starting object\n      attr: the name of the chaining attribute\n\n  Yields:\n      Each successive object in the chain.\n  \"\"\"\n  next = getattr(obj, attr)\n  while next:\n    yield next\n    next = getattr(next, attr)\n\n\np0 = \"\"\"for_stmt< 'for' any 'in' node=any ':' any* >\n        | comp_for< 'for' any 'in' node=any any* >\n     \"\"\"\np1 = \"\"\"\npower<\n    ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'sum' |\n      'any' | 'all' | 'enumerate' | (any* trailer< '.' 'join' >) )\n    trailer< '(' node=any ')' >\n    any*\n>\n\"\"\"\np2 = \"\"\"\npower<\n    ( 'sorted' | 'enumerate' )\n    trailer< '(' arglist<node=any any*> ')' >\n    any*\n>\n\"\"\"\npats_built = False\n\n\ndef in_special_context(node):\n  \"\"\" Returns true if node is in an environment where all that is required\n      of it is being iterable (ie, it doesn't matter if it returns a list\n      or an iterator).\n      See test_map_nochange in test_fixers.py for some examples and tests.\n  \"\"\"\n  global p0, p1, p2, pats_built\n  if not pats_built:\n    p0 = patcomp.compile_pattern(p0)\n    p1 = patcomp.compile_pattern(p1)\n    p2 = patcomp.compile_pattern(p2)\n    pats_built = True\n  patterns = [p0, p1, p2]\n  for pattern, parent in zip(patterns, attr_chain(node, 'parent')):\n    results = {}\n    if pattern.match(parent, results) and results['node'] is node:\n      return True\n  return False\n\n\ndef is_probably_builtin(node):\n  \"\"\"Check that something isn't an attribute or function name etc.\"\"\"\n  prev = node.prev_sibling\n  if prev is not None and prev.type == token.DOT:\n    # Attribute lookup.\n    return False\n  parent = node.parent\n  if parent.type in (syms.funcdef, syms.classdef):\n    return False\n  if parent.type == syms.expr_stmt and parent.children[0] is node:\n    # Assignment.\n    return False\n  if parent.type == syms.parameters or (parent.type == syms.typedargslist and (\n      (prev is not None and prev.type == token.COMMA) or\n      parent.children[0] is node)):\n    # The name of an argument.\n    return False\n  return True\n\n\ndef find_indentation(node):\n  \"\"\"Find the indentation of *node*.\"\"\"\n  while node is not None:\n    if node.type == syms.suite and len(node.children) > 2:\n      indent = node.children[1]\n      if indent.type == token.INDENT:\n        return indent.value\n    node = node.parent\n  return ''\n\n\n###########################################################\n# The following functions are to find bindings in a suite\n###########################################################\n\n\ndef make_suite(node):\n  if node.type == syms.suite:\n    return node\n  node = node.clone()\n  parent, node.parent = node.parent, None\n  suite = Node(syms.suite, [node])\n  suite.parent = parent\n  return suite\n\n\ndef find_root(node):\n  \"\"\"Find the top level namespace.\"\"\"\n  # Scamper up to the top level namespace\n  while node.type != syms.file_input:\n    node = node.parent\n    if not node:\n      raise ValueError('root found before file_input node was found.')\n  return node\n\n\ndef does_tree_import(package, name, node):\n  \"\"\" Returns true if name is imported from package at the\n      top level of the tree which node belongs to.\n      To cover the case of an import like 'import foo', use\n      None for the package and 'foo' for the name.\n  \"\"\"\n  binding = find_binding(name, find_root(node), package)\n  return bool(binding)\n\n\ndef is_import(node):\n  \"\"\"Returns true if the node is an import statement.\"\"\"\n  return node.type in (syms.import_name, syms.import_from)\n\n\ndef touch_import(package, name, node):\n  \"\"\" Works like `does_tree_import` but adds an import statement\n      if it was not imported. \"\"\"\n\n  def is_import_stmt(node):\n    return (node.type == syms.simple_stmt and node.children and\n            is_import(node.children[0]))\n\n  root = find_root(node)\n\n  if does_tree_import(package, name, root):\n    return\n\n  # figure out where to insert the new import.  First try to find\n  # the first import and then skip to the last one.\n  insert_pos = offset = 0\n  for idx, node in enumerate(root.children):\n    if not is_import_stmt(node):\n      continue\n    for offset, node2 in enumerate(root.children[idx:]):\n      if not is_import_stmt(node2):\n        break\n    insert_pos = idx + offset\n    break\n\n  # if there are no imports where we can insert, find the docstring.\n  # if that also fails, we stick to the beginning of the file\n  if insert_pos == 0:\n    for idx, node in enumerate(root.children):\n      if (node.type == syms.simple_stmt and node.children and\n          node.children[0].type == token.STRING):\n        insert_pos = idx + 1\n        break\n\n  if package is None:\n    import_ = Node(\n        syms.import_name,\n        [Leaf(token.NAME, 'import'),\n         Leaf(token.NAME, name, prefix=' ')])\n  else:\n    import_ = FromImport(package, [Leaf(token.NAME, name, prefix=' ')])\n\n  children = [import_, Newline()]\n  root.insert_child(insert_pos, Node(syms.simple_stmt, children))\n\n\n_def_syms = {syms.classdef, syms.funcdef}\n\n\ndef find_binding(name, node, package=None):\n  \"\"\" Returns the node which binds variable name, otherwise None.\n      If optional argument package is supplied, only imports will\n      be returned.\n      See test cases for examples.\n  \"\"\"\n  for child in node.children:\n    ret = None\n    if child.type == syms.for_stmt:\n      if _find(name, child.children[1]):\n        return child\n      n = find_binding(name, make_suite(child.children[-1]), package)\n      if n:\n        ret = n\n    elif child.type in (syms.if_stmt, syms.while_stmt):\n      n = find_binding(name, make_suite(child.children[-1]), package)\n      if n:\n        ret = n\n    elif child.type == syms.try_stmt:\n      n = find_binding(name, make_suite(child.children[2]), package)\n      if n:\n        ret = n\n      else:\n        for i, kid in enumerate(child.children[3:]):\n          if kid.type == token.COLON and kid.value == ':':\n            # i+3 is the colon, i+4 is the suite\n            n = find_binding(name, make_suite(child.children[i + 4]), package)\n            if n:\n              ret = n\n    elif child.type in _def_syms and child.children[1].value == name:\n      ret = child\n    elif _is_import_binding(child, name, package):\n      ret = child\n    elif child.type == syms.simple_stmt:\n      ret = find_binding(name, child, package)\n    elif child.type == syms.expr_stmt:\n      if _find(name, child.children[0]):\n        ret = child\n\n    if ret:\n      if not package:\n        return ret\n      if is_import(ret):\n        return ret\n  return None\n\n\n_block_syms = {syms.funcdef, syms.classdef, syms.trailer}\n\n\ndef _find(name, node):\n  nodes = [node]\n  while nodes:\n    node = nodes.pop()\n    if node.type > 256 and node.type not in _block_syms:\n      nodes.extend(node.children)\n    elif node.type == token.NAME and node.value == name:\n      return node\n  return None\n\n\ndef _is_import_binding(node, name, package=None):\n  \"\"\" Will return node if node will import name, or node\n      will import * from package.  None is returned otherwise.\n      See test cases for examples.\n  \"\"\"\n  if node.type == syms.import_name and not package:\n    imp = node.children[1]\n    if imp.type == syms.dotted_as_names:\n      for child in imp.children:\n        if child.type == syms.dotted_as_name:\n          if child.children[2].value == name:\n            return node\n        elif child.type == token.NAME and child.value == name:\n          return node\n    elif imp.type == syms.dotted_as_name:\n      last = imp.children[-1]\n      if last.type == token.NAME and last.value == name:\n        return node\n    elif imp.type == token.NAME and imp.value == name:\n      return node\n  elif node.type == syms.import_from:\n    # str(...) is used to make life easier here, because\n    # from a.b import parses to ['import', ['a', '.', 'b'], ...]\n    if package and str(node.children[1]).strip() != package:\n      return None\n    n = node.children[3]\n    if package and _find('as', n):\n      # See test_from_import_as for explanation\n      return None\n    elif n.type == syms.import_as_names and _find(name, n):\n      return node\n    elif n.type == syms.import_as_name:\n      child = n.children[2]\n      if child.type == token.NAME and child.value == name:\n        return node\n    elif n.type == token.NAME and n.value == name:\n      return node\n    elif package and n.type == token.STAR:\n      return node\n  return None\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/patcomp.py",
    "content": "# Copyright 2006 Google, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\"\"\"Pattern compiler.\n\nThe grammar is taken from PatternGrammar.txt.\n\nThe compiler compiles a pattern to a pytree.*Pattern instance.\n\"\"\"\n\n__author__ = 'Guido van Rossum <guido@python.org>'\n\n# Python imports\nimport io\n\n# Really local imports\nfrom . import pygram\nfrom . import pytree\n# Fairly local imports\nfrom .pgen2 import driver\nfrom .pgen2 import grammar\nfrom .pgen2 import literals\nfrom .pgen2 import parse\nfrom .pgen2 import token\nfrom .pgen2 import tokenize\n\n\nclass PatternSyntaxError(Exception):\n  pass\n\n\ndef tokenize_wrapper(input):\n  \"\"\"Tokenizes a string suppressing significant whitespace.\"\"\"\n  skip = {token.NEWLINE, token.INDENT, token.DEDENT}\n  tokens = tokenize.generate_tokens(io.StringIO(input).readline)\n  for quintuple in tokens:\n    type, value, start, end, line_text = quintuple\n    if type not in skip:\n      yield quintuple\n\n\nclass PatternCompiler(object):\n\n  def __init__(self, grammar_file=None):\n    \"\"\"Initializer.\n\n        Takes an optional alternative filename for the pattern grammar.\n        \"\"\"\n    if grammar_file is None:\n      self.grammar = pygram.pattern_grammar\n      self.syms = pygram.pattern_symbols\n    else:\n      self.grammar = driver.load_grammar(grammar_file)\n      self.syms = pygram.Symbols(self.grammar)\n    self.pygrammar = pygram.python_grammar\n    self.pysyms = pygram.python_symbols\n    self.driver = driver.Driver(self.grammar, convert=pattern_convert)\n\n  def compile_pattern(self, input, debug=False, with_tree=False):\n    \"\"\"Compiles a pattern string to a nested pytree.*Pattern object.\"\"\"\n    tokens = tokenize_wrapper(input)\n    try:\n      root = self.driver.parse_tokens(tokens, debug=debug)\n    except parse.ParseError as e:\n      raise PatternSyntaxError(str(e)) from None\n    if with_tree:\n      return self.compile_node(root), root\n    else:\n      return self.compile_node(root)\n\n  def compile_node(self, node):\n    \"\"\"Compiles a node, recursively.\n\n        This is one big switch on the node type.\n        \"\"\"\n    # XXX Optimize certain Wildcard-containing-Wildcard patterns\n    # that can be merged\n    if node.type == self.syms.Matcher:\n      node = node.children[0]  # Avoid unneeded recursion\n\n    if node.type == self.syms.Alternatives:\n      # Skip the odd children since they are just '|' tokens\n      alts = [self.compile_node(ch) for ch in node.children[::2]]\n      if len(alts) == 1:\n        return alts[0]\n      p = pytree.WildcardPattern([[a] for a in alts], min=1, max=1)\n      return p.optimize()\n\n    if node.type == self.syms.Alternative:\n      units = [self.compile_node(ch) for ch in node.children]\n      if len(units) == 1:\n        return units[0]\n      p = pytree.WildcardPattern([units], min=1, max=1)\n      return p.optimize()\n\n    if node.type == self.syms.NegatedUnit:\n      pattern = self.compile_basic(node.children[1:])\n      p = pytree.NegatedPattern(pattern)\n      return p.optimize()\n\n    assert node.type == self.syms.Unit\n\n    name = None\n    nodes = node.children\n    if len(nodes) >= 3 and nodes[1].type == token.EQUAL:\n      name = nodes[0].value\n      nodes = nodes[2:]\n    repeat = None\n    if len(nodes) >= 2 and nodes[-1].type == self.syms.Repeater:\n      repeat = nodes[-1]\n      nodes = nodes[:-1]\n\n    # Now we've reduced it to: STRING | NAME [Details] | (...) | [...]\n    pattern = self.compile_basic(nodes, repeat)\n\n    if repeat is not None:\n      assert repeat.type == self.syms.Repeater\n      children = repeat.children\n      child = children[0]\n      if child.type == token.STAR:\n        min = 0\n        max = pytree.HUGE\n      elif child.type == token.PLUS:\n        min = 1\n        max = pytree.HUGE\n      elif child.type == token.LBRACE:\n        assert children[-1].type == token.RBRACE\n        assert len(children) in (3, 5)\n        min = max = self.get_int(children[1])\n        if len(children) == 5:\n          max = self.get_int(children[3])\n      else:\n        assert False\n      if min != 1 or max != 1:\n        pattern = pattern.optimize()\n        pattern = pytree.WildcardPattern([[pattern]], min=min, max=max)\n\n    if name is not None:\n      pattern.name = name\n    return pattern.optimize()\n\n  def compile_basic(self, nodes, repeat=None):\n    # Compile STRING | NAME [Details] | (...) | [...]\n    assert len(nodes) >= 1\n    node = nodes[0]\n    if node.type == token.STRING:\n      value = str(literals.evalString(node.value))\n      return pytree.LeafPattern(_type_of_literal(value), value)\n    elif node.type == token.NAME:\n      value = node.value\n      if value.isupper():\n        if value not in TOKEN_MAP:\n          raise PatternSyntaxError('Invalid token: %r' % value)\n        if nodes[1:]:\n          raise PatternSyntaxError(\"Can't have details for token\")\n        return pytree.LeafPattern(TOKEN_MAP[value])\n      else:\n        if value == 'any':\n          type = None\n        elif not value.startswith('_'):\n          type = getattr(self.pysyms, value, None)\n          if type is None:\n            raise PatternSyntaxError('Invalid symbol: %r' % value)\n        if nodes[1:]:  # Details present\n          content = [self.compile_node(nodes[1].children[1])]\n        else:\n          content = None\n        return pytree.NodePattern(type, content)\n    elif node.value == '(':\n      return self.compile_node(nodes[1])\n    elif node.value == '[':\n      assert repeat is None\n      subpattern = self.compile_node(nodes[1])\n      return pytree.WildcardPattern([[subpattern]], min=0, max=1)\n    assert False, node\n\n  def get_int(self, node):\n    assert node.type == token.NUMBER\n    return int(node.value)\n\n\n# Map named tokens to the type value for a LeafPattern\nTOKEN_MAP = {\n    'NAME': token.NAME,\n    'STRING': token.STRING,\n    'NUMBER': token.NUMBER,\n    'TOKEN': None\n}\n\n\ndef _type_of_literal(value):\n  if value[0].isalpha():\n    return token.NAME\n  elif value in grammar.opmap:\n    return grammar.opmap[value]\n  else:\n    return None\n\n\ndef pattern_convert(grammar, raw_node_info):\n  \"\"\"Converts raw node information to a Node or Leaf instance.\"\"\"\n  type, value, context, children = raw_node_info\n  if children or type in grammar.number2symbol:\n    return pytree.Node(type, children, context=context)\n  else:\n    return pytree.Leaf(type, value, context=context)\n\n\ndef compile_pattern(pattern):\n  return PatternCompiler().compile_pattern(pattern)\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/pgen2/__init__.py",
    "content": "# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\"\"\"The pgen2 package.\"\"\"\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/pgen2/conv.py",
    "content": "# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\"\"\"Convert graminit.[ch] spit out by pgen to Python code.\n\nPgen is the Python parser generator.  It is useful to quickly create a\nparser from a grammar file in Python's grammar notation.  But I don't\nwant my parsers to be written in C (yet), so I'm translating the\nparsing tables to Python data structures and writing a Python parse\nengine.\n\nNote that the token numbers are constants determined by the standard\nPython tokenizer.  The standard token module defines these numbers and\ntheir names (the names are not used much).  The token numbers are\nhardcoded into the Python tokenizer and into pgen.  A Python\nimplementation of the Python tokenizer is also available, in the\nstandard tokenize module.\n\nOn the other hand, symbol numbers (representing the grammar's\nnon-terminals) are assigned by pgen based on the actual grammar\ninput.\n\nNote: this module is pretty much obsolete; the pgen module generates\nequivalent grammar tables directly from the Grammar.txt input file\nwithout having to invoke the Python pgen C program.\n\n\"\"\"\n\n# Python imports\nimport re\n\n# Local imports\nfrom pgen2 import grammar\nfrom pgen2 import token\n\n\nclass Converter(grammar.Grammar):\n  \"\"\"Grammar subclass that reads classic pgen output files.\n\n    The run() method reads the tables as produced by the pgen parser\n    generator, typically contained in two C files, graminit.h and\n    graminit.c.  The other methods are for internal use only.\n\n    See the base class for more documentation.\n\n    \"\"\"\n\n  def run(self, graminit_h, graminit_c):\n    \"\"\"Load the grammar tables from the text files written by pgen.\"\"\"\n    self.parse_graminit_h(graminit_h)\n    self.parse_graminit_c(graminit_c)\n    self.finish_off()\n\n  def parse_graminit_h(self, filename):\n    \"\"\"Parse the .h file written by pgen.  (Internal)\n\n        This file is a sequence of #define statements defining the\n        nonterminals of the grammar as numbers.  We build two tables\n        mapping the numbers to names and back.\n\n        \"\"\"\n    try:\n      f = open(filename)\n    except OSError as err:\n      print(\"Can't open %s: %s\" % (filename, err))\n      return False\n    self.symbol2number = {}\n    self.number2symbol = {}\n    lineno = 0\n    for line in f:\n      lineno += 1\n      mo = re.match(r'^#define\\s+(\\w+)\\s+(\\d+)$', line)\n      if not mo and line.strip():\n        print(\"%s(%s): can't parse %s\" % (filename, lineno, line.strip()))\n      else:\n        symbol, number = mo.groups()\n        number = int(number)\n        assert symbol not in self.symbol2number\n        assert number not in self.number2symbol\n        self.symbol2number[symbol] = number\n        self.number2symbol[number] = symbol\n    return True\n\n  def parse_graminit_c(self, filename):\n    \"\"\"Parse the .c file written by pgen.  (Internal)\n\n        The file looks as follows.  The first two lines are always this:\n\n        #include \"pgenheaders.h\"\n        #include \"grammar.h\"\n\n        After that come four blocks:\n\n        1) one or more state definitions\n        2) a table defining dfas\n        3) a table defining labels\n        4) a struct defining the grammar\n\n        A state definition has the following form:\n        - one or more arc arrays, each of the form:\n          static arc arcs_<n>_<m>[<k>] = {\n                  {<i>, <j>},\n                  ...\n          };\n        - followed by a state array, of the form:\n          static state states_<s>[<t>] = {\n                  {<k>, arcs_<n>_<m>},\n                  ...\n          };\n\n        \"\"\"\n    try:\n      f = open(filename)\n    except OSError as err:\n      print(\"Can't open %s: %s\" % (filename, err))\n      return False\n    # The code below essentially uses f's iterator-ness!\n    lineno = 0\n\n    # Expect the two #include lines\n    lineno, line = lineno + 1, next(f)\n    assert line == '#include \"pgenheaders.h\"\\n', (lineno, line)\n    lineno, line = lineno + 1, next(f)\n    assert line == '#include \"grammar.h\"\\n', (lineno, line)\n\n    # Parse the state definitions\n    lineno, line = lineno + 1, next(f)\n    allarcs = {}\n    states = []\n    while line.startswith('static arc '):\n      while line.startswith('static arc '):\n        mo = re.match(r'static arc arcs_(\\d+)_(\\d+)\\[(\\d+)\\] = {$', line)\n        assert mo, (lineno, line)\n        n, m, k = list(map(int, mo.groups()))\n        arcs = []\n        for _ in range(k):\n          lineno, line = lineno + 1, next(f)\n          mo = re.match(r'\\s+{(\\d+), (\\d+)},$', line)\n          assert mo, (lineno, line)\n          i, j = list(map(int, mo.groups()))\n          arcs.append((i, j))\n        lineno, line = lineno + 1, next(f)\n        assert line == '};\\n', (lineno, line)\n        allarcs[(n, m)] = arcs\n        lineno, line = lineno + 1, next(f)\n      mo = re.match(r'static state states_(\\d+)\\[(\\d+)\\] = {$', line)\n      assert mo, (lineno, line)\n      s, t = list(map(int, mo.groups()))\n      assert s == len(states), (lineno, line)\n      state = []\n      for _ in range(t):\n        lineno, line = lineno + 1, next(f)\n        mo = re.match(r'\\s+{(\\d+), arcs_(\\d+)_(\\d+)},$', line)\n        assert mo, (lineno, line)\n        k, n, m = list(map(int, mo.groups()))\n        arcs = allarcs[n, m]\n        assert k == len(arcs), (lineno, line)\n        state.append(arcs)\n      states.append(state)\n      lineno, line = lineno + 1, next(f)\n      assert line == '};\\n', (lineno, line)\n      lineno, line = lineno + 1, next(f)\n    self.states = states\n\n    # Parse the dfas\n    dfas = {}\n    mo = re.match(r'static dfa dfas\\[(\\d+)\\] = {$', line)\n    assert mo, (lineno, line)\n    ndfas = int(mo.group(1))\n    for i in range(ndfas):\n      lineno, line = lineno + 1, next(f)\n      mo = re.match(r'\\s+{(\\d+), \"(\\w+)\", (\\d+), (\\d+), states_(\\d+),$', line)\n      assert mo, (lineno, line)\n      symbol = mo.group(2)\n      number, x, y, z = list(map(int, mo.group(1, 3, 4, 5)))\n      assert self.symbol2number[symbol] == number, (lineno, line)\n      assert self.number2symbol[number] == symbol, (lineno, line)\n      assert x == 0, (lineno, line)\n      state = states[z]\n      assert y == len(state), (lineno, line)\n      lineno, line = lineno + 1, next(f)\n      mo = re.match(r'\\s+(\"(?:\\\\\\d\\d\\d)*\")},$', line)\n      assert mo, (lineno, line)\n      first = {}\n      rawbitset = eval(mo.group(1))\n      for i, c in enumerate(rawbitset):\n        byte = ord(c)\n        for j in range(8):\n          if byte & (1 << j):\n            first[i * 8 + j] = 1\n      dfas[number] = (state, first)\n    lineno, line = lineno + 1, next(f)\n    assert line == '};\\n', (lineno, line)\n    self.dfas = dfas\n\n    # Parse the labels\n    labels = []\n    lineno, line = lineno + 1, next(f)\n    mo = re.match(r'static label labels\\[(\\d+)\\] = {$', line)\n    assert mo, (lineno, line)\n    nlabels = int(mo.group(1))\n    for i in range(nlabels):\n      lineno, line = lineno + 1, next(f)\n      mo = re.match(r'\\s+{(\\d+), (0|\"\\w+\")},$', line)\n      assert mo, (lineno, line)\n      x, y = mo.groups()\n      x = int(x)\n      if y == '0':\n        y = None\n      else:\n        y = eval(y)\n      labels.append((x, y))\n    lineno, line = lineno + 1, next(f)\n    assert line == '};\\n', (lineno, line)\n    self.labels = labels\n\n    # Parse the grammar struct\n    lineno, line = lineno + 1, next(f)\n    assert line == 'grammar _PyParser_Grammar = {\\n', (lineno, line)\n    lineno, line = lineno + 1, next(f)\n    mo = re.match(r'\\s+(\\d+),$', line)\n    assert mo, (lineno, line)\n    ndfas = int(mo.group(1))\n    assert ndfas == len(self.dfas)\n    lineno, line = lineno + 1, next(f)\n    assert line == '\\tdfas,\\n', (lineno, line)\n    lineno, line = lineno + 1, next(f)\n    mo = re.match(r'\\s+{(\\d+), labels},$', line)\n    assert mo, (lineno, line)\n    nlabels = int(mo.group(1))\n    assert nlabels == len(self.labels), (lineno, line)\n    lineno, line = lineno + 1, next(f)\n    mo = re.match(r'\\s+(\\d+)$', line)\n    assert mo, (lineno, line)\n    start = int(mo.group(1))\n    assert start in self.number2symbol, (lineno, line)\n    self.start = start\n    lineno, line = lineno + 1, next(f)\n    assert line == '};\\n', (lineno, line)\n    try:\n      lineno, line = lineno + 1, next(f)\n    except StopIteration:\n      pass\n    else:\n      assert 0, (lineno, line)\n\n  def finish_off(self):\n    \"\"\"Create additional useful structures.  (Internal).\"\"\"\n    self.keywords = {}  # map from keyword strings to arc labels\n    self.tokens = {}  # map from numeric token values to arc labels\n    for ilabel, (type, value) in enumerate(self.labels):\n      if type == token.NAME and value is not None:\n        self.keywords[value] = ilabel\n      elif value is None:\n        self.tokens[type] = ilabel\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/pgen2/driver.py",
    "content": "# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\n# Modifications:\n# Copyright 2006 Google, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\"\"\"Parser driver.\n\nThis provides a high-level interface to parse a file into a syntax tree.\n\n\"\"\"\n\n__author__ = 'Guido van Rossum <guido@python.org>'\n\n__all__ = ['Driver', 'load_grammar']\n\nimport io\nimport logging\nimport os\nimport pkgutil\nimport sys\n# Python imports\nfrom contextlib import contextmanager\nfrom dataclasses import dataclass\nfrom dataclasses import field\nfrom pathlib import Path\nfrom typing import Any\nfrom typing import Iterator\nfrom typing import List\nfrom typing import Optional\n\nfrom platformdirs import user_cache_dir\n\nfrom yapf._version import __version__ as yapf_version\n\n# Pgen imports\nfrom . import grammar\nfrom . import parse\nfrom . import pgen\nfrom . import token\nfrom . import tokenize\n\n\n@dataclass\nclass ReleaseRange:\n  start: int\n  end: Optional[int] = None\n  tokens: List[Any] = field(default_factory=list)\n\n  def lock(self) -> None:\n    total_eaten = len(self.tokens)\n    self.end = self.start + total_eaten\n\n\nclass TokenProxy:\n\n  def __init__(self, generator: Any) -> None:\n    self._tokens = generator\n    self._counter = 0\n    self._release_ranges: List[ReleaseRange] = []\n\n  @contextmanager\n  def release(self) -> Iterator['TokenProxy']:\n    release_range = ReleaseRange(self._counter)\n    self._release_ranges.append(release_range)\n    try:\n      yield self\n    finally:\n      # Lock the last release range to the final position that\n      # has been eaten.\n      release_range.lock()\n\n  def eat(self, point: int) -> Any:\n    eaten_tokens = self._release_ranges[-1].tokens\n    if point < len(eaten_tokens):\n      return eaten_tokens[point]\n    else:\n      while point >= len(eaten_tokens):\n        token = next(self._tokens)\n        eaten_tokens.append(token)\n      return token\n\n  def __iter__(self) -> 'TokenProxy':\n    return self\n\n  def __next__(self) -> Any:\n    # If the current position is already compromised (looked up)\n    # return the eaten token, if not just go further on the given\n    # token producer.\n    for release_range in self._release_ranges:\n      assert release_range.end is not None\n\n      start, end = release_range.start, release_range.end\n      if start <= self._counter < end:\n        token = release_range.tokens[self._counter - start]\n        break\n    else:\n      token = next(self._tokens)\n    self._counter += 1\n    return token\n\n  def can_advance(self, to: int) -> bool:\n    # Try to eat, fail if it can't. The eat operation is cached\n    # so there wont be any additional cost of eating here\n    try:\n      self.eat(to)\n    except StopIteration:\n      return False\n    else:\n      return True\n\n\nclass Driver(object):\n\n  def __init__(self, grammar, convert=None, logger=None):\n    self.grammar = grammar\n    if logger is None:\n      logger = logging.getLogger()\n    self.logger = logger\n    self.convert = convert\n\n  def parse_tokens(self, tokens, debug=False):\n    \"\"\"Parse a series of tokens and return the syntax tree.\"\"\"\n    # XXX Move the prefix computation into a wrapper around tokenize.\n    p = parse.Parser(self.grammar, self.convert)\n    proxy = TokenProxy(tokens)\n    p.setup(proxy=proxy)\n    lineno = 1\n    column = 0\n    type = value = start = end = line_text = None\n    prefix = ''\n    for quintuple in proxy:\n      type, value, start, end, line_text = quintuple\n      if start != (lineno, column):\n        assert (lineno, column) <= start, ((lineno, column), start)\n        s_lineno, s_column = start\n        if lineno < s_lineno:\n          prefix += '\\n' * (s_lineno - lineno)\n          lineno = s_lineno\n          column = 0\n        if column < s_column:\n          prefix += line_text[column:s_column]\n          column = s_column\n      if type in (tokenize.COMMENT, tokenize.NL):\n        prefix += value\n        lineno, column = end\n        if value.endswith('\\n'):\n          lineno += 1\n          column = 0\n        continue\n      if type == token.OP:\n        type = grammar.opmap[value]\n      if debug:\n        self.logger.debug('%s %r (prefix=%r)', token.tok_name[type], value,\n                          prefix)\n      if p.addtoken(type, value, (prefix, start)):\n        if debug:\n          self.logger.debug('Stop.')\n        break\n      prefix = ''\n      lineno, column = end\n      if value.endswith('\\n'):\n        lineno += 1\n        column = 0\n    else:\n      # We never broke out -- EOF is too soon (how can this happen???)\n      raise parse.ParseError('incomplete input', type, value, (prefix, start))\n    return p.rootnode\n\n  def parse_stream_raw(self, stream, debug=False):\n    \"\"\"Parse a stream and return the syntax tree.\"\"\"\n    tokens = tokenize.generate_tokens(stream.readline)\n    return self.parse_tokens(tokens, debug)\n\n  def parse_stream(self, stream, debug=False):\n    \"\"\"Parse a stream and return the syntax tree.\"\"\"\n    return self.parse_stream_raw(stream, debug)\n\n  def parse_file(self, filename, encoding=None, debug=False):\n    \"\"\"Parse a file and return the syntax tree.\"\"\"\n    with io.open(filename, 'r', encoding=encoding) as stream:\n      return self.parse_stream(stream, debug)\n\n  def parse_string(self, text, debug=False):\n    \"\"\"Parse a string and return the syntax tree.\"\"\"\n    tokens = tokenize.generate_tokens(io.StringIO(text).readline)\n    return self.parse_tokens(tokens, debug)\n\n\ndef _generate_pickle_name(gt):\n  # type:(str) -> str\n  \"\"\"Get the filepath to write a pickle file to\n  given the path of a grammar textfile.\n\n  The returned filepath should be in a user-specific cache directory.\n\n  Args:\n      gt (str): path to grammar text file\n\n  Returns:\n      str: path to pickle file\n  \"\"\"\n\n  grammar_textfile_name = os.path.basename(gt)\n  head, tail = os.path.splitext(grammar_textfile_name)\n  if tail == '.txt':\n    tail = ''\n  cache_dir = user_cache_dir(\n      appname='YAPF', appauthor='Google', version=yapf_version)\n  return cache_dir + os.sep + head + tail + '-py' + '.'.join(\n      map(str, sys.version_info)) + '.pickle'\n\n\ndef load_grammar(gt='Grammar.txt',\n                 gp=None,\n                 save=True,\n                 force=False,\n                 logger=None):\n  # type:(str, str | None, bool, bool, logging.Logger | None) -> grammar.Grammar\n  \"\"\"Load the grammar (maybe from a pickle).\"\"\"\n  if logger is None:\n    logger = logging.getLogger()\n  gp = _generate_pickle_name(gt) if gp is None else gp\n  grammar_text = gt\n  try:\n    newer = _newer(gp, gt)\n  except OSError as err:\n    logger.debug('OSError, could not check if newer: %s', err.args)\n    newer = True\n  if not os.path.exists(gt):\n    # Assume package data\n    gt_basename = os.path.basename(gt)\n    pd = pkgutil.get_data('yapf_third_party._ylib2to3', gt_basename)\n    if pd is None:\n      raise RuntimeError('Failed to load grammer %s from package' % gt_basename)\n    grammar_text = io.StringIO(pd.decode(encoding='utf-8'))\n  if force or not newer:\n    g = pgen.generate_grammar(grammar_text)\n    if save:\n      try:\n        Path(gp).parent.mkdir(parents=True, exist_ok=True)\n        g.dump(gp)\n      except OSError:\n        # Ignore error, caching is not vital.\n        pass\n  else:\n    g = grammar.Grammar()\n    g.load(gp)\n  return g\n\n\ndef _newer(a, b):\n  \"\"\"Inquire whether file a was written since file b.\"\"\"\n  if not os.path.exists(a):\n    return False\n  if not os.path.exists(b):\n    return True\n  return os.path.getmtime(a) >= os.path.getmtime(b)\n\n\ndef load_packaged_grammar(package, grammar_source):\n  \"\"\"Normally, loads a pickled grammar by doing\n        pkgutil.get_data(package, pickled_grammar)\n    where *pickled_grammar* is computed from *grammar_source* by adding the\n    Python version and using a ``.pickle`` extension.\n\n    However, if *grammar_source* is an extant file, load_grammar(grammar_source)\n    is called instead. This facilitates using a packaged grammar file when needed\n    but preserves load_grammar's automatic regeneration behavior when possible.\n\n    \"\"\"  # noqa: E501\n  if os.path.isfile(grammar_source):\n    return load_grammar(grammar_source)\n  pickled_name = _generate_pickle_name(os.path.basename(grammar_source))\n  data = pkgutil.get_data(package, pickled_name)\n  g = grammar.Grammar()\n  g.loads(data)\n  return g\n\n\ndef main(*args):\n  \"\"\"Main program, when run as a script: produce grammar pickle files.\n\n    Calls load_grammar for each argument, a path to a grammar text file.\n    \"\"\"\n  if not args:\n    args = sys.argv[1:]\n  logging.basicConfig(\n      level=logging.INFO, stream=sys.stdout, format='%(message)s')\n  for gt in args:\n    load_grammar(gt, save=True, force=True)\n  return True\n\n\nif __name__ == '__main__':\n  sys.exit(int(not main()))\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/pgen2/grammar.py",
    "content": "# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\"\"\"This module defines the data structures used to represent a grammar.\n\nThese are a bit arcane because they are derived from the data\nstructures used by Python's 'pgen' parser generator.\n\nThere's also a table here mapping operators to their names in the\ntoken module; the Python tokenize module reports all operators as the\nfallback token code OP, but the parser needs the actual token code.\n\n\"\"\"\n\n# Python imports\nimport os\nimport pickle\nimport tempfile\n\n# Local imports\nfrom . import token\n\n\nclass Grammar(object):\n  \"\"\"Pgen parsing tables conversion class.\n\n    Once initialized, this class supplies the grammar tables for the\n    parsing engine implemented by parse.py.  The parsing engine\n    accesses the instance variables directly.  The class here does not\n    provide initialization of the tables; several subclasses exist to\n    do this (see the conv and pgen modules).\n\n    The load() method reads the tables from a pickle file, which is\n    much faster than the other ways offered by subclasses.  The pickle\n    file is written by calling dump() (after loading the grammar\n    tables using a subclass).  The report() method prints a readable\n    representation of the tables to stdout, for debugging.\n\n    The instance variables are as follows:\n\n    symbol2number -- a dict mapping symbol names to numbers.  Symbol\n                     numbers are always 256 or higher, to distinguish\n                     them from token numbers, which are between 0 and\n                     255 (inclusive).\n\n    number2symbol -- a dict mapping numbers to symbol names;\n                     these two are each other's inverse.\n\n    states        -- a list of DFAs, where each DFA is a list of\n                     states, each state is a list of arcs, and each\n                     arc is a (i, j) pair where i is a label and j is\n                     a state number.  The DFA number is the index into\n                     this list.  (This name is slightly confusing.)\n                     Final states are represented by a special arc of\n                     the form (0, j) where j is its own state number.\n\n    dfas          -- a dict mapping symbol numbers to (DFA, first)\n                     pairs, where DFA is an item from the states list\n                     above, and first is a set of tokens that can\n                     begin this grammar rule (represented by a dict\n                     whose values are always 1).\n\n    labels        -- a list of (x, y) pairs where x is either a token\n                     number or a symbol number, and y is either None\n                     or a string; the strings are keywords.  The label\n                     number is the index in this list; label numbers\n                     are used to mark state transitions (arcs) in the\n                     DFAs.\n\n    start         -- the number of the grammar's start symbol.\n\n    keywords      -- a dict mapping keyword strings to arc labels.\n\n    tokens        -- a dict mapping token numbers to arc labels.\n\n    \"\"\"\n\n  def __init__(self):\n    self.symbol2number = {}\n    self.number2symbol = {}\n    self.states = []\n    self.dfas = {}\n    self.labels = [(0, 'EMPTY')]\n    self.keywords = {}\n    self.soft_keywords = {}\n    self.tokens = {}\n    self.symbol2label = {}\n    self.start = 256\n\n  def dump(self, filename):\n    \"\"\"Dump the grammar tables to a pickle file.\"\"\"\n    # NOTE:\n    # - We're writing a tempfile first so that there is no chance\n    #   for someone to read a half-written file from this very spot\n    #   while we're were not done writing.\n    # - We're using ``os.rename`` to sure not copy data around (which\n    #   would get us back to square one with a reading-half-written file\n    #   race condition).\n    # - We're making the tempfile go to the same directory as the eventual\n    #   target ``filename`` so that there is no chance of failing from\n    #   cross-file-system renames in ``os.rename``.\n    # - We're using the same prefix and suffix for the tempfile so if we\n    #   ever have to leave a tempfile around for failure of deletion,\n    #   it will have a reasonable filename extension and its name will help\n    #   explain is nature.\n    tempfile_dir = os.path.dirname(filename)\n    tempfile_prefix, tempfile_suffix = os.path.splitext(filename)\n    with tempfile.NamedTemporaryFile(\n        mode='wb',\n        suffix=tempfile_suffix,\n        prefix=tempfile_prefix,\n        dir=tempfile_dir,\n        delete=False) as f:\n      pickle.dump(self.__dict__, f.file, pickle.HIGHEST_PROTOCOL)\n      try:\n        os.rename(f.name, filename)\n      except OSError:\n        # This makes sure that we do not leave the tempfile around\n        # unless we have to...\n        try:\n          os.remove(f.name)\n        except OSError:\n          pass\n        raise\n\n  def load(self, filename):\n    \"\"\"Load the grammar tables from a pickle file.\"\"\"\n    with open(filename, 'rb') as f:\n      d = pickle.load(f)\n    self.__dict__.update(d)\n\n  def loads(self, pkl):\n    \"\"\"Load the grammar tables from a pickle bytes object.\"\"\"\n    self.__dict__.update(pickle.loads(pkl))\n\n  def copy(self):\n    \"\"\"\n        Copy the grammar.\n        \"\"\"\n    new = self.__class__()\n    for dict_attr in ('symbol2number', 'number2symbol', 'dfas', 'keywords',\n                      'soft_keywords', 'tokens', 'symbol2label'):\n      setattr(new, dict_attr, getattr(self, dict_attr).copy())\n    new.labels = self.labels[:]\n    new.states = self.states[:]\n    new.start = self.start\n    return new\n\n  def report(self):\n    \"\"\"Dump the grammar tables to standard output, for debugging.\"\"\"\n    from pprint import pprint\n    print('s2n')\n    pprint(self.symbol2number)\n    print('n2s')\n    pprint(self.number2symbol)\n    print('states')\n    pprint(self.states)\n    print('dfas')\n    pprint(self.dfas)\n    print('labels')\n    pprint(self.labels)\n    print('start', self.start)\n\n\n# Map from operator to number (since tokenize doesn't do this)\n\nopmap_raw = \"\"\"\n( LPAR\n) RPAR\n[ LSQB\n] RSQB\n: COLON\n, COMMA\n; SEMI\n+ PLUS\n- MINUS\n* STAR\n/ SLASH\n| VBAR\n& AMPER\n< LESS\n> GREATER\n= EQUAL\n. DOT\n% PERCENT\n` BACKQUOTE\n{ LBRACE\n} RBRACE\n@ AT\n@= ATEQUAL\n== EQEQUAL\n!= NOTEQUAL\n<> NOTEQUAL\n<= LESSEQUAL\n>= GREATEREQUAL\n~ TILDE\n^ CIRCUMFLEX\n<< LEFTSHIFT\n>> RIGHTSHIFT\n** DOUBLESTAR\n+= PLUSEQUAL\n-= MINEQUAL\n*= STAREQUAL\n/= SLASHEQUAL\n%= PERCENTEQUAL\n&= AMPEREQUAL\n|= VBAREQUAL\n^= CIRCUMFLEXEQUAL\n<<= LEFTSHIFTEQUAL\n>>= RIGHTSHIFTEQUAL\n**= DOUBLESTAREQUAL\n// DOUBLESLASH\n//= DOUBLESLASHEQUAL\n-> RARROW\n:= COLONEQUAL\n\"\"\"\n\nopmap = {}\nfor line in opmap_raw.splitlines():\n  if line:\n    op, name = line.split()\n    opmap[op] = getattr(token, name)\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/pgen2/literals.py",
    "content": "# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\"\"\"Safely evaluate Python string literals without using eval().\"\"\"\n\nimport re\n\nsimple_escapes = {\n    'a': '\\a',\n    'b': '\\b',\n    'f': '\\f',\n    'n': '\\n',\n    'r': '\\r',\n    't': '\\t',\n    'v': '\\v',\n    \"'\": \"'\",\n    '\"': '\"',\n    '\\\\': '\\\\'\n}\n\n\ndef escape(m):\n  all, tail = m.group(0, 1)\n  assert all.startswith('\\\\')\n  esc = simple_escapes.get(tail)\n  if esc is not None:\n    return esc\n  if tail.startswith('x'):\n    hexes = tail[1:]\n    if len(hexes) < 2:\n      raise ValueError(\"invalid hex string escape ('\\\\%s')\" % tail)\n    try:\n      i = int(hexes, 16)\n    except ValueError:\n      raise ValueError(\"invalid hex string escape ('\\\\%s')\" % tail) from None\n  else:\n    try:\n      i = int(tail, 8)\n    except ValueError:\n      raise ValueError(\"invalid octal string escape ('\\\\%s')\" % tail) from None\n  return chr(i)\n\n\ndef evalString(s):\n  assert s.startswith(\"'\") or s.startswith('\"'), repr(s[:1])\n  q = s[0]\n  if s[:3] == q * 3:\n    q = q * 3\n  assert s.endswith(q), repr(s[-len(q):])\n  assert len(s) >= 2 * len(q)\n  s = s[len(q):-len(q)]\n  return re.sub(r\"\\\\(\\'|\\\"|\\\\|[abfnrtv]|x.{0,2}|[0-7]{1,3})\", escape, s)\n\n\ndef test():\n  for i in range(256):\n    c = chr(i)\n    s = repr(c)\n    e = evalString(s)\n    if e != c:\n      print(i, c, s, e)\n\n\nif __name__ == '__main__':\n  test()\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/pgen2/parse.py",
    "content": "# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\"\"\"Parser engine for the grammar tables generated by pgen.\n\nThe grammar table must be loaded first.\n\nSee Parser/parser.c in the Python distribution for additional info on\nhow this parsing engine works.\n\n\"\"\"\nfrom contextlib import contextmanager\nfrom typing import Any\nfrom typing import Callable\nfrom typing import Dict\nfrom typing import Iterator\nfrom typing import List\nfrom typing import Optional\nfrom typing import Set\nfrom typing import Text\nfrom typing import Tuple\nfrom typing import cast\n\nfrom ..pytree import Context\nfrom ..pytree import RawNode\nfrom ..pytree import convert\n# Local imports\nfrom . import grammar\nfrom . import token\nfrom . import tokenize\n\nDFA = List[List[Tuple[int, int]]]\nDFAS = Tuple[DFA, Dict[int, int]]\n\n# A placeholder node, used when parser is backtracking.\nDUMMY_NODE = (-1, None, None, None)\n\n\ndef stack_copy(\n    stack: List[Tuple[DFAS, int, RawNode]]) -> List[Tuple[DFAS, int, RawNode]]:\n  \"\"\"Nodeless stack copy.\"\"\"\n  return [(dfa, label, DUMMY_NODE) for dfa, label, _ in stack]\n\n\nclass Recorder:\n\n  def __init__(self, parser: 'Parser', ilabels: List[int],\n               context: Context) -> None:\n    self.parser = parser\n    self._ilabels = ilabels\n    self.context = context  # not really matter\n\n    self._dead_ilabels: Set[int] = set()\n    self._start_point = self.parser.stack\n    self._points = {ilabel: stack_copy(self._start_point) for ilabel in ilabels}\n\n  @property\n  def ilabels(self) -> Set[int]:\n    return self._dead_ilabels.symmetric_difference(self._ilabels)\n\n  @contextmanager\n  def switch_to(self, ilabel: int) -> Iterator[None]:\n    with self.backtrack():\n      self.parser.stack = self._points[ilabel]\n      try:\n        yield\n      except ParseError:\n        self._dead_ilabels.add(ilabel)\n      finally:\n        self.parser.stack = self._start_point\n\n  @contextmanager\n  def backtrack(self) -> Iterator[None]:\n    \"\"\"\n        Use the node-level invariant ones for basic parsing operations (push/pop/shift).\n        These still will operate on the stack; but they won't create any new nodes, or\n        modify the contents of any other existing nodes.\n        This saves us a ton of time when we are backtracking, since we\n        want to restore to the initial state as quick as possible, which\n        can only be done by having as little mutatations as possible.\n        \"\"\"  # noqa: E501\n    is_backtracking = self.parser.is_backtracking\n    try:\n      self.parser.is_backtracking = True\n      yield\n    finally:\n      self.parser.is_backtracking = is_backtracking\n\n  def add_token(self, tok_type: int, tok_val: Text, raw: bool = False) -> None:\n    func: Callable[..., Any]\n    if raw:\n      func = self.parser._addtoken\n    else:\n      func = self.parser.addtoken\n\n    for ilabel in self.ilabels:\n      with self.switch_to(ilabel):\n        args = [tok_type, tok_val, self.context]\n        if raw:\n          args.insert(0, ilabel)\n        func(*args)\n\n  def determine_route(self,\n                      value: Text = None,\n                      force: bool = False) -> Optional[int]:\n    alive_ilabels = self.ilabels\n    if len(alive_ilabels) == 0:\n      *_, most_successful_ilabel = self._dead_ilabels\n      raise ParseError('bad input', most_successful_ilabel, value, self.context)\n\n    ilabel, *rest = alive_ilabels\n    if force or not rest:\n      return ilabel\n    else:\n      return None\n\n\nclass ParseError(Exception):\n  \"\"\"Exception to signal the parser is stuck.\"\"\"\n\n  def __init__(self, msg, type, value, context):\n    Exception.__init__(\n        self, '%s: type=%r, value=%r, context=%r' % (msg, type, value, context))\n    self.msg = msg\n    self.type = type\n    self.value = value\n    self.context = context\n\n  def __reduce__(self):\n    return type(self), (self.msg, self.type, self.value, self.context)\n\n\nclass Parser(object):\n  \"\"\"Parser engine.\n\n    The proper usage sequence is:\n\n    p = Parser(grammar, [converter])  # create instance\n    p.setup([start])                  # prepare for parsing\n    <for each input token>:\n        if p.addtoken(...):           # parse a token; may raise ParseError\n            break\n    root = p.rootnode                 # root of abstract syntax tree\n\n    A Parser instance may be reused by calling setup() repeatedly.\n\n    A Parser instance contains state pertaining to the current token\n    sequence, and should not be used concurrently by different threads\n    to parse separate token sequences.\n\n    See driver.py for how to get input tokens by tokenizing a file or\n    string.\n\n    Parsing is complete when addtoken() returns True; the root of the\n    abstract syntax tree can then be retrieved from the rootnode\n    instance variable.  When a syntax error occurs, addtoken() raises\n    the ParseError exception.  There is no error recovery; the parser\n    cannot be used after a syntax error was reported (but it can be\n    reinitialized by calling setup()).\n\n    \"\"\"\n\n  def __init__(self, grammar, convert=None):\n    \"\"\"Constructor.\n\n        The grammar argument is a grammar.Grammar instance; see the\n        grammar module for more information.\n\n        The parser is not ready yet for parsing; you must call the\n        setup() method to get it started.\n\n        The optional convert argument is a function mapping concrete\n        syntax tree nodes to abstract syntax tree nodes.  If not\n        given, no conversion is done and the syntax tree produced is\n        the concrete syntax tree.  If given, it must be a function of\n        two arguments, the first being the grammar (a grammar.Grammar\n        instance), and the second being the concrete syntax tree node\n        to be converted.  The syntax tree is converted from the bottom\n        up.\n\n        A concrete syntax tree node is a (type, value, context, nodes)\n        tuple, where type is the node type (a token or symbol number),\n        value is None for symbols and a string for tokens, context is\n        None or an opaque value used for error reporting (typically a\n        (lineno, offset) pair), and nodes is a list of children for\n        symbols, and None for tokens.\n\n        An abstract syntax tree node may be anything; this is entirely\n        up to the converter function.\n\n        \"\"\"\n    self.grammar = grammar\n    self.convert = convert or (lambda grammar, node: node)\n    self.is_backtracking = False\n\n  def setup(self, proxy, start=None):\n    \"\"\"Prepare for parsing.\n\n        This *must* be called before starting to parse.\n\n        The optional argument is an alternative start symbol; it\n        defaults to the grammar's start symbol.\n\n        You can use a Parser instance to parse any number of programs;\n        each time you call setup() the parser is reset to an initial\n        state determined by the (implicit or explicit) start symbol.\n\n        \"\"\"\n    if start is None:\n      start = self.grammar.start\n    # Each stack entry is a tuple: (dfa, state, node).\n    # A node is a tuple: (type, value, context, children),\n    # where children is a list of nodes or None, and context may be None.\n    newnode = (start, None, None, [])\n    stackentry = (self.grammar.dfas[start], 0, newnode)\n    self.stack = [stackentry]\n    self.rootnode = None\n    self.used_names = set()  # Aliased to self.rootnode.used_names in pop()\n    self.proxy = proxy\n\n  def addtoken(self, type, value, context):\n    \"\"\"Add a token; return True iff this is the end of the program.\"\"\"\n    # Map from token to label\n    ilabels = self.classify(type, value, context)\n    assert len(ilabels) >= 1\n\n    # If we have only one state to advance, we'll directly\n    # take it as is.\n    if len(ilabels) == 1:\n      [ilabel] = ilabels\n      return self._addtoken(ilabel, type, value, context)\n\n    # If there are multiple states which we can advance (only\n    # happen under soft-keywords), then we will try all of them\n    # in parallel and as soon as one state can reach further than\n    # the rest, we'll choose that one. This is a pretty hacky\n    # and hopefully temporary algorithm.\n    #\n    # For a more detailed explanation, check out this post:\n    # https://tree.science/what-the-backtracking.html\n\n    with self.proxy.release() as proxy:\n      counter, force = 0, False\n      recorder = Recorder(self, ilabels, context)\n      recorder.add_token(type, value, raw=True)\n\n      next_token_value = value\n      while recorder.determine_route(next_token_value) is None:\n        if not proxy.can_advance(counter):\n          force = True\n          break\n\n        next_token_type, next_token_value, *_ = proxy.eat(counter)\n        if next_token_type in (tokenize.COMMENT, tokenize.NL):\n          counter += 1\n          continue\n\n        if next_token_type == tokenize.OP:\n          next_token_type = grammar.opmap[next_token_value]\n\n        recorder.add_token(next_token_type, next_token_value)\n        counter += 1\n\n      ilabel = cast(int,\n                    recorder.determine_route(next_token_value, force=force))\n      assert ilabel is not None\n\n    return self._addtoken(ilabel, type, value, context)\n\n  def _addtoken(self, ilabel: int, type: int, value: Text,\n                context: Context) -> bool:\n    # Loop until the token is shifted; may raise exceptions\n    while True:\n      dfa, state, node = self.stack[-1]\n      states, first = dfa\n      arcs = states[state]\n      # Look for a state with this label\n      for i, newstate in arcs:\n        t = self.grammar.labels[i][0]\n        if t >= 256:\n          # See if it's a symbol and if we're in its first set\n          itsdfa = self.grammar.dfas[t]\n          itsstates, itsfirst = itsdfa\n          if ilabel in itsfirst:\n            # Push a symbol\n            self.push(t, itsdfa, newstate, context)\n            break  # To continue the outer while loop\n\n        elif ilabel == i:\n          # Look it up in the list of labels\n          # Shift a token; we're done with it\n          self.shift(type, value, newstate, context)\n          # Pop while we are in an accept-only state\n          state = newstate\n          while states[state] == [(0, state)]:\n            self.pop()\n            if not self.stack:\n              # Done parsing!\n              return True\n            dfa, state, node = self.stack[-1]\n            states, first = dfa\n          # Done with this token\n          return False\n\n      else:\n        if (0, state) in arcs:\n          # An accepting state, pop it and try something else\n          self.pop()\n          if not self.stack:\n            # Done parsing, but another token is input\n            raise ParseError('too much input', type, value, context)\n        else:\n          # No success finding a transition\n          raise ParseError('bad input', type, value, context)\n\n  def classify(self, type, value, context):\n    \"\"\"Turn a token into a label.  (Internal)\n\n        Depending on whether the value is a soft-keyword or not,\n        this function may return multiple labels to choose from.\"\"\"\n    if type == token.NAME:\n      # Keep a listing of all used names\n      self.used_names.add(value)\n      # Check for reserved words\n      if value in self.grammar.keywords:\n        return [self.grammar.keywords[value]]\n      elif value in self.grammar.soft_keywords:\n        assert type in self.grammar.tokens\n        return [\n            self.grammar.soft_keywords[value],\n            self.grammar.tokens[type],\n        ]\n\n    ilabel = self.grammar.tokens.get(type)\n    if ilabel is None:\n      raise ParseError('bad token', type, value, context)\n    return [ilabel]\n\n  def shift(self, type: int, value: Text, newstate: int,\n            context: Context) -> None:\n    \"\"\"Shift a token.  (Internal)\"\"\"\n    if self.is_backtracking:\n      dfa, state, _ = self.stack[-1]\n      self.stack[-1] = (dfa, newstate, DUMMY_NODE)\n    else:\n      dfa, state, node = self.stack[-1]\n      rawnode: RawNode = (type, value, context, None)\n      newnode = convert(self.grammar, rawnode)\n      assert node[-1] is not None\n      node[-1].append(newnode)\n      self.stack[-1] = (dfa, newstate, node)\n\n  def push(self, type: int, newdfa: DFAS, newstate: int,\n           context: Context) -> None:\n    \"\"\"Push a nonterminal.  (Internal)\"\"\"\n    if self.is_backtracking:\n      dfa, state, _ = self.stack[-1]\n      self.stack[-1] = (dfa, newstate, DUMMY_NODE)\n      self.stack.append((newdfa, 0, DUMMY_NODE))\n    else:\n      dfa, state, node = self.stack[-1]\n      newnode: RawNode = (type, None, context, [])\n      self.stack[-1] = (dfa, newstate, node)\n      self.stack.append((newdfa, 0, newnode))\n\n  def pop(self) -> None:\n    \"\"\"Pop a nonterminal.  (Internal)\"\"\"\n    if self.is_backtracking:\n      self.stack.pop()\n    else:\n      popdfa, popstate, popnode = self.stack.pop()\n      newnode = convert(self.grammar, popnode)\n      if self.stack:\n        dfa, state, node = self.stack[-1]\n        assert node[-1] is not None\n        node[-1].append(newnode)\n      else:\n        self.rootnode = newnode\n        self.rootnode.used_names = self.used_names\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/pgen2/pgen.py",
    "content": "# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\n# Pgen imports\nfrom io import StringIO\n\nfrom . import grammar\nfrom . import token\nfrom . import tokenize\n\n\nclass PgenGrammar(grammar.Grammar):\n  pass\n\n\nclass ParserGenerator(object):\n\n  def __init__(self, filename=None, stream=None):\n    close_stream = None\n    if filename is None and stream is None:\n      raise RuntimeError(\n          'Either a filename or a stream is expected, both were none')\n    if stream is None:\n      stream = open(filename, encoding='utf-8')\n      close_stream = stream.close\n    self.filename = filename\n    self.stream = stream\n    self.generator = tokenize.generate_tokens(stream.readline)\n    self.gettoken()  # Initialize lookahead\n    self.dfas, self.startsymbol = self.parse()\n    if close_stream is not None:\n      close_stream()\n    self.first = {}  # map from symbol name to set of tokens\n    self.addfirstsets()\n\n  def make_grammar(self):\n    c = PgenGrammar()\n    names = list(self.dfas.keys())\n    names.sort()\n    names.remove(self.startsymbol)\n    names.insert(0, self.startsymbol)\n    for name in names:\n      i = 256 + len(c.symbol2number)\n      c.symbol2number[name] = i\n      c.number2symbol[i] = name\n    for name in names:\n      dfa = self.dfas[name]\n      states = []\n      for state in dfa:\n        arcs = []\n        for label, next in sorted(state.arcs.items()):\n          arcs.append((self.make_label(c, label), dfa.index(next)))\n        if state.isfinal:\n          arcs.append((0, dfa.index(state)))\n        states.append(arcs)\n      c.states.append(states)\n      c.dfas[c.symbol2number[name]] = (states, self.make_first(c, name))\n    c.start = c.symbol2number[self.startsymbol]\n    return c\n\n  def make_first(self, c, name):\n    rawfirst = self.first[name]\n    first = {}\n    for label in sorted(rawfirst):\n      ilabel = self.make_label(c, label)\n      # assert ilabel not in first # XXX failed on <> ... !=\n      first[ilabel] = 1\n    return first\n\n  def make_label(self, c, label):\n    # XXX Maybe this should be a method on a subclass of converter?\n    ilabel = len(c.labels)\n    if label[0].isalpha():\n      # Either a symbol name or a named token\n      if label in c.symbol2number:\n        # A symbol name (a non-terminal)\n        if label in c.symbol2label:\n          return c.symbol2label[label]\n        else:\n          c.labels.append((c.symbol2number[label], None))\n          c.symbol2label[label] = ilabel\n          return ilabel\n      else:\n        # A named token (NAME, NUMBER, STRING)\n        itoken = getattr(token, label, None)\n        assert isinstance(itoken, int), label\n        assert itoken in token.tok_name, label\n        if itoken in c.tokens:\n          return c.tokens[itoken]\n        else:\n          c.labels.append((itoken, None))\n          c.tokens[itoken] = ilabel\n          return ilabel\n    else:\n      # Either a keyword or an operator\n      assert label[0] in ('\"', \"'\"), label\n      value = eval(label)\n      if value[0].isalpha():\n        if label[0] == '\"':\n          keywords = c.soft_keywords\n        else:\n          keywords = c.keywords\n\n        # A keyword\n        if value in keywords:\n          return keywords[value]\n        else:\n          c.labels.append((token.NAME, value))\n          keywords[value] = ilabel\n          return ilabel\n      else:\n        # An operator (any non-numeric token)\n        itoken = grammar.opmap[value]  # Fails if unknown token\n        if itoken in c.tokens:\n          return c.tokens[itoken]\n        else:\n          c.labels.append((itoken, None))\n          c.tokens[itoken] = ilabel\n          return ilabel\n\n  def addfirstsets(self):\n    names = list(self.dfas.keys())\n    names.sort()\n    for name in names:\n      if name not in self.first:\n        self.calcfirst(name)\n      # print name, self.first[name].keys()\n\n  def calcfirst(self, name):\n    dfa = self.dfas[name]\n    self.first[name] = None  # dummy to detect left recursion\n    state = dfa[0]\n    totalset = {}\n    overlapcheck = {}\n    for label, next in state.arcs.items():\n      if label in self.dfas:\n        if label in self.first:\n          fset = self.first[label]\n          if fset is None:\n            raise ValueError('recursion for rule %r' % name)\n        else:\n          self.calcfirst(label)\n          fset = self.first[label]\n        totalset.update(fset)\n        overlapcheck[label] = fset\n      else:\n        totalset[label] = 1\n        overlapcheck[label] = {label: 1}\n    inverse = {}\n    for label, itsfirst in overlapcheck.items():\n      for symbol in itsfirst:\n        if symbol in inverse:\n          raise ValueError('rule %s is ambiguous; %s is in the'\n                           ' first sets of %s as well as %s' %\n                           (name, symbol, label, inverse[symbol]))\n        inverse[symbol] = label\n    self.first[name] = totalset\n\n  def parse(self):\n    dfas = {}\n    startsymbol = None\n    # MSTART: (NEWLINE | RULE)* ENDMARKER\n    while self.type != token.ENDMARKER:\n      while self.type == token.NEWLINE:\n        self.gettoken()\n      # RULE: NAME ':' RHS NEWLINE\n      name = self.expect(token.NAME)\n      self.expect(token.OP, ':')\n      a, z = self.parse_rhs()\n      self.expect(token.NEWLINE)\n      # self.dump_nfa(name, a, z)\n      dfa = self.make_dfa(a, z)\n      # self.dump_dfa(name, dfa)\n      self.simplify_dfa(dfa)\n      dfas[name] = dfa\n      # print name, oldlen, newlen\n      if startsymbol is None:\n        startsymbol = name\n    return dfas, startsymbol\n\n  def make_dfa(self, start, finish):\n    # To turn an NFA into a DFA, we define the states of the DFA\n    # to correspond to *sets* of states of the NFA.  Then do some\n    # state reduction.  Let's represent sets as dicts with 1 for\n    # values.\n    assert isinstance(start, NFAState)\n    assert isinstance(finish, NFAState)\n\n    def closure(state):\n      base = {}\n      addclosure(state, base)\n      return base\n\n    def addclosure(state, base):\n      assert isinstance(state, NFAState)\n      if state in base:\n        return\n      base[state] = 1\n      for label, next in state.arcs:\n        if label is None:\n          addclosure(next, base)\n\n    states = [DFAState(closure(start), finish)]\n    for state in states:  # NB states grows while we're iterating\n      arcs = {}\n      for nfastate in state.nfaset:\n        for label, next in nfastate.arcs:\n          if label is not None:\n            addclosure(next, arcs.setdefault(label, {}))\n      for label, nfaset in sorted(arcs.items()):\n        for st in states:\n          if st.nfaset == nfaset:\n            break\n        else:\n          st = DFAState(nfaset, finish)\n          states.append(st)\n        state.addarc(st, label)\n    return states  # List of DFAState instances; first one is start\n\n  def dump_nfa(self, name, start, finish):\n    print('Dump of NFA for', name)\n    todo = [start]\n    for i, state in enumerate(todo):\n      print('  State', i, state is finish and '(final)' or '')\n      for label, next in state.arcs:\n        if next in todo:\n          j = todo.index(next)\n        else:\n          j = len(todo)\n          todo.append(next)\n        if label is None:\n          print('    -> %d' % j)\n        else:\n          print('    %s -> %d' % (label, j))\n\n  def dump_dfa(self, name, dfa):\n    print('Dump of DFA for', name)\n    for i, state in enumerate(dfa):\n      print('  State', i, state.isfinal and '(final)' or '')\n      for label, next in sorted(state.arcs.items()):\n        print('    %s -> %d' % (label, dfa.index(next)))\n\n  def simplify_dfa(self, dfa):\n    # This is not theoretically optimal, but works well enough.\n    # Algorithm: repeatedly look for two states that have the same\n    # set of arcs (same labels pointing to the same nodes) and\n    # unify them, until things stop changing.\n\n    # dfa is a list of DFAState instances\n    changes = True\n    while changes:\n      changes = False\n      for i, state_i in enumerate(dfa):\n        for j in range(i + 1, len(dfa)):\n          state_j = dfa[j]\n          if state_i == state_j:\n            # print \"  unify\", i, j\n            del dfa[j]\n            for state in dfa:\n              state.unifystate(state_j, state_i)\n            changes = True\n            break\n\n  def parse_rhs(self):\n    # RHS: ALT ('|' ALT)*\n    a, z = self.parse_alt()\n    if self.value != '|':\n      return a, z\n    else:\n      aa = NFAState()\n      zz = NFAState()\n      aa.addarc(a)\n      z.addarc(zz)\n      while self.value == '|':\n        self.gettoken()\n        a, z = self.parse_alt()\n        aa.addarc(a)\n        z.addarc(zz)\n      return aa, zz\n\n  def parse_alt(self):\n    # ALT: ITEM+\n    a, b = self.parse_item()\n    while (self.value in ('(', '[') or self.type in (token.NAME, token.STRING)):\n      c, d = self.parse_item()\n      b.addarc(c)\n      b = d\n    return a, b\n\n  def parse_item(self):\n    # ITEM: '[' RHS ']' | ATOM ['+' | '*']\n    if self.value == '[':\n      self.gettoken()\n      a, z = self.parse_rhs()\n      self.expect(token.OP, ']')\n      a.addarc(z)\n      return a, z\n    else:\n      a, z = self.parse_atom()\n      value = self.value\n      if value not in ('+', '*'):\n        return a, z\n      self.gettoken()\n      z.addarc(a)\n      if value == '+':\n        return a, z\n      else:\n        return a, a\n\n  def parse_atom(self):\n    # ATOM: '(' RHS ')' | NAME | STRING\n    if self.value == '(':\n      self.gettoken()\n      a, z = self.parse_rhs()\n      self.expect(token.OP, ')')\n      return a, z\n    elif self.type in (token.NAME, token.STRING):\n      a = NFAState()\n      z = NFAState()\n      a.addarc(z, self.value)\n      self.gettoken()\n      return a, z\n    else:\n      self.raise_error('expected (...) or NAME or STRING, got %s/%s', self.type,\n                       self.value)\n\n  def expect(self, type, value=None):\n    if self.type != type or (value is not None and self.value != value):\n      self.raise_error('expected %s/%s, got %s/%s', type, value, self.type,\n                       self.value)\n    value = self.value\n    self.gettoken()\n    return value\n\n  def gettoken(self):\n    tup = next(self.generator)\n    while tup[0] in (tokenize.COMMENT, tokenize.NL):\n      tup = next(self.generator)\n    self.type, self.value, self.begin, self.end, self.line = tup\n    # print token.tok_name[self.type], repr(self.value)\n\n  def raise_error(self, msg, *args):\n    if args:\n      try:\n        msg = msg % args\n      except Exception:\n        msg = ' '.join([msg] + list(map(str, args)))\n    raise SyntaxError(msg, (self.filename, self.end[0], self.end[1], self.line))\n\n\nclass NFAState(object):\n\n  def __init__(self):\n    self.arcs = []  # list of (label, NFAState) pairs\n\n  def addarc(self, next, label=None):\n    assert label is None or isinstance(label, str)\n    assert isinstance(next, NFAState)\n    self.arcs.append((label, next))\n\n\nclass DFAState(object):\n\n  def __init__(self, nfaset, final):\n    assert isinstance(nfaset, dict)\n    assert isinstance(next(iter(nfaset)), NFAState)\n    assert isinstance(final, NFAState)\n    self.nfaset = nfaset\n    self.isfinal = final in nfaset\n    self.arcs = {}  # map from label to DFAState\n\n  def addarc(self, next, label):\n    assert isinstance(label, str)\n    assert label not in self.arcs\n    assert isinstance(next, DFAState)\n    self.arcs[label] = next\n\n  def unifystate(self, old, new):\n    for label, next in self.arcs.items():\n      if next is old:\n        self.arcs[label] = new\n\n  def __eq__(self, other):\n    # Equality test -- ignore the nfaset instance variable\n    assert isinstance(other, DFAState)\n    if self.isfinal != other.isfinal:\n      return False\n    # Can't just return self.arcs == other.arcs, because that\n    # would invoke this method recursively, with cycles...\n    if len(self.arcs) != len(other.arcs):\n      return False\n    for label, next in self.arcs.items():\n      if next is not other.arcs.get(label):\n        return False\n    return True\n\n  __hash__ = None  # For Py3 compatibility.\n\n\ndef generate_grammar(filename_or_stream='Grammar.txt'):\n  # type:(str | StringIO) -> PgenGrammar\n  if isinstance(filename_or_stream, str):\n    p = ParserGenerator(filename_or_stream)\n  elif isinstance(filename_or_stream, StringIO):\n    p = ParserGenerator(stream=filename_or_stream)\n  else:\n    raise NotImplementedError('Type %s not implemented' %\n                              type(filename_or_stream))\n  return p.make_grammar()\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/pgen2/token.py",
    "content": "#! /usr/bin/env python3\n\"\"\"Token constants (from \"token.h\").\"\"\"\n\n#  Taken from Python (r53757) and modified to include some tokens\n#   originally monkeypatched in by pgen2.tokenize\n\n# --start constants--\nENDMARKER = 0\nNAME = 1\nNUMBER = 2\nSTRING = 3\nNEWLINE = 4\nINDENT = 5\nDEDENT = 6\nLPAR = 7\nRPAR = 8\nLSQB = 9\nRSQB = 10\nCOLON = 11\nCOMMA = 12\nSEMI = 13\nPLUS = 14\nMINUS = 15\nSTAR = 16\nSLASH = 17\nVBAR = 18\nAMPER = 19\nLESS = 20\nGREATER = 21\nEQUAL = 22\nDOT = 23\nPERCENT = 24\nBACKQUOTE = 25\nLBRACE = 26\nRBRACE = 27\nEQEQUAL = 28\nNOTEQUAL = 29\nLESSEQUAL = 30\nGREATEREQUAL = 31\nTILDE = 32\nCIRCUMFLEX = 33\nLEFTSHIFT = 34\nRIGHTSHIFT = 35\nDOUBLESTAR = 36\nPLUSEQUAL = 37\nMINEQUAL = 38\nSTAREQUAL = 39\nSLASHEQUAL = 40\nPERCENTEQUAL = 41\nAMPEREQUAL = 42\nVBAREQUAL = 43\nCIRCUMFLEXEQUAL = 44\nLEFTSHIFTEQUAL = 45\nRIGHTSHIFTEQUAL = 46\nDOUBLESTAREQUAL = 47\nDOUBLESLASH = 48\nDOUBLESLASHEQUAL = 49\nAT = 50\nATEQUAL = 51\nOP = 52\nCOMMENT = 53\nNL = 54\nRARROW = 55\nAWAIT = 56\nASYNC = 57\nERRORTOKEN = 58\nCOLONEQUAL = 59\nN_TOKENS = 60\nNT_OFFSET = 256\n# --end constants--\n\ntok_name = {\n    _value: _name\n    for _name, _value in globals().copy().items()\n    if isinstance(_value, int)\n}\n\n\ndef ISTERMINAL(x):\n  return x < NT_OFFSET\n\n\ndef ISNONTERMINAL(x):\n  return x >= NT_OFFSET\n\n\ndef ISEOF(x):\n  return x == ENDMARKER\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/pgen2/tokenize.py",
    "content": "# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 Python Software Foundation.\n# All rights reserved.\n\"\"\"Tokenization help for Python programs.\n\ngenerate_tokens(readline) is a generator that breaks a stream of\ntext into Python tokens.  It accepts a readline-like method which is called\nrepeatedly to get the next line of input (or \"\" for EOF).  It generates\n5-tuples with these members:\n\n    the token type (see token.py)\n    the token (a string)\n    the starting (row, column) indices of the token (a 2-tuple of ints)\n    the ending (row, column) indices of the token (a 2-tuple of ints)\n    the original line (string)\n\nIt is designed to match the working of the Python tokenizer exactly, except\nthat it produces COMMENT tokens for comments and gives type OP for all\noperators\n\nOlder entry points\n    tokenize_loop(readline, tokeneater)\n    tokenize(readline, tokeneater=printtoken)\nare the same, except instead of generating tokens, tokeneater is a callback\nfunction to which the 5 fields described above are passed as 5 arguments,\neach time a new token is found.\"\"\"\n\n__author__ = 'Ka-Ping Yee <ping@lfw.org>'\n__credits__ = \\\n    'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro'\n\nimport re\nimport string\nfrom codecs import BOM_UTF8\nfrom codecs import lookup\n\nfrom . import token\nfrom .token import ASYNC\nfrom .token import AWAIT\nfrom .token import COMMENT\nfrom .token import DEDENT\nfrom .token import ENDMARKER\nfrom .token import ERRORTOKEN\nfrom .token import INDENT\nfrom .token import NAME\nfrom .token import NEWLINE\nfrom .token import NL\nfrom .token import NUMBER\nfrom .token import OP\nfrom .token import STRING\nfrom .token import tok_name\n\n__all__ = [x for x in dir(token) if x[0] != '_'\n          ] + ['tokenize', 'generate_tokens', 'untokenize']\ndel token\n\ntry:\n  bytes\nexcept NameError:\n  # Support bytes type in Python <= 2.5, so 2to3 turns itself into\n  # valid Python 3 code.\n  bytes = str\n\n\ndef group(*choices):\n  return '(' + '|'.join(choices) + ')'\n\n\ndef any(*choices):\n  return group(*choices) + '*'\n\n\ndef maybe(*choices):\n  return group(*choices) + '?'\n\n\ndef _combinations(*l):  # noqa: E741\n  return set(\n      x + y for x in l for y in l + ('',) if x.casefold() != y.casefold())\n\n\nWhitespace = r'[ \\f\\t]*'\nComment = r'#[^\\r\\n]*'\nIgnore = Whitespace + any(r'\\\\\\r?\\n' + Whitespace) + maybe(Comment)\nName = r'\\w+'\n\nBinnumber = r'0[bB]_?[01]+(?:_[01]+)*'\nHexnumber = r'0[xX]_?[\\da-fA-F]+(?:_[\\da-fA-F]+)*[lL]?'\nOctnumber = r'0[oO]?_?[0-7]+(?:_[0-7]+)*[lL]?'\nDecnumber = group(r'[1-9]\\d*(?:_\\d+)*[lL]?', '0[lL]?')\nIntnumber = group(Binnumber, Hexnumber, Octnumber, Decnumber)\nExponent = r'[eE][-+]?\\d+(?:_\\d+)*'\nPointfloat = group(r'\\d+(?:_\\d+)*\\.(?:\\d+(?:_\\d+)*)?',\n                   r'\\.\\d+(?:_\\d+)*') + maybe(Exponent)\nExpfloat = r'\\d+(?:_\\d+)*' + Exponent\nFloatnumber = group(Pointfloat, Expfloat)\nImagnumber = group(r'\\d+(?:_\\d+)*[jJ]', Floatnumber + r'[jJ]')\nNumber = group(Imagnumber, Floatnumber, Intnumber)\n\n# Tail end of ' string.\nSingle = r\"[^'\\\\]*(?:\\\\.[^'\\\\]*)*'\"\n# Tail end of \" string.\nDouble = r'[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"'\n# Tail end of ''' string.\nSingle3 = r\"[^'\\\\]*(?:(?:\\\\.|'(?!''))[^'\\\\]*)*'''\"\n# Tail end of \"\"\" string.\nDouble3 = r'[^\"\\\\]*(?:(?:\\\\.|\"(?!\"\"))[^\"\\\\]*)*\"\"\"'\n_litprefix = r'(?:[uUrRbBfF]|[rR][fFbB]|[fFbBuU][rR])?'\nTriple = group(_litprefix + \"'''\", _litprefix + '\"\"\"')\n# Single-line ' or \" string.\nString = group(_litprefix + r\"'[^\\n'\\\\]*(?:\\\\.[^\\n'\\\\]*)*'\",\n               _litprefix + r'\"[^\\n\"\\\\]*(?:\\\\.[^\\n\"\\\\]*)*\"')\n\n# Because of leftmost-then-longest match semantics, be sure to put the\n# longest operators first (e.g., if = came before ==, == would get\n# recognized as two instances of =).\nOperator = group(r'\\*\\*=?', r'>>=?', r'<<=?', r'<>', r'!=', r'//=?', r'->',\n                 r'[+\\-*/%&@|^=<>]=?', r'~')\n\nBracket = '[][(){}]'\nSpecial = group(r'\\r?\\n', r':=', r'[:;.,`@]')\nFunny = group(Operator, Bracket, Special)\n\nPlainToken = group(Number, Funny, String, Name)\nToken = Ignore + PlainToken\n\n# First (or only) line of ' or \" string.\nContStr = group(\n    _litprefix + r\"'[^\\n'\\\\]*(?:\\\\.[^\\n'\\\\]*)*\" + group(\"'\", r'\\\\\\r?\\n'),\n    _litprefix + r'\"[^\\n\"\\\\]*(?:\\\\.[^\\n\"\\\\]*)*' + group('\"', r'\\\\\\r?\\n'))\nPseudoExtras = group(r'\\\\\\r?\\n', Comment, Triple)\nPseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)\n\ntokenprog, pseudoprog, single3prog, double3prog = map(\n    re.compile, (Token, PseudoToken, Single3, Double3))\n\n_strprefixes = (\n    _combinations('r', 'R', 'f', 'F') | _combinations('r', 'R', 'b', 'B')\n    | {'u', 'U', 'ur', 'uR', 'Ur', 'UR'})\n\nendprogs = {\n    \"'\": re.compile(Single),\n    '\"': re.compile(Double),\n    \"'''\": single3prog,\n    '\"\"\"': double3prog,\n    **{\n        f\"{prefix}'''\": single3prog for prefix in _strprefixes\n    },\n    **{\n        f'{prefix}\"\"\"': double3prog for prefix in _strprefixes\n    },\n    **{\n        prefix: None for prefix in _strprefixes\n    }\n}\n\ntriple_quoted = ({\"'''\", '\"\"\"'} | {f\"{prefix}'''\" for prefix in _strprefixes}\n                 | {f'{prefix}\"\"\"' for prefix in _strprefixes})\nsingle_quoted = ({\"'\", '\"'} | {f\"{prefix}'\" for prefix in _strprefixes}\n                 | {f'{prefix}\"' for prefix in _strprefixes})\n\ntabsize = 8\n\n\nclass TokenError(Exception):\n  pass\n\n\nclass StopTokenizing(Exception):\n  pass\n\n\ndef printtoken(type, token, xxx_todo_changeme, xxx_todo_changeme1,\n               line):  # for testing\n  (srow, scol) = xxx_todo_changeme\n  (erow, ecol) = xxx_todo_changeme1\n  print('%d,%d-%d,%d:\\t%s\\t%s' %\n        (srow, scol, erow, ecol, tok_name[type], repr(token)))\n\n\ndef tokenize(readline, tokeneater=printtoken):\n  \"\"\"\n    The tokenize() function accepts two parameters: one representing the\n    input stream, and one providing an output mechanism for tokenize().\n\n    The first parameter, readline, must be a callable object which provides\n    the same interface as the readline() method of built-in file objects.\n    Each call to the function should return one line of input as a string.\n\n    The second parameter, tokeneater, must also be a callable object. It is\n    called once for each token, with five arguments, corresponding to the\n    tuples generated by generate_tokens().\n    \"\"\"\n  try:\n    tokenize_loop(readline, tokeneater)\n  except StopTokenizing:\n    pass\n\n\n# backwards compatible interface\ndef tokenize_loop(readline, tokeneater):\n  for token_info in generate_tokens(readline):\n    tokeneater(*token_info)\n\n\nclass Untokenizer:\n\n  def __init__(self):\n    self.tokens = []\n    self.prev_row = 1\n    self.prev_col = 0\n\n  def add_whitespace(self, start):\n    row, col = start\n    assert row <= self.prev_row\n    col_offset = col - self.prev_col\n    if col_offset:\n      self.tokens.append(' ' * col_offset)\n\n  def untokenize(self, iterable):\n    for t in iterable:\n      if len(t) == 2:\n        self.compat(t, iterable)\n        break\n      tok_type, token, start, end, line = t\n      self.add_whitespace(start)\n      self.tokens.append(token)\n      self.prev_row, self.prev_col = end\n      if tok_type in (NEWLINE, NL):\n        self.prev_row += 1\n        self.prev_col = 0\n    return ''.join(self.tokens)\n\n  def compat(self, token, iterable):\n    startline = False\n    indents = []\n    toks_append = self.tokens.append\n    toknum, tokval = token\n    if toknum in (NAME, NUMBER):\n      tokval += ' '\n    if toknum in (NEWLINE, NL):\n      startline = True\n    for tok in iterable:\n      toknum, tokval = tok[:2]\n\n      if toknum in (NAME, NUMBER, ASYNC, AWAIT):\n        tokval += ' '\n\n      if toknum == INDENT:\n        indents.append(tokval)\n        continue\n      elif toknum == DEDENT:\n        indents.pop()\n        continue\n      elif toknum in (NEWLINE, NL):\n        startline = True\n      elif startline and indents:\n        toks_append(indents[-1])\n        startline = False\n      toks_append(tokval)\n\n\ncookie_re = re.compile(r'^[ \\t\\f]*#.*?coding[:=][ \\t]*([-\\w.]+)', re.ASCII)\nblank_re = re.compile(br'^[ \\t\\f]*(?:[#\\r\\n]|$)', re.ASCII)\n\n\ndef _get_normal_name(orig_enc):\n  \"\"\"Imitates get_normal_name in tokenizer.c.\"\"\"\n  # Only care about the first 12 characters.\n  enc = orig_enc[:12].lower().replace('_', '-')\n  if enc == 'utf-8' or enc.startswith('utf-8-'):\n    return 'utf-8'\n  if enc in ('latin-1', 'iso-8859-1', 'iso-latin-1') or \\\n     enc.startswith(('latin-1-', 'iso-8859-1-', 'iso-latin-1-')):\n    return 'iso-8859-1'\n  return orig_enc\n\n\ndef detect_encoding(readline):\n  \"\"\"\n    The detect_encoding() function is used to detect the encoding that should\n    be used to decode a Python source file. It requires one argument, readline,\n    in the same way as the tokenize() generator.\n\n    It will call readline a maximum of twice, and return the encoding used\n    (as a string) and a list of any lines (left as bytes) it has read\n    in.\n\n    It detects the encoding from the presence of a utf-8 bom or an encoding\n    cookie as specified in pep-0263. If both a bom and a cookie are present, but\n    disagree, a SyntaxError will be raised. If the encoding cookie is an invalid\n    charset, raise a SyntaxError.  Note that if a utf-8 bom is found,\n    'utf-8-sig' is returned.\n\n    If no encoding is specified, then the default of 'utf-8' will be returned.\n    \"\"\"\n  bom_found = False\n  encoding = None\n  default = 'utf-8'\n\n  def read_or_stop():\n    try:\n      return readline()\n    except StopIteration:\n      return bytes()\n\n  def find_cookie(line):\n    try:\n      line_string = line.decode('ascii')\n    except UnicodeDecodeError:\n      return None\n    match = cookie_re.match(line_string)\n    if not match:\n      return None\n    encoding = _get_normal_name(match.group(1))\n    try:\n      codec = lookup(encoding)\n    except LookupError:\n      # This behaviour mimics the Python interpreter\n      raise SyntaxError('unknown encoding: ' + encoding)\n\n    if bom_found:\n      if codec.name != 'utf-8':\n        # This behaviour mimics the Python interpreter\n        raise SyntaxError('encoding problem: utf-8')\n      encoding += '-sig'\n    return encoding\n\n  first = read_or_stop()\n  if first.startswith(BOM_UTF8):\n    bom_found = True\n    first = first[3:]\n    default = 'utf-8-sig'\n  if not first:\n    return default, []\n\n  encoding = find_cookie(first)\n  if encoding:\n    return encoding, [first]\n  if not blank_re.match(first):\n    return default, [first]\n\n  second = read_or_stop()\n  if not second:\n    return default, [first]\n\n  encoding = find_cookie(second)\n  if encoding:\n    return encoding, [first, second]\n\n  return default, [first, second]\n\n\ndef untokenize(iterable):\n  \"\"\"Transform tokens back into Python source code.\n\n    Each element returned by the iterable must be a token sequence\n    with at least two elements, a token number and token value.  If\n    only two tokens are passed, the resulting output is poor.\n\n    Round-trip invariant for full input:\n        Untokenized source will match input source exactly\n\n    Round-trip invariant for limited input:\n        # Output text will tokenize the back to the input\n        t1 = [tok[:2] for tok in generate_tokens(f.readline)]\n        newcode = untokenize(t1)\n        readline = iter(newcode.splitlines(1)).next\n        t2 = [tok[:2] for tokin generate_tokens(readline)]\n        assert t1 == t2\n    \"\"\"\n  ut = Untokenizer()\n  return ut.untokenize(iterable)\n\n\ndef generate_tokens(readline):\n  \"\"\"\n    The generate_tokens() generator requires one argument, readline, which\n    must be a callable object which provides the same interface as the\n    readline() method of built-in file objects. Each call to the function\n    should return one line of input as a string.  Alternately, readline\n    can be a callable function terminating with StopIteration:\n        readline = open(myfile).next    # Example of alternate readline\n\n    The generator produces 5-tuples with these members: the token type; the\n    token string; a 2-tuple (srow, scol) of ints specifying the row and\n    column where the token begins in the source; a 2-tuple (erow, ecol) of\n    ints specifying the row and column where the token ends in the source;\n    and the line on which the token was found. The line passed is the\n    physical line.\n    \"\"\"\n  strstart = ''\n  endprog = ''\n  lnum = parenlev = continued = 0\n  contstr, needcont = '', 0\n  contline = None\n  indents = [0]\n\n  # 'stashed' and 'async_*' are used for async/await parsing\n  stashed = None\n  async_def = False\n  async_def_indent = 0\n  async_def_nl = False\n\n  while 1:  # loop over lines in stream\n    try:\n      line = readline()\n    except StopIteration:\n      line = ''\n    lnum = lnum + 1\n    pos, max = 0, len(line)\n\n    if contstr:  # continued string\n      if not line:\n        raise TokenError('EOF in multi-line string', strstart)\n      endmatch = endprog.match(line)\n      if endmatch:\n        pos = end = endmatch.end(0)\n        yield (STRING, contstr + line[:end], strstart, (lnum, end),\n               contline + line)\n        contstr, needcont = '', 0\n        contline = None\n      elif needcont and line[-2:] != '\\\\\\n' and line[-3:] != '\\\\\\r\\n':\n        yield (ERRORTOKEN, contstr + line, strstart, (lnum, len(line)),\n               contline)\n        contstr = ''\n        contline = None\n        continue\n      else:\n        contstr = contstr + line\n        contline = contline + line\n        continue\n\n    elif parenlev == 0 and not continued:  # new statement\n      if not line:\n        break\n      column = 0\n      while pos < max:  # measure leading whitespace\n        if line[pos] == ' ':\n          column = column + 1\n        elif line[pos] == '\\t':\n          column = (column // tabsize + 1) * tabsize\n        elif line[pos] == '\\f':\n          column = 0\n        else:\n          break\n        pos = pos + 1\n      if pos == max:\n        break\n\n      if stashed:\n        yield stashed\n        stashed = None\n\n      if line[pos] in '#\\r\\n':  # skip comments or blank lines\n        if line[pos] == '#':\n          comment_token = line[pos:].rstrip('\\r\\n')\n          nl_pos = pos + len(comment_token)\n          yield (COMMENT, comment_token, (lnum, pos),\n                 (lnum, pos + len(comment_token)), line)\n          yield (NL, line[nl_pos:], (lnum, nl_pos), (lnum, len(line)), line)\n        else:\n          yield ((NL, COMMENT)[line[pos] == '#'], line[pos:], (lnum, pos),\n                 (lnum, len(line)), line)\n        continue\n\n      if column > indents[-1]:  # count indents or dedents\n        indents.append(column)\n        yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line)\n      while column < indents[-1]:\n        if column not in indents:\n          raise IndentationError(\n              'unindent does not match any outer indentation level',\n              ('<tokenize>', lnum, pos, line))\n        indents = indents[:-1]\n\n        if async_def and async_def_indent >= indents[-1]:\n          async_def = False\n          async_def_nl = False\n          async_def_indent = 0\n\n        yield (DEDENT, '', (lnum, pos), (lnum, pos), line)\n\n      if async_def and async_def_nl and async_def_indent >= indents[-1]:\n        async_def = False\n        async_def_nl = False\n        async_def_indent = 0\n\n    else:  # continued statement\n      if not line:\n        raise TokenError('EOF in multi-line statement', (lnum, 0))\n      continued = 0\n\n    while pos < max:\n      pseudomatch = pseudoprog.match(line, pos)\n      if pseudomatch:  # scan for tokens\n        start, end = pseudomatch.span(1)\n        spos, epos, pos = (lnum, start), (lnum, end), end\n        token, initial = line[start:end], line[start]\n\n        if initial in string.digits or \\\n           (initial == '.' and token != '.'):      # ordinary number\n          yield (NUMBER, token, spos, epos, line)\n        elif initial in '\\r\\n':\n          newline = NEWLINE\n          if parenlev > 0:\n            newline = NL\n          elif async_def:\n            async_def_nl = True\n          if stashed:\n            yield stashed\n            stashed = None\n          yield (newline, token, spos, epos, line)\n\n        elif initial == '#':\n          assert not token.endswith('\\n')\n          if stashed:\n            yield stashed\n            stashed = None\n          yield (COMMENT, token, spos, epos, line)\n        elif token in triple_quoted:\n          endprog = endprogs[token]\n          endmatch = endprog.match(line, pos)\n          if endmatch:  # all on one line\n            pos = endmatch.end(0)\n            token = line[start:pos]\n            if stashed:\n              yield stashed\n              stashed = None\n            yield (STRING, token, spos, (lnum, pos), line)\n          else:\n            strstart = (lnum, start)  # multiple lines\n            contstr = line[start:]\n            contline = line\n            break\n        elif initial in single_quoted or \\\n            token[:2] in single_quoted or \\\n            token[:3] in single_quoted:\n          if token[-1] == '\\n':  # continued string\n            strstart = (lnum, start)  # noqa: F841\n            endprog = (\n                endprogs[initial] or endprogs[token[1]] or endprogs[token[2]])\n            contstr, needcont = line[start:], 1\n            contline = line\n            break\n          else:  # ordinary string\n            if stashed:\n              yield stashed\n              stashed = None\n            yield (STRING, token, spos, epos, line)\n        elif initial.isidentifier():  # ordinary name\n          if token in ('async', 'await'):\n            if async_def:\n              yield (ASYNC if token == 'async' else AWAIT, token, spos, epos,\n                     line)\n              continue\n\n          tok = (NAME, token, spos, epos, line)\n          if token == 'async' and not stashed:\n            stashed = tok\n            continue\n\n          if token in ('def', 'for'):\n            if (stashed and stashed[0] == NAME and stashed[1] == 'async'):\n\n              if token == 'def':\n                async_def = True\n                async_def_indent = indents[-1]\n\n              yield (ASYNC, stashed[1], stashed[2], stashed[3], stashed[4])\n              stashed = None\n\n          if stashed:\n            yield stashed\n            stashed = None\n\n          yield tok\n        elif initial == '\\\\':  # continued stmt\n          # This yield is new; needed for better idempotency:\n          if stashed:\n            yield stashed\n            stashed = None\n          yield (NL, token, spos, (lnum, pos), line)\n          continued = 1\n        else:\n          if initial in '([{':\n            parenlev = parenlev + 1\n          elif initial in ')]}':\n            parenlev = parenlev - 1\n          if stashed:\n            yield stashed\n            stashed = None\n          yield (OP, token, spos, epos, line)\n      else:\n        yield (ERRORTOKEN, line[pos], (lnum, pos), (lnum, pos + 1), line)\n        pos = pos + 1\n\n  if stashed:\n    yield stashed\n    stashed = None\n\n  for indent in indents[1:]:  # pop remaining indent levels\n    yield (DEDENT, '', (lnum, 0), (lnum, 0), '')\n  yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '')\n\n\nif __name__ == '__main__':  # testing\n  import sys\n  if len(sys.argv) > 1:\n    tokenize(open(sys.argv[1]).readline)\n  else:\n    tokenize(sys.stdin.readline)\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/pygram.py",
    "content": "# Copyright 2006 Google, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\"\"\"Export the Python grammar and symbols.\"\"\"\n\n# Python imports\nimport os\n\n# Local imports\nfrom .pgen2 import driver\n\n# The grammar file\n_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), 'Grammar.txt')\n_PATTERN_GRAMMAR_FILE = os.path.join(\n    os.path.dirname(__file__), 'PatternGrammar.txt')\n\n\nclass Symbols(object):\n\n  def __init__(self, grammar):\n    \"\"\"Initializer.\n\n        Creates an attribute for each grammar symbol (nonterminal),\n        whose value is the symbol's type (an int >= 256).\n        \"\"\"\n    for name, symbol in grammar.symbol2number.items():\n      setattr(self, name, symbol)\n\n\npython_grammar = driver.load_grammar(_GRAMMAR_FILE)\n\npython_symbols = Symbols(python_grammar)\n\npython_grammar_no_print_statement = python_grammar.copy()\ndel python_grammar_no_print_statement.keywords['print']\n\npython_grammar_no_print_and_exec_statement = python_grammar_no_print_statement.copy()  # yapf: disable # noqa: E501\ndel python_grammar_no_print_and_exec_statement.keywords['exec']\n\npattern_grammar = driver.load_grammar(_PATTERN_GRAMMAR_FILE)\npattern_symbols = Symbols(pattern_grammar)\n"
  },
  {
    "path": "third_party/yapf_third_party/_ylib2to3/pytree.py",
    "content": "# Copyright 2006 Google, Inc. All Rights Reserved.\n# Licensed to PSF under a Contributor Agreement.\n\"\"\"\nPython parse tree definitions.\n\nThis is a very concrete parse tree; we need to keep every token and\neven the comments and whitespace between tokens.\n\nThere's also a pattern matching implementation here.\n\"\"\"\n\n__author__ = 'Guido van Rossum <guido@python.org>'\n\nimport sys\nfrom io import StringIO\nfrom typing import List\nfrom typing import Optional\nfrom typing import Text\nfrom typing import Tuple\nfrom typing import Union\n\nHUGE = 0x7FFFFFFF  # maximum repeat count, default max\n\n_type_reprs = {}\n\n\ndef type_repr(type_num):\n  global _type_reprs\n  if not _type_reprs:\n    from .pygram import python_symbols\n\n    # printing tokens is possible but not as useful\n    # from .pgen2 import token // token.__dict__.items():\n    for name, val in python_symbols.__dict__.items():\n      if isinstance(val, int):\n        _type_reprs[val] = name\n  return _type_reprs.setdefault(type_num, type_num)\n\n\nNL = Union['Node', 'Leaf']\nContext = Tuple[Text, Tuple[int, int]]\nRawNode = Tuple[int, Optional[Text], Optional[Context], Optional[List[NL]]]\n\n\nclass Base(object):\n  \"\"\"\n    Abstract base class for Node and Leaf.\n\n    This provides some default functionality and boilerplate using the\n    template pattern.\n\n    A node may be a subnode of at most one parent.\n    \"\"\"\n\n  # Default values for instance variables\n  type = None  # int: token number (< 256) or symbol number (>= 256)\n  parent = None  # Parent node pointer, or None\n  children = ()  # Tuple of subnodes\n  was_changed = False\n  was_checked = False\n\n  def __new__(cls, *args, **kwds):\n    \"\"\"Constructor that prevents Base from being instantiated.\"\"\"\n    assert cls is not Base, 'Cannot instantiate Base'\n    return object.__new__(cls)\n\n  def __eq__(self, other):\n    \"\"\"\n        Compare two nodes for equality.\n\n        This calls the method _eq().\n        \"\"\"\n    if self.__class__ is not other.__class__:\n      return NotImplemented\n    return self._eq(other)\n\n  __hash__ = None  # For Py3 compatibility.\n\n  def _eq(self, other):\n    \"\"\"\n        Compare two nodes for equality.\n\n        This is called by __eq__ and __ne__.  It is only called if the two nodes\n        have the same type.  This must be implemented by the concrete subclass.\n        Nodes should be considered equal if they have the same structure,\n        ignoring the prefix string and other context information.\n        \"\"\"\n    raise NotImplementedError\n\n  def clone(self):\n    \"\"\"\n        Return a cloned (deep) copy of self.\n\n        This must be implemented by the concrete subclass.\n        \"\"\"\n    raise NotImplementedError\n\n  def post_order(self):\n    \"\"\"\n        Return a post-order iterator for the tree.\n\n        This must be implemented by the concrete subclass.\n        \"\"\"\n    raise NotImplementedError\n\n  def pre_order(self):\n    \"\"\"\n        Return a pre-order iterator for the tree.\n\n        This must be implemented by the concrete subclass.\n        \"\"\"\n    raise NotImplementedError\n\n  def replace(self, new):\n    \"\"\"Replace this node with a new one in the parent.\"\"\"\n    assert self.parent is not None, str(self)\n    assert new is not None\n    if not isinstance(new, list):\n      new = [new]\n    l_children = []\n    found = False\n    for ch in self.parent.children:\n      if ch is self:\n        assert not found, (self.parent.children, self, new)\n        if new is not None:\n          l_children.extend(new)\n        found = True\n      else:\n        l_children.append(ch)\n    assert found, (self.children, self, new)\n    self.parent.changed()\n    self.parent.children = l_children\n    for x in new:\n      x.parent = self.parent\n    self.parent = None\n\n  def get_lineno(self):\n    \"\"\"Return the line number which generated the invocant node.\"\"\"\n    node = self\n    while not isinstance(node, Leaf):\n      if not node.children:\n        return\n      node = node.children[0]\n    return node.lineno\n\n  def changed(self):\n    if self.parent:\n      self.parent.changed()\n    self.was_changed = True\n\n  def remove(self):\n    \"\"\"\n        Remove the node from the tree. Returns the position of the node in its\n        parent's children before it was removed.\n        \"\"\"\n    if self.parent:\n      for i, node in enumerate(self.parent.children):\n        if node is self:\n          self.parent.changed()\n          del self.parent.children[i]\n          self.parent = None\n          return i\n\n  @property\n  def next_sibling(self):\n    \"\"\"\n        The node immediately following the invocant in their parent's children\n        list. If the invocant does not have a next sibling, it is None\n        \"\"\"\n    if self.parent is None:\n      return None\n\n    # Can't use index(); we need to test by identity\n    for i, child in enumerate(self.parent.children):\n      if child is self:\n        try:\n          return self.parent.children[i + 1]\n        except IndexError:\n          return None\n\n  @property\n  def prev_sibling(self):\n    \"\"\"\n        The node immediately preceding the invocant in their parent's children\n        list. If the invocant does not have a previous sibling, it is None.\n        \"\"\"\n    if self.parent is None:\n      return None\n\n    # Can't use index(); we need to test by identity\n    for i, child in enumerate(self.parent.children):\n      if child is self:\n        if i == 0:\n          return None\n        return self.parent.children[i - 1]\n\n  def leaves(self):\n    for child in self.children:\n      yield from child.leaves()\n\n  def depth(self):\n    if self.parent is None:\n      return 0\n    return 1 + self.parent.depth()\n\n  def get_suffix(self):\n    \"\"\"\n        Return the string immediately following the invocant node. This is\n        effectively equivalent to node.next_sibling.prefix\n        \"\"\"\n    next_sib = self.next_sibling\n    if next_sib is None:\n      return ''\n    return next_sib.prefix\n\n  if sys.version_info < (3, 0):\n\n    def __str__(self):\n      return str(self).encode('ascii')\n\n\nclass Node(Base):\n  \"\"\"Concrete implementation for interior nodes.\"\"\"\n\n  def __init__(self,\n               type,\n               children,\n               context=None,\n               prefix=None,\n               fixers_applied=None):\n    \"\"\"\n        Initializer.\n\n        Takes a type constant (a symbol number >= 256), a sequence of\n        child nodes, and an optional context keyword argument.\n\n        As a side effect, the parent pointers of the children are updated.\n        \"\"\"\n    assert type >= 256, type\n    self.type = type\n    self.children = list(children)\n    for ch in self.children:\n      assert ch.parent is None, repr(ch)\n      ch.parent = self\n    if prefix is not None:\n      self.prefix = prefix\n    if fixers_applied:\n      self.fixers_applied = fixers_applied[:]\n    else:\n      self.fixers_applied = None\n\n  def __repr__(self):\n    \"\"\"Return a canonical string representation.\"\"\"\n    return '%s(%s, %r)' % (self.__class__.__name__, type_repr(\n        self.type), self.children)\n\n  def __unicode__(self):\n    \"\"\"\n        Return a pretty string representation.\n\n        This reproduces the input source exactly.\n        \"\"\"\n    return ''.join(map(str, self.children))\n\n  if sys.version_info > (3, 0):\n    __str__ = __unicode__\n\n  def _eq(self, other):\n    \"\"\"Compare two nodes for equality.\"\"\"\n    return (self.type, self.children) == (other.type, other.children)\n\n  def clone(self):\n    \"\"\"Return a cloned (deep) copy of self.\"\"\"\n    return Node(\n        self.type, [ch.clone() for ch in self.children],\n        fixers_applied=self.fixers_applied)\n\n  def post_order(self):\n    \"\"\"Return a post-order iterator for the tree.\"\"\"\n    for child in self.children:\n      yield from child.post_order()\n    yield self\n\n  def pre_order(self):\n    \"\"\"Return a pre-order iterator for the tree.\"\"\"\n    yield self\n    for child in self.children:\n      yield from child.pre_order()\n\n  @property\n  def prefix(self):\n    \"\"\"\n        The whitespace and comments preceding this node in the input.\n        \"\"\"\n    if not self.children:\n      return ''\n    return self.children[0].prefix\n\n  @prefix.setter\n  def prefix(self, prefix):\n    if self.children:\n      self.children[0].prefix = prefix\n\n  def set_child(self, i, child):\n    \"\"\"\n        Equivalent to 'node.children[i] = child'. This method also sets the\n        child's parent attribute appropriately.\n        \"\"\"\n    child.parent = self\n    self.children[i].parent = None\n    self.children[i] = child\n    self.changed()\n\n  def insert_child(self, i, child):\n    \"\"\"\n        Equivalent to 'node.children.insert(i, child)'. This method also sets\n        the child's parent attribute appropriately.\n        \"\"\"\n    child.parent = self\n    self.children.insert(i, child)\n    self.changed()\n\n  def append_child(self, child):\n    \"\"\"\n        Equivalent to 'node.children.append(child)'. This method also sets the\n        child's parent attribute appropriately.\n        \"\"\"\n    child.parent = self\n    self.children.append(child)\n    self.changed()\n\n\nclass Leaf(Base):\n  \"\"\"Concrete implementation for leaf nodes.\"\"\"\n\n  # Default values for instance variables\n  _prefix = ''  # Whitespace and comments preceding this token in the input\n  lineno = 0  # Line where this token starts in the input\n  column = 0  # Column where this token tarts in the input\n\n  def __init__(self, type, value, context=None, prefix=None, fixers_applied=[]):\n    \"\"\"\n        Initializer.\n\n        Takes a type constant (a token number < 256), a string value, and an\n        optional context keyword argument.\n        \"\"\"\n    assert 0 <= type < 256, type\n    if context is not None:\n      self._prefix, (self.lineno, self.column) = context\n    self.type = type\n    self.value = value\n    if prefix is not None:\n      self._prefix = prefix\n    self.fixers_applied = fixers_applied[:]\n\n  def __repr__(self):\n    \"\"\"Return a canonical string representation.\"\"\"\n    return '%s(%r, %r)' % (self.__class__.__name__, self.type, self.value)\n\n  def __unicode__(self):\n    \"\"\"\n        Return a pretty string representation.\n\n        This reproduces the input source exactly.\n        \"\"\"\n    return self.prefix + str(self.value)\n\n  if sys.version_info > (3, 0):\n    __str__ = __unicode__\n\n  def _eq(self, other):\n    \"\"\"Compare two nodes for equality.\"\"\"\n    return (self.type, self.value) == (other.type, other.value)\n\n  def clone(self):\n    \"\"\"Return a cloned (deep) copy of self.\"\"\"\n    return Leaf(\n        self.type,\n        self.value, (self.prefix, (self.lineno, self.column)),\n        fixers_applied=self.fixers_applied)\n\n  def leaves(self):\n    yield self\n\n  def post_order(self):\n    \"\"\"Return a post-order iterator for the tree.\"\"\"\n    yield self\n\n  def pre_order(self):\n    \"\"\"Return a pre-order iterator for the tree.\"\"\"\n    yield self\n\n  @property\n  def prefix(self):\n    \"\"\"\n        The whitespace and comments preceding this token in the input.\n        \"\"\"\n    return self._prefix\n\n  @prefix.setter\n  def prefix(self, prefix):\n    self.changed()\n    self._prefix = prefix\n\n\ndef convert(gr, raw_node):\n  \"\"\"\n    Convert raw node information to a Node or Leaf instance.\n\n    This is passed to the parser driver which calls it whenever a reduction of a\n    grammar rule produces a new complete node, so that the tree is build\n    strictly bottom-up.\n    \"\"\"\n  type, value, context, children = raw_node\n  if children or type in gr.number2symbol:\n    # If there's exactly one child, return that child instead of\n    # creating a new node.\n    if len(children) == 1:\n      return children[0]\n    return Node(type, children, context=context)\n  else:\n    return Leaf(type, value, context=context)\n\n\nclass BasePattern(object):\n  \"\"\"\n    A pattern is a tree matching pattern.\n\n    It looks for a specific node type (token or symbol), and\n    optionally for a specific content.\n\n    This is an abstract base class.  There are three concrete\n    subclasses:\n\n    - LeafPattern matches a single leaf node;\n    - NodePattern matches a single node (usually non-leaf);\n    - WildcardPattern matches a sequence of nodes of variable length.\n    \"\"\"\n\n  # Defaults for instance variables\n  type = None  # Node type (token if < 256, symbol if >= 256)\n  content = None  # Optional content matching pattern\n  name = None  # Optional name used to store match in results dict\n\n  def __new__(cls, *args, **kwds):\n    \"\"\"Constructor that prevents BasePattern from being instantiated.\"\"\"\n    assert cls is not BasePattern, 'Cannot instantiate BasePattern'\n    return object.__new__(cls)\n\n  def __repr__(self):\n    args = [type_repr(self.type), self.content, self.name]\n    while args and args[-1] is None:\n      del args[-1]\n    return '%s(%s)' % (self.__class__.__name__, ', '.join(map(repr, args)))\n\n  def optimize(self):\n    \"\"\"\n        A subclass can define this as a hook for optimizations.\n\n        Returns either self or another node with the same effect.\n        \"\"\"\n    return self\n\n  def match(self, node, results=None):\n    \"\"\"\n        Does this pattern exactly match a node?\n\n        Returns True if it matches, False if not.\n\n        If results is not None, it must be a dict which will be\n        updated with the nodes matching named subpatterns.\n\n        Default implementation for non-wildcard patterns.\n        \"\"\"\n    if self.type is not None and node.type != self.type:\n      return False\n    if self.content is not None:\n      r = None\n      if results is not None:\n        r = {}\n      if not self._submatch(node, r):\n        return False\n      if r:\n        results.update(r)\n    if results is not None and self.name:\n      results[self.name] = node\n    return True\n\n  def match_seq(self, nodes, results=None):\n    \"\"\"\n        Does this pattern exactly match a sequence of nodes?\n\n        Default implementation for non-wildcard patterns.\n        \"\"\"\n    if len(nodes) != 1:\n      return False\n    return self.match(nodes[0], results)\n\n  def generate_matches(self, nodes):\n    \"\"\"\n        Generator yielding all matches for this pattern.\n\n        Default implementation for non-wildcard patterns.\n        \"\"\"\n    r = {}\n    if nodes and self.match(nodes[0], r):\n      yield 1, r\n\n\nclass LeafPattern(BasePattern):\n\n  def __init__(self, type=None, content=None, name=None):\n    \"\"\"\n        Initializer.  Takes optional type, content, and name.\n\n        The type, if given must be a token type (< 256).  If not given,\n        this matches any *leaf* node; the content may still be required.\n\n        The content, if given, must be a string.\n\n        If a name is given, the matching node is stored in the results\n        dict under that key.\n        \"\"\"\n    if type is not None:\n      assert 0 <= type < 256, type\n    if content is not None:\n      assert isinstance(content, str), repr(content)\n    self.type = type\n    self.content = content\n    self.name = name\n\n  def match(self, node, results=None):\n    \"\"\"Override match() to insist on a leaf node.\"\"\"\n    if not isinstance(node, Leaf):\n      return False\n    return BasePattern.match(self, node, results)\n\n  def _submatch(self, node, results=None):\n    \"\"\"\n        Match the pattern's content to the node's children.\n\n        This assumes the node type matches and self.content is not None.\n\n        Returns True if it matches, False if not.\n\n        If results is not None, it must be a dict which will be\n        updated with the nodes matching named subpatterns.\n\n        When returning False, the results dict may still be updated.\n        \"\"\"\n    return self.content == node.value\n\n\nclass NodePattern(BasePattern):\n\n  wildcards = False\n\n  def __init__(self, type=None, content=None, name=None):\n    \"\"\"\n        Initializer.  Takes optional type, content, and name.\n\n        The type, if given, must be a symbol type (>= 256).  If the\n        type is None this matches *any* single node (leaf or not),\n        except if content is not None, in which it only matches\n        non-leaf nodes that also match the content pattern.\n\n        The content, if not None, must be a sequence of Patterns that\n        must match the node's children exactly.  If the content is\n        given, the type must not be None.\n\n        If a name is given, the matching node is stored in the results\n        dict under that key.\n        \"\"\"\n    if type is not None:\n      assert type >= 256, type\n    if content is not None:\n      assert not isinstance(content, str), repr(content)\n      content = list(content)\n      for i, item in enumerate(content):\n        assert isinstance(item, BasePattern), (i, item)\n        if isinstance(item, WildcardPattern):\n          self.wildcards = True\n    self.type = type\n    self.content = content\n    self.name = name\n\n  def _submatch(self, node, results=None):\n    \"\"\"\n        Match the pattern's content to the node's children.\n\n        This assumes the node type matches and self.content is not None.\n\n        Returns True if it matches, False if not.\n\n        If results is not None, it must be a dict which will be\n        updated with the nodes matching named subpatterns.\n\n        When returning False, the results dict may still be updated.\n        \"\"\"\n    if self.wildcards:\n      for c, r in generate_matches(self.content, node.children):\n        if c == len(node.children):\n          if results is not None:\n            results.update(r)\n          return True\n      return False\n    if len(self.content) != len(node.children):\n      return False\n    for subpattern, child in zip(self.content, node.children):\n      if not subpattern.match(child, results):\n        return False\n    return True\n\n\nclass WildcardPattern(BasePattern):\n  \"\"\"\n    A wildcard pattern can match zero or more nodes.\n\n    This has all the flexibility needed to implement patterns like:\n\n    .*      .+      .?      .{m,n}\n    (a b c | d e | f)\n    (...)*  (...)+  (...)?  (...){m,n}\n\n    except it always uses non-greedy matching.\n    \"\"\"\n\n  def __init__(self, content=None, min=0, max=HUGE, name=None):\n    \"\"\"\n        Initializer.\n\n        Args:\n            content: optional sequence of subsequences of patterns;\n                     if absent, matches one node;\n                     if present, each subsequence is an alternative [*]\n            min: optional minimum number of times to match, default 0\n            max: optional maximum number of times to match, default HUGE\n            name: optional name assigned to this match\n\n        [*] Thus, if content is [[a, b, c], [d, e], [f, g, h]] this is\n            equivalent to (a b c | d e | f g h); if content is None,\n            this is equivalent to '.' in regular expression terms.\n            The min and max parameters work as follows:\n                min=0, max=maxint: .*\n                min=1, max=maxint: .+\n                min=0, max=1: .?\n                min=1, max=1: .\n            If content is not None, replace the dot with the parenthesized\n            list of alternatives, e.g. (a b c | d e | f g h)*\n        \"\"\"\n    assert 0 <= min <= max <= HUGE, (min, max)\n    if content is not None:\n      content = tuple(map(tuple, content))  # Protect against alterations\n      # Check sanity of alternatives\n      assert len(content), repr(content)  # Can't have zero alternatives\n      for alt in content:\n        assert len(alt), repr(alt)  # Can have empty alternatives\n    self.content = content\n    self.min = min\n    self.max = max\n    self.name = name\n\n  def optimize(self):\n    \"\"\"Optimize certain stacked wildcard patterns.\"\"\"\n    subpattern = None\n    if (self.content is not None and len(self.content) == 1 and\n        len(self.content[0]) == 1):\n      subpattern = self.content[0][0]\n    if self.min == 1 and self.max == 1:\n      if self.content is None:\n        return NodePattern(name=self.name)\n      if subpattern is not None and self.name == subpattern.name:\n        return subpattern.optimize()\n    if (self.min <= 1 and isinstance(subpattern, WildcardPattern) and\n        subpattern.min <= 1 and self.name == subpattern.name):\n      return WildcardPattern(subpattern.content, self.min * subpattern.min,\n                             self.max * subpattern.max, subpattern.name)\n    return self\n\n  def match(self, node, results=None):\n    \"\"\"Does this pattern exactly match a node?\"\"\"\n    return self.match_seq([node], results)\n\n  def match_seq(self, nodes, results=None):\n    \"\"\"Does this pattern exactly match a sequence of nodes?\"\"\"\n    for c, r in self.generate_matches(nodes):\n      if c == len(nodes):\n        if results is not None:\n          results.update(r)\n          if self.name:\n            results[self.name] = list(nodes)\n        return True\n    return False\n\n  def generate_matches(self, nodes):\n    \"\"\"\n        Generator yielding matches for a sequence of nodes.\n\n        Args:\n            nodes: sequence of nodes\n\n        Yields:\n            (count, results) tuples where:\n            count: the match comprises nodes[:count];\n            results: dict containing named submatches.\n        \"\"\"\n    if self.content is None:\n      # Shortcut for special case (see __init__.__doc__)\n      for count in range(self.min, 1 + min(len(nodes), self.max)):\n        r = {}\n        if self.name:\n          r[self.name] = nodes[:count]\n        yield count, r\n    elif self.name == 'bare_name':\n      yield self._bare_name_matches(nodes)\n    else:\n      # The reason for this is that hitting the recursion limit usually\n      # results in some ugly messages about how RuntimeErrors are being\n      # ignored. We only have to do this on CPython, though, because other\n      # implementations don't have this nasty bug in the first place.\n      if hasattr(sys, 'getrefcount'):\n        save_stderr = sys.stderr\n        sys.stderr = StringIO()\n      try:\n        for count, r in self._recursive_matches(nodes, 0):\n          if self.name:\n            r[self.name] = nodes[:count]\n          yield count, r\n      except RuntimeError:\n        # We fall back to the iterative pattern matching scheme if the recursive\n        # scheme hits the recursion limit.\n        for count, r in self._iterative_matches(nodes):\n          if self.name:\n            r[self.name] = nodes[:count]\n          yield count, r\n      finally:\n        if hasattr(sys, 'getrefcount'):\n          sys.stderr = save_stderr\n\n  def _iterative_matches(self, nodes):\n    \"\"\"Helper to iteratively yield the matches.\"\"\"\n    nodelen = len(nodes)\n    if 0 >= self.min:\n      yield 0, {}\n\n    results = []\n    # generate matches that use just one alt from self.content\n    for alt in self.content:\n      for c, r in generate_matches(alt, nodes):\n        yield c, r\n        results.append((c, r))\n\n    # for each match, iterate down the nodes\n    while results:\n      new_results = []\n      for c0, r0 in results:\n        # stop if the entire set of nodes has been matched\n        if c0 < nodelen and c0 <= self.max:\n          for alt in self.content:\n            for c1, r1 in generate_matches(alt, nodes[c0:]):\n              if c1 > 0:\n                r = {}\n                r.update(r0)\n                r.update(r1)\n                yield c0 + c1, r\n                new_results.append((c0 + c1, r))\n      results = new_results\n\n  def _bare_name_matches(self, nodes):\n    \"\"\"Special optimized matcher for bare_name.\"\"\"\n    count = 0\n    r = {}\n    done = False\n    max = len(nodes)\n    while not done and count < max:\n      done = True\n      for leaf in self.content:\n        if leaf[0].match(nodes[count], r):\n          count += 1\n          done = False\n          break\n    r[self.name] = nodes[:count]\n    return count, r\n\n  def _recursive_matches(self, nodes, count):\n    \"\"\"Helper to recursively yield the matches.\"\"\"\n    assert self.content is not None\n    if count >= self.min:\n      yield 0, {}\n    if count < self.max:\n      for alt in self.content:\n        for c0, r0 in generate_matches(alt, nodes):\n          for c1, r1 in self._recursive_matches(nodes[c0:], count + 1):\n            r = {}\n            r.update(r0)\n            r.update(r1)\n            yield c0 + c1, r\n\n\nclass NegatedPattern(BasePattern):\n\n  def __init__(self, content=None):\n    \"\"\"\n        Initializer.\n\n        The argument is either a pattern or None.  If it is None, this\n        only matches an empty sequence (effectively '$' in regex\n        lingo).  If it is not None, this matches whenever the argument\n        pattern doesn't have any matches.\n        \"\"\"\n    if content is not None:\n      assert isinstance(content, BasePattern), repr(content)\n    self.content = content\n\n  def match(self, node):\n    # We never match a node in its entirety\n    return False\n\n  def match_seq(self, nodes):\n    # We only match an empty sequence of nodes in its entirety\n    return len(nodes) == 0\n\n  def generate_matches(self, nodes):\n    if self.content is None:\n      # Return a match if there is an empty sequence\n      if len(nodes) == 0:\n        yield 0, {}\n    else:\n      # Return a match if the argument pattern has no matches\n      for c, r in self.content.generate_matches(nodes):\n        return\n      yield 0, {}\n\n\ndef generate_matches(patterns, nodes):\n  \"\"\"\n    Generator yielding matches for a sequence of patterns and nodes.\n\n    Args:\n        patterns: a sequence of patterns\n        nodes: a sequence of nodes\n\n    Yields:\n        (count, results) tuples where:\n        count: the entire sequence of patterns matches nodes[:count];\n        results: dict containing named submatches.\n        \"\"\"\n  if not patterns:\n    yield 0, {}\n  else:\n    p, rest = patterns[0], patterns[1:]\n    for c0, r0 in p.generate_matches(nodes):\n      if not rest:\n        yield c0, r0\n      else:\n        for c1, r1 in generate_matches(rest, nodes[c0:]):\n          r = {}\n          r.update(r0)\n          r.update(r1)\n          yield c0 + c1, r\n"
  },
  {
    "path": "third_party/yapf_third_party/yapf_diff/LICENSE",
    "content": "                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n    TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n    1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n    2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n    3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n    4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n    5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n    6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n    7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n    8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n    9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n    END OF TERMS AND CONDITIONS\n\n    APPENDIX: How to apply the Apache License to your work.\n\n      To apply the Apache License to your work, attach the following\n      boilerplate notice, with the fields enclosed by brackets \"[]\"\n      replaced with your own identifying information. (Don't include\n      the brackets!)  The text should be enclosed in the appropriate\n      comment syntax for the file format. We also recommend that a\n      file or class name and description of purpose be included on the\n      same \"printed page\" as the copyright notice for easier\n      identification within third-party archives.\n\n    Copyright [yyyy] [name of copyright owner]\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n\n\n--- LLVM Exceptions to the Apache 2.0 License ----\n\nAs an exception, if, as a result of your compiling your source code, portions\nof this Software are embedded into an Object form of such source code, you\nmay redistribute such embedded portions in such Object form without complying\nwith the conditions of Sections 4(a), 4(b) and 4(d) of the License.\n\nIn addition, if you combine or link compiled forms of this Software with\nsoftware that is licensed under the GPLv2 (\"Combined Software\") and if a\ncourt of competent jurisdiction determines that the patent provision (Section\n3), the indemnity provision (Section 9) or other Section of the License\nconflicts with the conditions of the GPLv2, you may retroactively and\nprospectively choose to deem waived or otherwise exclude such Section(s) of\nthe License, but only in their entirety and only with respect to the Combined\nSoftware.\n"
  },
  {
    "path": "third_party/yapf_third_party/yapf_diff/__init__.py",
    "content": ""
  },
  {
    "path": "third_party/yapf_third_party/yapf_diff/yapf_diff.py",
    "content": "# Modified copy of clang-format-diff.py that works with yapf.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\") with LLVM\n# Exceptions; you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     https://llvm.org/LICENSE.txt\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"\nThis script reads input from a unified diff and reformats all the changed\nlines. This is useful to reformat all the lines touched by a specific patch.\nExample usage for git/svn users:\n\n  git diff -U0 --no-color --relative HEAD^ | yapf-diff -i\n  svn diff --diff-cmd=diff -x-U0 | yapf-diff -p0 -i\n\nIt should be noted that the filename contained in the diff is used unmodified\nto determine the source file to update. Users calling this script directly\nshould be careful to ensure that the path in the diff is correct relative to the\ncurrent working directory.\n\"\"\"\n\nimport argparse\nimport difflib\nimport re\nimport subprocess\nimport sys\nfrom io import StringIO\n\n\ndef main():\n  parser = argparse.ArgumentParser(\n      description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)\n  parser.add_argument(\n      '-i',\n      '--in-place',\n      action='store_true',\n      default=False,\n      help='apply edits to files instead of displaying a diff')\n  parser.add_argument(\n      '-p',\n      '--prefix',\n      metavar='NUM',\n      default=1,\n      help='strip the smallest prefix containing P slashes')\n  parser.add_argument(\n      '--regex',\n      metavar='PATTERN',\n      default=None,\n      help='custom pattern selecting file paths to reformat '\n      '(case sensitive, overrides -iregex)')\n  parser.add_argument(\n      '--iregex',\n      metavar='PATTERN',\n      default=r'.*\\.(py)',\n      help='custom pattern selecting file paths to reformat '\n      '(case insensitive, overridden by -regex)')\n  parser.add_argument(\n      '-v',\n      '--verbose',\n      action='store_true',\n      help='be more verbose, ineffective without -i')\n  parser.add_argument(\n      '--style',\n      help='specify formatting style: either a style name (for '\n      'example \"pep8\" or \"google\"), or the name of a file with '\n      'style settings. The default is pep8 unless a '\n      '.style.yapf or setup.cfg file located in one of the '\n      'parent directories of the source file (or current '\n      'directory for stdin)')\n  parser.add_argument(\n      '--binary', default='yapf', help='location of binary to use for yapf')\n  args = parser.parse_args()\n\n  # Extract changed lines for each file.\n  filename = None\n  lines_by_file = {}\n  for line in sys.stdin:\n    match = re.search(r'^\\+\\+\\+\\ (.*?/){%s}(\\S*)' % args.prefix, line)\n    if match:\n      filename = match.group(2)\n    if filename is None:\n      continue\n\n    if args.regex is not None:\n      if not re.match('^%s$' % args.regex, filename):\n        continue\n    elif not re.match('^%s$' % args.iregex, filename, re.IGNORECASE):\n      continue\n\n    match = re.search(r'^@@.*\\+(\\d+)(,(\\d+))?', line)\n    if match:\n      start_line = int(match.group(1))\n      line_count = 1\n      if match.group(3):\n        line_count = int(match.group(3))\n      if line_count == 0:\n        continue\n      end_line = start_line + line_count - 1\n      lines_by_file.setdefault(filename, []).extend(\n          ['--lines', str(start_line) + '-' + str(end_line)])\n\n  # Reformat files containing changes in place.\n  for filename, lines in lines_by_file.items():\n    if args.in_place and args.verbose:\n      print('Formatting {}'.format(filename))\n    command = [args.binary, filename]\n    if args.in_place:\n      command.append('-i')\n    command.extend(lines)\n    if args.style:\n      command.extend(['--style', args.style])\n    p = subprocess.Popen(\n        command,\n        stdout=subprocess.PIPE,\n        stderr=None,\n        stdin=subprocess.PIPE,\n        universal_newlines=True)\n    stdout, stderr = p.communicate()\n    if p.returncode != 0:\n      sys.exit(p.returncode)\n\n    if not args.in_place:\n      with open(filename) as f:\n        code = f.readlines()\n      formatted_code = StringIO(stdout).readlines()\n      diff = difflib.unified_diff(code, formatted_code, filename, filename,\n                                  '(before formatting)', '(after formatting)')\n      diff_string = ''.join(diff)\n      if len(diff_string) > 0:\n        sys.stdout.write(diff_string)\n\n\nif __name__ == '__main__':\n  main()\n"
  },
  {
    "path": "tox.ini",
    "content": "[tox]\nrequires =\n    tox<4\n    tox-pyenv\n    tox-wheel\nenvlist = py37,py38,py39,py310,py311,py312\n# tox-wheel alias for `wheel_pep517 = true`\nisolated_build = True\ndistshare = ./dist\n\n[testenv]\nwheel = True\nwheel_build_env = bdist_wheel\ncommands = python -m unittest discover -p '*_test.py' yapftests/\n\n[testenv:bdist_wheel]\n\n[testenv:sdist]\nwheel = False\n"
  },
  {
    "path": "yapf/__init__.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"YAPF.\n\nYAPF uses the algorithm in clang-format to figure out the \"best\" formatting for\nPython code. It looks at the program as a series of \"unwrappable lines\" ---\ni.e., lines which, if there were no column limit, we would place all tokens on\nthat line. It then uses a priority queue to figure out what the best formatting\nis --- i.e., the formatting with the least penalty.\n\nIt differs from tools like autopep8 in that it doesn't just look for\nviolations of the style guide, but looks at the module as a whole, making\nformatting decisions based on what's the best format for each line.\n\nIf no filenames are specified, YAPF reads the code from stdin.\n\"\"\"\n\nimport argparse\nimport codecs\nimport io\nimport logging\nimport os\nimport sys\n\nfrom yapf._version import __version__\nfrom yapf.yapflib import errors\nfrom yapf.yapflib import file_resources\nfrom yapf.yapflib import style\nfrom yapf.yapflib import yapf_api\n\n\ndef _raw_input():\n  wrapper = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')\n  return wrapper.buffer.raw.readall().decode('utf-8')\n\n\ndef _removeBOM(source):\n  \"\"\"Remove any Byte-order-Mark bytes from the beginning of a file.\"\"\"\n  bom = codecs.BOM_UTF8\n  bom = bom.decode('utf-8')\n  if source.startswith(bom):\n    return source[len(bom):]\n  return source\n\n\ndef main(argv):\n  \"\"\"Main program.\n\n  Arguments:\n    argv: command-line arguments, such as sys.argv (including the program name\n      in argv[0]).\n\n  Returns:\n    Zero on successful program termination, non-zero otherwise.\n    With --diff: zero if there were no changes, non-zero otherwise.\n\n  Raises:\n    YapfError: if none of the supplied files were Python files.\n  \"\"\"\n  parser = _BuildParser()\n  args = parser.parse_args(argv[1:])\n  style_config = args.style\n\n  if args.style_help:\n    _PrintHelp(args)\n    return 0\n\n  if args.lines and len(args.files) > 1:\n    parser.error('cannot use -l/--lines with more than one file')\n\n  lines = _GetLines(args.lines) if args.lines is not None else None\n  if not args.files:\n    # No arguments specified. Read code from stdin.\n    if args.in_place or args.diff:\n      parser.error('cannot use --in-place or --diff flags when reading '\n                   'from stdin')\n\n    original_source = []\n    while True:\n      # Test that sys.stdin has the \"closed\" attribute. When using pytest, it\n      # co-opts sys.stdin, which makes the \"main_tests.py\" fail. This is gross.\n      if hasattr(sys.stdin, 'closed') and sys.stdin.closed:\n        break\n      try:\n        # Use 'raw_input' instead of 'sys.stdin.read', because otherwise the\n        # user will need to hit 'Ctrl-D' more than once if they're inputting\n        # the program by hand. 'raw_input' throws an EOFError exception if\n        # 'Ctrl-D' is pressed, which makes it easy to bail out of this loop.\n        original_source.append(_raw_input())\n      except EOFError:\n        break\n      except KeyboardInterrupt:\n        return 1\n\n    if style_config is None and not args.no_local_style:\n      style_config = file_resources.GetDefaultStyleForDir(os.getcwd())\n\n    source = [line.rstrip() for line in original_source]\n    source[0] = _removeBOM(source[0])\n\n    try:\n      reformatted_source, _ = yapf_api.FormatCode(\n          str('\\n'.join(source).replace('\\r\\n', '\\n') + '\\n'),\n          filename='<stdin>',\n          style_config=style_config,\n          lines=lines)\n    except errors.YapfError:\n      raise\n    except Exception as e:\n      raise errors.YapfError(errors.FormatErrorMsg(e))\n\n    file_resources.WriteReformattedCode('<stdout>', reformatted_source)\n    return 0\n\n  # Get additional exclude patterns from ignorefile\n  exclude_patterns_from_ignore_file = file_resources.GetExcludePatternsForDir(\n      os.getcwd())\n\n  files = file_resources.GetCommandLineFiles(args.files, args.recursive,\n                                             (args.exclude or []) +\n                                             exclude_patterns_from_ignore_file)\n  if not files:\n    raise errors.YapfError('input filenames did not match any python files')\n\n  changed = FormatFiles(\n      files,\n      lines,\n      style_config=args.style,\n      no_local_style=args.no_local_style,\n      in_place=args.in_place,\n      print_diff=args.diff,\n      parallel=args.parallel,\n      quiet=args.quiet,\n      verbose=args.verbose,\n      print_modified=args.print_modified)\n  return 1 if changed and (args.diff or args.quiet) else 0\n\n\ndef _PrintHelp(args):\n  \"\"\"Prints the help menu.\"\"\"\n\n  if args.style is None and not args.no_local_style:\n    args.style = file_resources.GetDefaultStyleForDir(os.getcwd())\n  style.SetGlobalStyle(style.CreateStyleFromConfig(args.style))\n  print('[style]')\n  for option, docstring in sorted(style.Help().items()):\n    for line in docstring.splitlines():\n      print('#', line and ' ' or '', line, sep='')\n    option_value = style.Get(option)\n    if isinstance(option_value, (set, list)):\n      option_value = ', '.join(map(str, option_value))\n    print(option.lower(), '=', option_value, sep='')\n    print()\n\n\ndef FormatFiles(filenames,\n                lines,\n                style_config=None,\n                no_local_style=False,\n                in_place=False,\n                print_diff=False,\n                parallel=False,\n                quiet=False,\n                verbose=False,\n                print_modified=False):\n  \"\"\"Format a list of files.\n\n  Arguments:\n    filenames: (list of unicode) A list of files to reformat.\n    lines: (list of tuples of integers) A list of tuples of lines, [start, end],\n      that we want to format. The lines are 1-based indexed. This argument\n      overrides the 'args.lines'. It can be used by third-party code (e.g.,\n      IDEs) when reformatting a snippet of code.\n    style_config: (string) Style name or file path.\n    no_local_style: (string) If style_config is None don't search for\n      directory-local style configuration.\n    in_place: (bool) Modify the files in place.\n    print_diff: (bool) Instead of returning the reformatted source, return a\n      diff that turns the formatted source into reformatter source.\n    parallel: (bool) True if should format multiple files in parallel.\n    quiet: (bool) True if should output nothing.\n    verbose: (bool) True if should print out filenames while processing.\n    print_modified: (bool) True if should print out filenames of modified files.\n\n  Returns:\n    True if the source code changed in any of the files being formatted.\n  \"\"\"\n  changed = False\n  if parallel:\n    import concurrent.futures  # pylint: disable=g-import-not-at-top\n    import multiprocessing  # pylint: disable=g-import-not-at-top\n    workers = min(multiprocessing.cpu_count(), len(filenames))\n    with concurrent.futures.ProcessPoolExecutor(workers) as executor:\n      future_formats = [\n          executor.submit(_FormatFile, filename, lines, style_config,\n                          no_local_style, in_place, print_diff, quiet, verbose,\n                          print_modified) for filename in filenames\n      ]\n      for future in concurrent.futures.as_completed(future_formats):\n        changed |= future.result()\n  else:\n    for filename in filenames:\n      changed |= _FormatFile(filename, lines, style_config, no_local_style,\n                             in_place, print_diff, quiet, verbose,\n                             print_modified)\n  return changed\n\n\ndef _FormatFile(filename,\n                lines,\n                style_config=None,\n                no_local_style=False,\n                in_place=False,\n                print_diff=False,\n                quiet=False,\n                verbose=False,\n                print_modified=False):\n  \"\"\"Format an individual file.\"\"\"\n  if verbose and not quiet:\n    print(f'Reformatting {filename}')\n\n  if style_config is None and not no_local_style:\n    style_config = file_resources.GetDefaultStyleForDir(\n        os.path.dirname(filename))\n\n  try:\n    reformatted_code, encoding, has_change = yapf_api.FormatFile(\n        filename,\n        in_place=in_place,\n        style_config=style_config,\n        lines=lines,\n        print_diff=print_diff,\n        logger=logging.warning)\n  except errors.YapfError:\n    raise\n  except Exception as e:\n    raise errors.YapfError(errors.FormatErrorMsg(e))\n\n  if not in_place and not quiet and reformatted_code:\n    file_resources.WriteReformattedCode(filename, reformatted_code, encoding,\n                                        in_place)\n  if print_modified and has_change and in_place and not quiet:\n    print(f'Formatted {filename}')\n  return has_change\n\n\ndef _GetLines(line_strings):\n  \"\"\"Parses the start and end lines from a line string like 'start-end'.\n\n  Arguments:\n    line_strings: (array of string) A list of strings representing a line\n      range like 'start-end'.\n\n  Returns:\n    A list of tuples of the start and end line numbers.\n\n  Raises:\n    ValueError: If the line string failed to parse or was an invalid line range.\n  \"\"\"\n  lines = []\n  for line_string in line_strings:\n    # The 'list' here is needed by Python 3.\n    line = list(map(int, line_string.split('-', 1)))\n    if line[0] < 1:\n      raise errors.YapfError('invalid start of line range: %r' % line)\n    if line[0] > line[1]:\n      raise errors.YapfError('end comes before start in line range: %r' % line)\n    lines.append(tuple(line))\n  return lines\n\n\ndef _BuildParser():\n  \"\"\"Constructs the parser for the command line arguments.\n\n  Returns:\n    An ArgumentParser instance for the CLI.\n  \"\"\"\n  parser = argparse.ArgumentParser(\n      prog='yapf', description='Formatter for Python code.')\n  parser.add_argument(\n      '-v',\n      '--version',\n      action='version',\n      version='%(prog)s {}'.format(__version__))\n\n  diff_inplace_quiet_group = parser.add_mutually_exclusive_group()\n  diff_inplace_quiet_group.add_argument(\n      '-d',\n      '--diff',\n      action='store_true',\n      help='print the diff for the fixed source')\n  diff_inplace_quiet_group.add_argument(\n      '-i',\n      '--in-place',\n      action='store_true',\n      help='make changes to files in place')\n  diff_inplace_quiet_group.add_argument(\n      '-q',\n      '--quiet',\n      action='store_true',\n      help='output nothing and set return value')\n\n  lines_recursive_group = parser.add_mutually_exclusive_group()\n  lines_recursive_group.add_argument(\n      '-r',\n      '--recursive',\n      action='store_true',\n      help='run recursively over directories')\n  lines_recursive_group.add_argument(\n      '-l',\n      '--lines',\n      metavar='START-END',\n      action='append',\n      default=None,\n      help='range of lines to reformat, one-based')\n\n  parser.add_argument(\n      '-e',\n      '--exclude',\n      metavar='PATTERN',\n      action='append',\n      default=None,\n      help='patterns for files to exclude from formatting')\n  parser.add_argument(\n      '--style',\n      action='store',\n      help=('specify formatting style: either a style name (for example \"pep8\" '\n            'or \"google\"), or the name of a file with style settings. The '\n            'default is pep8 unless a %s or %s or %s file located in the same '\n            'directory as the source or one of its parent directories '\n            '(for stdin, the current directory is used).' %\n            (style.LOCAL_STYLE, style.SETUP_CONFIG, style.PYPROJECT_TOML)))\n  parser.add_argument(\n      '--style-help',\n      action='store_true',\n      help=('show style settings and exit; this output can be '\n            'saved to .style.yapf to make your settings '\n            'permanent'))\n  parser.add_argument(\n      '--no-local-style',\n      action='store_true',\n      help=\"don't search for local style definition\")\n  parser.add_argument(\n      '-p',\n      '--parallel',\n      action='store_true',\n      help=('run YAPF in parallel when formatting multiple files.'))\n  parser.add_argument(\n      '-m',\n      '--print-modified',\n      action='store_true',\n      help='print out file names of modified files')\n  parser.add_argument(\n      '-vv',\n      '--verbose',\n      action='store_true',\n      help='print out file names while processing')\n\n  parser.add_argument(\n      'files', nargs='*', help='reads from stdin when no files are specified.')\n  return parser\n\n\ndef run_main():  # pylint: disable=invalid-name\n  try:\n    sys.exit(main(sys.argv))\n  except errors.YapfError as e:\n    sys.stderr.write('yapf: ' + str(e) + '\\n')\n    sys.exit(1)\n\n\nif __name__ == '__main__':\n  run_main()\n"
  },
  {
    "path": "yapf/__main__.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Main entry point.\"\"\"\n# pylint: disable=invalid-name\nimport yapf\n\nyapf.run_main()\n"
  },
  {
    "path": "yapf/_version.py",
    "content": "__version__ = '0.43.0'\n"
  },
  {
    "path": "yapf/pyparser/__init__.py",
    "content": "# Copyright 2022 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n"
  },
  {
    "path": "yapf/pyparser/pyparser.py",
    "content": "# Copyright 2022 Bill Wendling, All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Simple Python Parser\n\nParse Python code into a list of logical lines, represented by LogicalLine\nobjects. This uses Python's tokenizer to generate the tokens. As such, YAPF must\nbe run with the appropriate Python version---Python >=3.7 for Python 3.7 code,\nPython >=3.8 for Python 3.8 code, etc.\n\nThis parser uses Python's native \"tokenizer\" module to generate a list of tokens\nfor the source code. It then uses Python's native \"ast\" module to assign\nsubtypes, calculate split penalties, etc.\n\nA \"logical line\" produced by Python's \"tokenizer\" module ends with a\ntokenize.NEWLINE, rather than a tokenize.NL, making it easy to separate them\nout. Comments all end with a tokentizer.NL, so we need to make sure we don't\nerrantly pick up non-comment tokens when parsing comment blocks.\n\n  ParseCode(): parse the code producing a list of logical lines.\n\"\"\"\n\n# TODO: Call from yapf_api.FormatCode.\n\nimport ast\nimport codecs\nimport os\nimport token\nimport tokenize\nfrom io import StringIO\nfrom tokenize import TokenInfo\n\nfrom yapf.pyparser import split_penalty_visitor\nfrom yapf.yapflib import format_token\nfrom yapf.yapflib import logical_line\n\nCONTINUATION = token.N_TOKENS\n\n\ndef ParseCode(unformatted_source, filename='<unknown>'):\n  \"\"\"Parse a string of Python code into logical lines.\n\n  This provides an alternative entry point to YAPF.\n\n  Arguments:\n    unformatted_source: (unicode) The code to format.\n    filename: (unicode) The name of the file being reformatted.\n\n  Returns:\n    A list of LogicalLines.\n\n  Raises:\n    An exception is raised if there's an error during AST parsing.\n  \"\"\"\n  if not unformatted_source.endswith(os.linesep):\n    unformatted_source += os.linesep\n\n  try:\n    ast_tree = ast.parse(unformatted_source, filename)\n    ast.fix_missing_locations(ast_tree)\n    readline = StringIO(unformatted_source).readline\n    tokens = tokenize.generate_tokens(readline)\n  except Exception:\n    raise\n\n  logical_lines = _CreateLogicalLines(tokens)\n\n  # Process the logical lines.\n  split_penalty_visitor.SplitPenalty(logical_lines).visit(ast_tree)\n\n  return logical_lines\n\n\ndef _CreateLogicalLines(tokens):\n  \"\"\"Separate tokens into logical lines.\n\n  Arguments:\n    tokens: (list of tokenizer.TokenInfo) Tokens generated by tokenizer.\n\n  Returns:\n    A list of LogicalLines.\n  \"\"\"\n  formatted_tokens = []\n\n  # Convert tokens into \"TokenInfo\" and add tokens for continuation markers.\n  prev_tok = None\n  for tok in tokens:\n    tok = TokenInfo(*tok)\n\n    if (prev_tok and prev_tok.line.rstrip().endswith('\\\\') and\n        prev_tok.start[0] < tok.start[0]):\n      ctok = TokenInfo(\n          type=CONTINUATION,\n          string='\\\\',\n          start=(prev_tok.start[0], prev_tok.start[1] + 1),\n          end=(prev_tok.end[0], prev_tok.end[0] + 2),\n          line=prev_tok.line)\n      ctok.lineno = ctok.start[0]\n      ctok.column = ctok.start[1]\n      ctok.value = '\\\\'\n      formatted_tokens.append(format_token.FormatToken(ctok, 'CONTINUATION'))\n\n    tok.lineno = tok.start[0]\n    tok.column = tok.start[1]\n    tok.value = tok.string\n    formatted_tokens.append(\n        format_token.FormatToken(tok, token.tok_name[tok.type]))\n    prev_tok = tok\n\n  # Generate logical lines.\n  logical_lines, cur_logical_line = [], []\n  depth = 0\n  for tok in formatted_tokens:\n    if tok.type == tokenize.ENDMARKER:\n      break\n\n    if tok.type == tokenize.NEWLINE:\n      # End of a logical line.\n      logical_lines.append(logical_line.LogicalLine(depth, cur_logical_line))\n      cur_logical_line = []\n    elif tok.type == tokenize.INDENT:\n      depth += 1\n    elif tok.type == tokenize.DEDENT:\n      depth -= 1\n    elif tok.type == tokenize.NL:\n      pass\n    else:\n      if (cur_logical_line and not tok.type == tokenize.COMMENT and\n          cur_logical_line[0].type == tokenize.COMMENT):\n        # We were parsing a comment block, but now we have real code to worry\n        # about. Store the comment and carry on.\n        logical_lines.append(logical_line.LogicalLine(depth, cur_logical_line))\n        cur_logical_line = []\n\n      cur_logical_line.append(tok)\n\n  # Link the FormatTokens in each line together to form a doubly linked list.\n  for line in logical_lines:\n    previous = line.first\n    bracket_stack = [previous] if previous.OpensScope() else []\n    for tok in line.tokens[1:]:\n      tok.previous_token = previous\n      previous.next_token = tok\n      previous = tok\n\n      # Set up the \"matching_bracket\" attribute.\n      if tok.OpensScope():\n        bracket_stack.append(tok)\n      elif tok.ClosesScope():\n        bracket_stack[-1].matching_bracket = tok\n        tok.matching_bracket = bracket_stack.pop()\n\n  return logical_lines\n"
  },
  {
    "path": "yapf/pyparser/pyparser_utils.py",
    "content": "# Copyright 2022 Bill Wendling, All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"PyParser-related utilities.\n\nThis module collects various utilities related to the parse trees produced by\nthe pyparser.\n\n  GetLogicalLine: produces a list of tokens from the logical lines within a\n    range.\n  GetTokensInSubRange: produces a sublist of tokens from a current token list\n    within a range.\n  GetTokenIndex: Get the index of a token.\n  GetNextTokenIndex: Get the index of the next token after a given position.\n  GetPrevTokenIndex: Get the index of the previous token before a given\n    position.\n  TokenStart: Convenience function to return the token's start as a tuple.\n  TokenEnd: Convenience function to return the token's end as a tuple.\n\"\"\"\n\n\ndef GetLogicalLine(logical_lines, node):\n  \"\"\"Get a list of tokens within the node's range from the logical lines.\"\"\"\n  start = TokenStart(node)\n  end = TokenEnd(node)\n  tokens = []\n\n  for line in logical_lines:\n    if line.start > end:\n      break\n    if line.start <= start or line.end >= end:\n      tokens.extend(GetTokensInSubRange(line.tokens, node))\n\n  return tokens\n\n\ndef GetTokensInSubRange(tokens, node):\n  \"\"\"Get a subset of tokens representing the node.\"\"\"\n  start = TokenStart(node)\n  end = TokenEnd(node)\n  tokens_in_range = []\n\n  for tok in tokens:\n    tok_range = (tok.lineno, tok.column)\n    if tok_range >= start and tok_range < end:\n      tokens_in_range.append(tok)\n\n  return tokens_in_range\n\n\ndef GetTokenIndex(tokens, pos):\n  \"\"\"Get the index of the token at pos.\"\"\"\n  for index, token in enumerate(tokens):\n    if (token.lineno, token.column) == pos:\n      return index\n\n  return None\n\n\ndef GetNextTokenIndex(tokens, pos):\n  \"\"\"Get the index of the next token after pos.\"\"\"\n  for index, token in enumerate(tokens):\n    if (token.lineno, token.column) >= pos:\n      return index\n\n  return None\n\n\ndef GetPrevTokenIndex(tokens, pos):\n  \"\"\"Get the index of the previous token before pos.\"\"\"\n  for index, token in enumerate(tokens):\n    if index > 0 and (token.lineno, token.column) >= pos:\n      return index - 1\n\n  return None\n\n\ndef TokenStart(node):\n  return (node.lineno, node.col_offset)\n\n\ndef TokenEnd(node):\n  return (node.end_lineno, node.end_col_offset)\n\n\n#############################################################################\n# Code for debugging                                                        #\n#############################################################################\n\n\ndef AstDump(node):\n  import ast\n  print(ast.dump(node, include_attributes=True, indent=4))\n"
  },
  {
    "path": "yapf/pyparser/pyparser_visitor.py.tmpl",
    "content": "# Copyright 2022 Bill Wendling, All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"AST visitor template.\n\nThis is a template for a pyparser visitor. Example use:\n\n    import ast\n    from io import StringIO\n\n    from yapf.pyparser import pyparser_visitor\n\n    def parse_code(source, filename):\n        ast_tree = ast.parse(source, filename)\n        readline = StringIO(source).readline\n        tokens = tokenize.generate_tokens(readline)\n        logical_lines = _CreateLogicalLines(tokens)\n\n        pyparser_visitor.Visitor(logical_lines).visit(ast_tree)\n\"\"\"\n\nimport ast\n\n\n# This is a skeleton of an AST visitor.\nclass Visitor(ast.NodeVisitor):\n  \"\"\"Compute split penalties between tokens.\"\"\"\n\n  def __init__(self, logical_lines):\n    super(Visitor, self).__init__()\n    self.logical_lines = logical_lines\n\n  ############################################################################\n  # Statements                                                               #\n  ############################################################################\n\n  def visit_FunctionDef(self, node):\n    # FunctionDef(name=Name,\n    #             args=arguments(\n    #                 posonlyargs=[],\n    #                 args=[],\n    #                 vararg=[],\n    #                 kwonlyargs=[],\n    #                 kw_defaults=[],\n    #                 defaults=[]),\n    #             body=[...],\n    #             decorator_list=[Expr_1, Expr_2, ..., Expr_n],\n    #             keywords=[])\n    return self.generic_visit(node)\n\n  def visit_AsyncFunctionDef(self, node):\n    # AsyncFunctionDef(name=Name,\n    #                  args=arguments(\n    #                      posonlyargs=[],\n    #                      args=[],\n    #                      vararg=[],\n    #                      kwonlyargs=[],\n    #                      kw_defaults=[],\n    #                      defaults=[]),\n    #                  body=[...],\n    #                  decorator_list=[Expr_1, Expr_2, ..., Expr_n],\n    #                  keywords=[])\n    return self.generic_visit(node)\n\n  def visit_ClassDef(self, node):\n    # ClassDef(name=Name,\n    #          bases=[Expr_1, Expr_2, ..., Expr_n],\n    #          keywords=[],\n    #          body=[],\n    #          decorator_list=[Expr_1, Expr_2, ..., Expr_m])\n    return self.generic_visit(node)\n\n  def visit_Return(self, node):\n    # Return(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_Delete(self, node):\n    # Delete(targets=[Expr_1, Expr_2, ..., Expr_n])\n    return self.generic_visit(node)\n\n  def visit_Assign(self, node):\n    # Assign(targets=[Expr_1, Expr_2, ..., Expr_n],\n    #        value=Expr)\n    return self.generic_visit(node)\n\n  def visit_AugAssign(self, node):\n    # AugAssign(target=Name,\n    #           op=Add(),\n    #           value=Expr)\n    return self.generic_visit(node)\n\n  def visit_AnnAssign(self, node):\n    # AnnAssign(target=Name,\n    #           annotation=TypeName,\n    #           value=Expr,\n    #           simple=number)\n    return self.generic_visit(node)\n\n  def visit_For(self, node):\n    # For(target=Expr,\n    #     iter=Expr,\n    #     body=[...],\n    #     orelse=[...])\n    return self.generic_visit(node)\n\n  def visit_AsyncFor(self, node):\n    # AsyncFor(target=Expr,\n    #          iter=Expr,\n    #          body=[...],\n    #          orelse=[...])\n    return self.generic_visit(node)\n\n  def visit_While(self, node):\n    # While(test=Expr,\n    #       body=[...],\n    #       orelse=[...])\n    return self.generic_visit(node)\n\n  def visit_If(self, node):\n    # If(test=Expr,\n    #    body=[...],\n    #    orelse=[...])\n    return self.generic_visit(node)\n\n  def visit_With(self, node):\n    # With(items=[withitem_1, withitem_2, ..., withitem_n],\n    #      body=[...])\n    return self.generic_visit(node)\n\n  def visit_AsyncWith(self, node):\n    # AsyncWith(items=[withitem_1, withitem_2, ..., withitem_n],\n    #           body=[...])\n    return self.generic_visit(node)\n\n  def visit_Match(self, node):\n    # Match(subject=Expr,\n    #       cases=[\n    #           match_case(\n    #               pattern=pattern,\n    #               guard=Expr,\n    #               body=[...]),\n    #             ...\n    #       ])\n    return self.generic_visit(node)\n\n  def visit_Raise(self, node):\n    # Raise(exc=Expr)\n    return self.generic_visit(node)\n\n  def visit_Try(self, node):\n    # Try(body=[...],\n    #     handlers=[ExceptHandler_1, ExceptHandler_2, ..., ExceptHandler_b],\n    #     orelse=[...],\n    #     finalbody=[...])\n    return self.generic_visit(node)\n\n  def visit_Assert(self, node):\n    # Assert(test=Expr)\n    return self.generic_visit(node)\n\n  def visit_Import(self, node):\n    # Import(names=[\n    #            alias(\n    #                name=Identifier,\n    #                asname=Identifier),\n    #              ...\n    #        ])\n    return self.generic_visit(node)\n\n  def visit_ImportFrom(self, node):\n    # ImportFrom(module=Identifier,\n    #            names=[\n    #                alias(\n    #                    name=Identifier,\n    #                    asname=Identifier),\n    #                  ...\n    #            ],\n    #            level=num\n    return self.generic_visit(node)\n\n  def visit_Global(self, node):\n    # Global(names=[Identifier_1, Identifier_2, ..., Identifier_n])\n    return self.generic_visit(node)\n\n  def visit_Nonlocal(self, node):\n    # Nonlocal(names=[Identifier_1, Identifier_2, ..., Identifier_n])\n    return self.generic_visit(node)\n\n  def visit_Expr(self, node):\n    # Expr(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_Pass(self, node):\n    # Pass()\n    return self.generic_visit(node)\n\n  def visit_Break(self, node):\n    # Break()\n    return self.generic_visit(node)\n\n  def visit_Continue(self, node):\n    # Continue()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Expressions                                                              #\n  ############################################################################\n\n  def visit_BoolOp(self, node):\n    # BoolOp(op=And | Or,\n    #        values=[Expr_1, Expr_2, ..., Expr_n])\n    return self.generic_visit(node)\n\n  def visit_NamedExpr(self, node):\n    # NamedExpr(target=Name,\n    #           value=Expr)\n    return self.generic_visit(node)\n\n  def visit_BinOp(self, node):\n    # BinOp(left=LExpr\n    #       op=Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift |\n    #          RShift | BitOr | BitXor | BitAnd | FloorDiv\n    #       right=RExpr)\n    return self.generic_visit(node)\n\n  def visit_UnaryOp(self, node):\n    # UnaryOp(op=Not | USub | UAdd | Invert,\n    #         operand=Expr)\n    return self.generic_visit(node)\n\n  def visit_Lambda(self, node):\n    # Lambda(args=arguments(\n    #            posonlyargs=[],\n    #            args=[\n    #                arg(arg='a'),\n    #                arg(arg='b')],\n    #            kwonlyargs=[],\n    #            kw_defaults=[],\n    #            defaults=[]),\n    #        body=Expr)\n    return self.generic_visit(node)\n\n  def visit_IfExp(self, node):\n    # IfExp(test=TestExpr,\n    #       body=BodyExpr,\n    #       orelse=OrElseExpr)\n    return self.generic_visit(node)\n\n  def visit_Dict(self, node):\n    # Dict(keys=[Expr_1, Expr_2, ..., Expr_n],\n    #      values=[Expr_1, Expr_2, ..., Expr_n])\n    return self.generic_visit(node)\n\n  def visit_Set(self, node):\n    # Set(elts=[Expr_1, Expr_2, ..., Expr_n])\n    return self.generic_visit(node)\n\n  def visit_ListComp(self, node):\n    # ListComp(elt=Expr,\n    #          generators=[\n    #              comprehension(\n    #                  target=Expr,\n    #                  iter=Expr,\n    #                  ifs=[Expr_1, Expr_2, ..., Expr_n],\n    #                  is_async=0),\n    #               ...\n    #          ])\n    return self.generic_visit(node)\n\n  def visit_SetComp(self, node):\n    # SetComp(elt=Expr,\n    #         generators=[\n    #             comprehension(\n    #                 target=Expr,\n    #                 iter=Expr,\n    #                 ifs=[Expr_1, Expr_2, ..., Expr_n],\n    #                 is_async=0),\n    #           ...\n    #         ])\n    return self.generic_visit(node)\n\n  def visit_DictComp(self, node):\n    # DictComp(key=KeyExpr,\n    #          value=ValExpr,\n    #          generators=[\n    #              comprehension(\n    #                  target=TargetExpr\n    #                  iter=IterExpr,\n    #                  ifs=[Expr_1, Expr_2, ..., Expr_n]),\n    #                  is_async=0)],\n    #           ...\n    #         ])\n    return self.generic_visit(node)\n\n  def visit_GeneratorExp(self, node):\n    # GeneratorExp(elt=Expr,\n    #              generators=[\n    #                  comprehension(\n    #                      target=Expr,\n    #                      iter=Expr,\n    #                      ifs=[Expr_1, Expr_2, ..., Expr_n],\n    #                      is_async=0),\n    #                ...\n    #              ])\n    return self.generic_visit(node)\n\n  def visit_Await(self, node):\n    # Await(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_Yield(self, node):\n    # Yield(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_YieldFrom(self, node):\n    # YieldFrom(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_Compare(self, node):\n    # Compare(left=LExpr,\n    #         ops=[Op_1, Op_2, ..., Op_n],\n    #         comparators=[Expr_1, Expr_2, ..., Expr_n])\n    return self.generic_visit(node)\n\n  def visit_Call(self, node):\n    # Call(func=Expr,\n    #      args=[Expr_1, Expr_2, ..., Expr_n],\n    #      keywords=[\n    #          keyword(\n    #              arg='d',\n    #              value=Expr),\n    #            ...\n    #      ])\n    return self.generic_visit(node)\n\n  def visit_FormattedValue(self, node):\n    # FormattedValue(value=Expr,\n    #                conversion=-1,\n    #                format_spec=FSExpr)\n    return self.generic_visit(node)\n\n  def visit_JoinedStr(self, node):\n    # JoinedStr(values=[Expr_1, Expr_2, ..., Expr_n])\n    return self.generic_visit(node)\n\n  def visit_Constant(self, node):\n    # Constant(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_Attribute(self, node):\n    # Attribute(value=Expr,\n    #           attr=Identifier)\n    return self.generic_visit(node)\n\n  def visit_Subscript(self, node):\n    # Subscript(value=VExpr,\n    #           slice=SExpr)\n    return self.generic_visit(node)\n\n  def visit_Starred(self, node):\n    # Starred(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_Name(self, node):\n    # Name(id=Identifier)\n    return self.generic_visit(node)\n\n  def visit_List(self, node):\n    # List(elts=[Expr_1, Expr_2, ..., Expr_n])\n    return self.generic_visit(node)\n\n  def visit_Tuple(self, node):\n    # Tuple(elts=[Expr_1, Expr_2, ..., Expr_n])\n    return self.generic_visit(node)\n\n  def visit_Slice(self, node):\n    # Slice(lower=Expr,\n    #       upper=Expr,\n    #       step=Expr)\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Expression Context                                                       #\n  ############################################################################\n\n  def visit_Load(self, node):\n    # Load()\n    return self.generic_visit(node)\n\n  def visit_Store(self, node):\n    # Store()\n    return self.generic_visit(node)\n\n  def visit_Del(self, node):\n    # Del()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Boolean Operators                                                        #\n  ############################################################################\n\n  def visit_And(self, node):\n    # And()\n    return self.generic_visit(node)\n\n  def visit_Or(self, node):\n    # Or()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Binary Operators                                                         #\n  ############################################################################\n\n  def visit_Add(self, node):\n    # Add()\n    return self.generic_visit(node)\n\n  def visit_Sub(self, node):\n    # Sub()\n    return self.generic_visit(node)\n\n  def visit_Mult(self, node):\n    # Mult()\n    return self.generic_visit(node)\n\n  def visit_MatMult(self, node):\n    # MatMult()\n    return self.generic_visit(node)\n\n  def visit_Div(self, node):\n    # Div()\n    return self.generic_visit(node)\n\n  def visit_Mod(self, node):\n    # Mod()\n    return self.generic_visit(node)\n\n  def visit_Pow(self, node):\n    # Pow()\n    return self.generic_visit(node)\n\n  def visit_LShift(self, node):\n    # LShift()\n    return self.generic_visit(node)\n\n  def visit_RShift(self, node):\n    # RShift()\n    return self.generic_visit(node)\n\n  def visit_BitOr(self, node):\n    # BitOr()\n    return self.generic_visit(node)\n\n  def visit_BitXor(self, node):\n    # BitXor()\n    return self.generic_visit(node)\n\n  def visit_BitAnd(self, node):\n    # BitAnd()\n    return self.generic_visit(node)\n\n  def visit_FloorDiv(self, node):\n    # FloorDiv()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Unary Operators                                                          #\n  ############################################################################\n\n  def visit_Invert(self, node):\n    # Invert()\n    return self.generic_visit(node)\n\n  def visit_Not(self, node):\n    # Not()\n    return self.generic_visit(node)\n\n  def visit_UAdd(self, node):\n    # UAdd()\n    return self.generic_visit(node)\n\n  def visit_USub(self, node):\n    # USub()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Comparison Operators                                                     #\n  ############################################################################\n\n  def visit_Eq(self, node):\n    # Eq()\n    return self.generic_visit(node)\n\n  def visit_NotEq(self, node):\n    # NotEq()\n    return self.generic_visit(node)\n\n  def visit_Lt(self, node):\n    # Lt()\n    return self.generic_visit(node)\n\n  def visit_LtE(self, node):\n    # LtE()\n    return self.generic_visit(node)\n\n  def visit_Gt(self, node):\n    # Gt()\n    return self.generic_visit(node)\n\n  def visit_GtE(self, node):\n    # GtE()\n    return self.generic_visit(node)\n\n  def visit_Is(self, node):\n    # Is()\n    return self.generic_visit(node)\n\n  def visit_IsNot(self, node):\n    # IsNot()\n    return self.generic_visit(node)\n\n  def visit_In(self, node):\n    # In()\n    return self.generic_visit(node)\n\n  def visit_NotIn(self, node):\n    # NotIn()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Exception Handler                                                        #\n  ############################################################################\n\n  def visit_ExceptionHandler(self, node):\n    # ExceptHandler(type=Expr,\n    #               name=Identifier,\n    #               body=[...])\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Matching Patterns                                                        #\n  ############################################################################\n\n  def visit_MatchValue(self, node):\n    # MatchValue(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_MatchSingleton(self, node):\n    # MatchSingleton(value=Constant)\n    return self.generic_visit(node)\n\n  def visit_MatchSequence(self, node):\n    # MatchSequence(patterns=[pattern_1, pattern_2, ..., pattern_n])\n    return self.generic_visit(node)\n\n  def visit_MatchMapping(self, node):\n    # MatchMapping(keys=[Expr_1, Expr_2, ..., Expr_n],\n    #              patterns=[pattern_1, pattern_2, ..., pattern_m],\n    #              rest=Identifier)\n    return self.generic_visit(node)\n\n  def visit_MatchClass(self, node):\n    # MatchClass(cls=Expr,\n    #            patterns=[pattern_1, pattern_2, ...],\n    #            kwd_attrs=[Identifier_1, Identifier_2, ...],\n    #            kwd_patterns=[pattern_1, pattern_2, ...])\n    return self.generic_visit(node)\n\n  def visit_MatchStar(self, node):\n    # MatchStar(name=Identifier)\n    return self.generic_visit(node)\n\n  def visit_MatchAs(self, node):\n    # MatchAs(pattern=pattern,\n    #         name=Identifier)\n    return self.generic_visit(node)\n\n  def visit_MatchOr(self, node):\n    # MatchOr(patterns=[pattern_1, pattern_2, ...])\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Type Ignore                                                              #\n  ############################################################################\n\n  def visit_TypeIgnore(self, node):\n    # TypeIgnore(tag=string)\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Miscellaneous                                                            #\n  ############################################################################\n\n  def visit_comprehension(self, node):\n    # comprehension(target=Expr,\n    #               iter=Expr,\n    #               ifs=[Expr_1, Expr_2, ..., Expr_n],\n    #               is_async=0)\n    return self.generic_visit(node)\n\n  def visit_arguments(self, node):\n    # arguments(posonlyargs=[],\n    #           args=[],\n    #           vararg=arg,\n    #           kwonlyargs=[],\n    #           kw_defaults=[],\n    #           kwarg=arg,\n    #           defaults=[]),\n    return self.generic_visit(node)\n\n  def visit_arg(self, node):\n    # arg(arg=Identifier,\n    #     annotation=Expr,\n    #     type_comment='')\n    return self.generic_visit(node)\n\n  def visit_keyword(self, node):\n    # keyword(arg=Identifier,\n    #         value=Expr)\n    return self.generic_visit(node)\n\n  def visit_alias(self, node):\n    # alias(name=Identifier,\n    #       asname=Identifier)\n    return self.generic_visit(node)\n\n  def visit_withitem(self, node):\n    # withitem(context_expr=Expr,\n    #          optional_vars=Expr)\n    return self.generic_visit(node)\n\n  def visit_match_case(self, node):\n    # match_case(pattern=pattern,\n    #            guard=Expr,\n    #            body=[...])\n    return self.generic_visit(node)\n"
  },
  {
    "path": "yapf/pyparser/split_penalty_visitor.py",
    "content": "# Copyright 2022 Bill Wendling, All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport ast\n\nfrom yapf.pyparser import pyparser_utils as pyutils\nfrom yapf.yapflib import split_penalty\nfrom yapf.yapflib import style\nfrom yapf.yapflib import subtypes\n\n\nclass SplitPenalty(ast.NodeVisitor):\n  \"\"\"Compute split penalties between tokens.\"\"\"\n\n  def __init__(self, logical_lines):\n    super(SplitPenalty, self).__init__()\n    self.logical_lines = logical_lines\n\n    # We never want to split before a colon or comma.\n    for logical_line in logical_lines:\n      for token in logical_line.tokens:\n        if token.value in frozenset({',', ':'}):\n          token.split_penalty = split_penalty.UNBREAKABLE\n\n  def _GetTokens(self, node):\n    return pyutils.GetLogicalLine(self.logical_lines, node)\n\n  ############################################################################\n  # Statements                                                               #\n  ############################################################################\n\n  def visit_FunctionDef(self, node):\n    # FunctionDef(name=Name,\n    #             args=arguments(\n    #                 posonlyargs=[],\n    #                 args=[],\n    #                 vararg=[],\n    #                 kwonlyargs=[],\n    #                 kw_defaults=[],\n    #                 defaults=[]),\n    #             body=[...],\n    #             decorator_list=[Call_1, Call_2, ..., Call_n],\n    #             keywords=[])\n    tokens = self._GetTokens(node)\n\n    for decorator in node.decorator_list:\n      # The decorator token list begins after the '@'. The body of the decorator\n      # is formatted like a normal \"call.\"\n      decorator_range = self._GetTokens(decorator)\n      # Don't split after the '@'.\n      decorator_range[0].split_penalty = split_penalty.UNBREAKABLE\n\n    for token in tokens[1:]:\n      if token.value == '(':\n        break\n      _SetPenalty(token, split_penalty.UNBREAKABLE)\n\n    if node.returns:\n      start_index = pyutils.GetTokenIndex(tokens,\n                                          pyutils.TokenStart(node.returns))\n      _IncreasePenalty(tokens[start_index - 1:start_index + 1],\n                       split_penalty.VERY_STRONGLY_CONNECTED)\n      end_index = pyutils.GetTokenIndex(tokens, pyutils.TokenEnd(node.returns))\n      _IncreasePenalty(tokens[start_index + 1:end_index],\n                       split_penalty.STRONGLY_CONNECTED)\n\n    return self.generic_visit(node)\n\n  def visit_AsyncFunctionDef(self, node):\n    # AsyncFunctionDef(name=Name,\n    #                  args=arguments(\n    #                      posonlyargs=[],\n    #                      args=[],\n    #                      vararg=[],\n    #                      kwonlyargs=[],\n    #                      kw_defaults=[],\n    #                      defaults=[]),\n    #                  body=[...],\n    #                  decorator_list=[Expr_1, Expr_2, ..., Expr_n],\n    #                  keywords=[])\n    return self.visit_FunctionDef(node)\n\n  def visit_ClassDef(self, node):\n    # ClassDef(name=Name,\n    #          bases=[Expr_1, Expr_2, ..., Expr_n],\n    #          keywords=[],\n    #          body=[],\n    #          decorator_list=[Expr_1, Expr_2, ..., Expr_m])\n    for base in node.bases:\n      tokens = self._GetTokens(base)\n      _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n\n    for decorator in node.decorator_list:\n      # Don't split after the '@'.\n      tokens = self._GetTokens(decorator)\n      tokens[0].split_penalty = split_penalty.UNBREAKABLE\n\n    return self.generic_visit(node)\n\n  def visit_Return(self, node):\n    # Return(value=Expr)\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_Delete(self, node):\n    # Delete(targets=[Expr_1, Expr_2, ..., Expr_n])\n    for target in node.targets:\n      tokens = self._GetTokens(target)\n      _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_Assign(self, node):\n    # Assign(targets=[Expr_1, Expr_2, ..., Expr_n],\n    #        value=Expr)\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_AugAssign(self, node):\n    # AugAssign(target=Name,\n    #           op=Add(),\n    #           value=Expr)\n    return self.generic_visit(node)\n\n  def visit_AnnAssign(self, node):\n    # AnnAssign(target=Expr,\n    #           annotation=TypeName,\n    #           value=Expr,\n    #           simple=number)\n    return self.generic_visit(node)\n\n  def visit_For(self, node):\n    # For(target=Expr,\n    #     iter=Expr,\n    #     body=[...],\n    #     orelse=[...])\n    return self.generic_visit(node)\n\n  def visit_AsyncFor(self, node):\n    # AsyncFor(target=Expr,\n    #          iter=Expr,\n    #          body=[...],\n    #          orelse=[...])\n    return self.generic_visit(node)\n\n  def visit_While(self, node):\n    # While(test=Expr,\n    #       body=[...],\n    #       orelse=[...])\n    return self.generic_visit(node)\n\n  def visit_If(self, node):\n    # If(test=Expr,\n    #    body=[...],\n    #    orelse=[...])\n    return self.generic_visit(node)\n\n  def visit_With(self, node):\n    # With(items=[withitem_1, withitem_2, ..., withitem_n],\n    #      body=[...])\n    return self.generic_visit(node)\n\n  def visit_AsyncWith(self, node):\n    # AsyncWith(items=[withitem_1, withitem_2, ..., withitem_n],\n    #           body=[...])\n    return self.generic_visit(node)\n\n  def visit_Match(self, node):\n    # Match(subject=Expr,\n    #       cases=[\n    #           match_case(\n    #               pattern=pattern,\n    #               guard=Expr,\n    #               body=[...]),\n    #             ...\n    #       ])\n    return self.generic_visit(node)\n\n  def visit_Raise(self, node):\n    # Raise(exc=Expr)\n    return self.generic_visit(node)\n\n  def visit_Try(self, node):\n    # Try(body=[...],\n    #     handlers=[ExceptHandler_1, ExceptHandler_2, ..., ExceptHandler_b],\n    #     orelse=[...],\n    #     finalbody=[...])\n    return self.generic_visit(node)\n\n  def visit_Assert(self, node):\n    # Assert(test=Expr)\n    return self.generic_visit(node)\n\n  def visit_Import(self, node):\n    # Import(names=[\n    #            alias(\n    #                name=Identifier,\n    #                asname=Identifier),\n    #              ...\n    #        ])\n    return self.generic_visit(node)\n\n  def visit_ImportFrom(self, node):\n    # ImportFrom(module=Identifier,\n    #            names=[\n    #                alias(\n    #                    name=Identifier,\n    #                    asname=Identifier),\n    #                  ...\n    #            ],\n    #            level=num\n    return self.generic_visit(node)\n\n  def visit_Global(self, node):\n    # Global(names=[Identifier_1, Identifier_2, ..., Identifier_n])\n    return self.generic_visit(node)\n\n  def visit_Nonlocal(self, node):\n    # Nonlocal(names=[Identifier_1, Identifier_2, ..., Identifier_n])\n    return self.generic_visit(node)\n\n  def visit_Expr(self, node):\n    # Expr(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_Pass(self, node):\n    # Pass()\n    return self.generic_visit(node)\n\n  def visit_Break(self, node):\n    # Break()\n    return self.generic_visit(node)\n\n  def visit_Continue(self, node):\n    # Continue()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Expressions                                                              #\n  ############################################################################\n\n  def visit_BoolOp(self, node):\n    # BoolOp(op=And | Or,\n    #        values=[Expr_1, Expr_2, ..., Expr_n])\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n\n    # Lower the split penalty to allow splitting before or after the logical\n    # operator.\n    split_before_operator = style.Get('SPLIT_BEFORE_LOGICAL_OPERATOR')\n    operator_indices = [\n        pyutils.GetNextTokenIndex(tokens, pyutils.TokenEnd(value))\n        for value in node.values[:-1]\n    ]\n    for operator_index in operator_indices:\n      if not split_before_operator:\n        operator_index += 1\n      _DecreasePenalty(tokens[operator_index], split_penalty.EXPR * 2)\n\n    return self.generic_visit(node)\n\n  def visit_NamedExpr(self, node):\n    # NamedExpr(target=Name,\n    #           value=Expr)\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_BinOp(self, node):\n    # BinOp(left=LExpr\n    #       op=Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift |\n    #          RShift | BitOr | BitXor | BitAnd | FloorDiv\n    #       right=RExpr)\n    tokens = self._GetTokens(node)\n\n    _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n\n    # Lower the split penalty to allow splitting before or after the arithmetic\n    # operator.\n    operator_index = pyutils.GetNextTokenIndex(tokens,\n                                               pyutils.TokenEnd(node.left))\n    if not style.Get('SPLIT_BEFORE_ARITHMETIC_OPERATOR'):\n      operator_index += 1\n\n    _DecreasePenalty(tokens[operator_index], split_penalty.EXPR * 2)\n\n    return self.generic_visit(node)\n\n  def visit_UnaryOp(self, node):\n    # UnaryOp(op=Not | USub | UAdd | Invert,\n    #         operand=Expr)\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n    _IncreasePenalty(tokens[1], style.Get('SPLIT_PENALTY_AFTER_UNARY_OPERATOR'))\n\n    return self.generic_visit(node)\n\n  def visit_Lambda(self, node):\n    # Lambda(args=arguments(\n    #            posonlyargs=[arg(...), arg(...), ..., arg(...)],\n    #            args=[arg(...), arg(...), ..., arg(...)],\n    #            kwonlyargs=[arg(...), arg(...), ..., arg(...)],\n    #            kw_defaults=[arg(...), arg(...), ..., arg(...)],\n    #            defaults=[arg(...), arg(...), ..., arg(...)]),\n    #        body=Expr)\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.LAMBDA)\n\n    if style.Get('ALLOW_MULTILINE_LAMBDAS'):\n      _SetPenalty(self._GetTokens(node.body), split_penalty.MULTIPLINE_LAMBDA)\n\n    return self.generic_visit(node)\n\n  def visit_IfExp(self, node):\n    # IfExp(test=TestExpr,\n    #       body=BodyExpr,\n    #       orelse=OrElseExpr)\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_Dict(self, node):\n    # Dict(keys=[Expr_1, Expr_2, ..., Expr_n],\n    #      values=[Expr_1, Expr_2, ..., Expr_n])\n    tokens = self._GetTokens(node)\n\n    # The keys should be on a single line if at all possible.\n    for key in node.keys:\n      subrange = pyutils.GetTokensInSubRange(tokens, key)\n      _IncreasePenalty(subrange[1:], split_penalty.DICT_KEY_EXPR)\n\n    for value in node.values:\n      subrange = pyutils.GetTokensInSubRange(tokens, value)\n      _IncreasePenalty(subrange[1:], split_penalty.DICT_VALUE_EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_Set(self, node):\n    # Set(elts=[Expr_1, Expr_2, ..., Expr_n])\n    tokens = self._GetTokens(node)\n    for element in node.elts:\n      subrange = pyutils.GetTokensInSubRange(tokens, element)\n      _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_ListComp(self, node):\n    # ListComp(elt=Expr,\n    #          generators=[\n    #              comprehension(\n    #                  target=Expr,\n    #                  iter=Expr,\n    #                  ifs=[Expr_1, Expr_2, ..., Expr_n],\n    #                  is_async=0),\n    #               ...\n    #          ])\n    tokens = self._GetTokens(node)\n    element = pyutils.GetTokensInSubRange(tokens, node.elt)\n    _IncreasePenalty(element[1:], split_penalty.EXPR)\n\n    for comp in node.generators:\n      subrange = pyutils.GetTokensInSubRange(tokens, comp.iter)\n      _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n\n      for if_expr in comp.ifs:\n        subrange = pyutils.GetTokensInSubRange(tokens, if_expr)\n        _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_SetComp(self, node):\n    # SetComp(elt=Expr,\n    #         generators=[\n    #             comprehension(\n    #                 target=Expr,\n    #                 iter=Expr,\n    #                 ifs=[Expr_1, Expr_2, ..., Expr_n],\n    #                 is_async=0),\n    #           ...\n    #         ])\n    tokens = self._GetTokens(node)\n    element = pyutils.GetTokensInSubRange(tokens, node.elt)\n    _IncreasePenalty(element[1:], split_penalty.EXPR)\n\n    for comp in node.generators:\n      subrange = pyutils.GetTokensInSubRange(tokens, comp.iter)\n      _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n\n      for if_expr in comp.ifs:\n        subrange = pyutils.GetTokensInSubRange(tokens, if_expr)\n        _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_DictComp(self, node):\n    # DictComp(key=KeyExpr,\n    #          value=ValExpr,\n    #          generators=[\n    #              comprehension(\n    #                  target=TargetExpr\n    #                  iter=IterExpr,\n    #                  ifs=[Expr_1, Expr_2, ..., Expr_n]),\n    #                  is_async=0)],\n    #           ...\n    #         ])\n    tokens = self._GetTokens(node)\n    key = pyutils.GetTokensInSubRange(tokens, node.key)\n    _IncreasePenalty(key[1:], split_penalty.EXPR)\n\n    value = pyutils.GetTokensInSubRange(tokens, node.value)\n    _IncreasePenalty(value[1:], split_penalty.EXPR)\n\n    for comp in node.generators:\n      subrange = pyutils.GetTokensInSubRange(tokens, comp.iter)\n      _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n\n      for if_expr in comp.ifs:\n        subrange = pyutils.GetTokensInSubRange(tokens, if_expr)\n        _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_GeneratorExp(self, node):\n    # GeneratorExp(elt=Expr,\n    #              generators=[\n    #                  comprehension(\n    #                      target=Expr,\n    #                      iter=Expr,\n    #                      ifs=[Expr_1, Expr_2, ..., Expr_n],\n    #                      is_async=0),\n    #                ...\n    #              ])\n    tokens = self._GetTokens(node)\n    element = pyutils.GetTokensInSubRange(tokens, node.elt)\n    _IncreasePenalty(element[1:], split_penalty.EXPR)\n\n    for comp in node.generators:\n      subrange = pyutils.GetTokensInSubRange(tokens, comp.iter)\n      _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n\n      for if_expr in comp.ifs:\n        subrange = pyutils.GetTokensInSubRange(tokens, if_expr)\n        _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_Await(self, node):\n    # Await(value=Expr)\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_Yield(self, node):\n    # Yield(value=Expr)\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_YieldFrom(self, node):\n    # YieldFrom(value=Expr)\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n    tokens[2].split_penalty = split_penalty.UNBREAKABLE\n\n    return self.generic_visit(node)\n\n  def visit_Compare(self, node):\n    # Compare(left=LExpr,\n    #         ops=[Op_1, Op_2, ..., Op_n],\n    #         comparators=[Expr_1, Expr_2, ..., Expr_n])\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.EXPR)\n\n    operator_indices = [\n        pyutils.GetNextTokenIndex(tokens, pyutils.TokenEnd(node.left))\n    ] + [\n        pyutils.GetNextTokenIndex(tokens, pyutils.TokenEnd(comparator))\n        for comparator in node.comparators[:-1]\n    ]\n    split_before = style.Get('SPLIT_BEFORE_ARITHMETIC_OPERATOR')\n\n    for operator_index in operator_indices:\n      if not split_before:\n        operator_index += 1\n      _DecreasePenalty(tokens[operator_index], split_penalty.EXPR * 2)\n\n    return self.generic_visit(node)\n\n  def visit_Call(self, node):\n    # Call(func=Expr,\n    #      args=[Expr_1, Expr_2, ..., Expr_n],\n    #      keywords=[\n    #          keyword(\n    #              arg='d',\n    #              value=Expr),\n    #            ...\n    #      ])\n    tokens = self._GetTokens(node)\n\n    # Don't never split before the opening parenthesis.\n    paren_index = pyutils.GetNextTokenIndex(tokens, pyutils.TokenEnd(node.func))\n    _IncreasePenalty(tokens[paren_index], split_penalty.UNBREAKABLE)\n\n    for arg in node.args:\n      subrange = pyutils.GetTokensInSubRange(tokens, arg)\n      _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n\n    return self.generic_visit(node)\n\n  def visit_FormattedValue(self, node):\n    # FormattedValue(value=Expr,\n    #                conversion=-1)\n    return node  # Ignore formatted values.\n\n  def visit_JoinedStr(self, node):\n    # JoinedStr(values=[Expr_1, Expr_2, ..., Expr_n])\n    return self.generic_visit(node)\n\n  def visit_Constant(self, node):\n    # Constant(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_Attribute(self, node):\n    # Attribute(value=Expr,\n    #           attr=Identifier)\n    tokens = self._GetTokens(node)\n    split_before = style.Get('SPLIT_BEFORE_DOT')\n    dot_indices = pyutils.GetNextTokenIndex(tokens,\n                                            pyutils.TokenEnd(node.value))\n\n    if not split_before:\n      dot_indices += 1\n    _IncreasePenalty(tokens[dot_indices], split_penalty.VERY_STRONGLY_CONNECTED)\n\n    return self.generic_visit(node)\n\n  def visit_Subscript(self, node):\n    # Subscript(value=ValueExpr,\n    #           slice=SliceExpr)\n    tokens = self._GetTokens(node)\n\n    # Don't split before the opening bracket of a subscript.\n    bracket_index = pyutils.GetNextTokenIndex(tokens,\n                                              pyutils.TokenEnd(node.value))\n    _IncreasePenalty(tokens[bracket_index], split_penalty.UNBREAKABLE)\n\n    return self.generic_visit(node)\n\n  def visit_Starred(self, node):\n    # Starred(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_Name(self, node):\n    # Name(id=Identifier)\n    tokens = self._GetTokens(node)\n    _IncreasePenalty(tokens[1:], split_penalty.UNBREAKABLE)\n\n    return self.generic_visit(node)\n\n  def visit_List(self, node):\n    # List(elts=[Expr_1, Expr_2, ..., Expr_n])\n    tokens = self._GetTokens(node)\n\n    for element in node.elts:\n      subrange = pyutils.GetTokensInSubRange(tokens, element)\n      _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n      _DecreasePenalty(subrange[0], split_penalty.EXPR // 2)\n\n    return self.generic_visit(node)\n\n  def visit_Tuple(self, node):\n    # Tuple(elts=[Expr_1, Expr_2, ..., Expr_n])\n    tokens = self._GetTokens(node)\n\n    for element in node.elts:\n      subrange = pyutils.GetTokensInSubRange(tokens, element)\n      _IncreasePenalty(subrange[1:], split_penalty.EXPR)\n      _DecreasePenalty(subrange[0], split_penalty.EXPR // 2)\n\n    return self.generic_visit(node)\n\n  def visit_Slice(self, node):\n    # Slice(lower=Expr,\n    #       upper=Expr,\n    #       step=Expr)\n    tokens = self._GetTokens(node)\n\n    if hasattr(node, 'lower') and node.lower:\n      subrange = pyutils.GetTokensInSubRange(tokens, node.lower)\n      _IncreasePenalty(subrange, split_penalty.EXPR)\n      _DecreasePenalty(subrange[0], split_penalty.EXPR // 2)\n\n    if hasattr(node, 'upper') and node.upper:\n      colon_index = pyutils.GetPrevTokenIndex(tokens,\n                                              pyutils.TokenStart(node.upper))\n      _IncreasePenalty(tokens[colon_index], split_penalty.UNBREAKABLE)\n      subrange = pyutils.GetTokensInSubRange(tokens, node.upper)\n      _IncreasePenalty(subrange, split_penalty.EXPR)\n      _DecreasePenalty(subrange[0], split_penalty.EXPR // 2)\n\n    if hasattr(node, 'step') and node.step:\n      colon_index = pyutils.GetPrevTokenIndex(tokens,\n                                              pyutils.TokenStart(node.step))\n      _IncreasePenalty(tokens[colon_index], split_penalty.UNBREAKABLE)\n      subrange = pyutils.GetTokensInSubRange(tokens, node.step)\n      _IncreasePenalty(subrange, split_penalty.EXPR)\n      _DecreasePenalty(subrange[0], split_penalty.EXPR // 2)\n\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Expression Context                                                       #\n  ############################################################################\n\n  def visit_Load(self, node):\n    # Load()\n    return self.generic_visit(node)\n\n  def visit_Store(self, node):\n    # Store()\n    return self.generic_visit(node)\n\n  def visit_Del(self, node):\n    # Del()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Boolean Operators                                                        #\n  ############################################################################\n\n  def visit_And(self, node):\n    # And()\n    return self.generic_visit(node)\n\n  def visit_Or(self, node):\n    # Or()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Binary Operators                                                         #\n  ############################################################################\n\n  def visit_Add(self, node):\n    # Add()\n    return self.generic_visit(node)\n\n  def visit_Sub(self, node):\n    # Sub()\n    return self.generic_visit(node)\n\n  def visit_Mult(self, node):\n    # Mult()\n    return self.generic_visit(node)\n\n  def visit_MatMult(self, node):\n    # MatMult()\n    return self.generic_visit(node)\n\n  def visit_Div(self, node):\n    # Div()\n    return self.generic_visit(node)\n\n  def visit_Mod(self, node):\n    # Mod()\n    return self.generic_visit(node)\n\n  def visit_Pow(self, node):\n    # Pow()\n    return self.generic_visit(node)\n\n  def visit_LShift(self, node):\n    # LShift()\n    return self.generic_visit(node)\n\n  def visit_RShift(self, node):\n    # RShift()\n    return self.generic_visit(node)\n\n  def visit_BitOr(self, node):\n    # BitOr()\n    return self.generic_visit(node)\n\n  def visit_BitXor(self, node):\n    # BitXor()\n    return self.generic_visit(node)\n\n  def visit_BitAnd(self, node):\n    # BitAnd()\n    return self.generic_visit(node)\n\n  def visit_FloorDiv(self, node):\n    # FloorDiv()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Unary Operators                                                          #\n  ############################################################################\n\n  def visit_Invert(self, node):\n    # Invert()\n    return self.generic_visit(node)\n\n  def visit_Not(self, node):\n    # Not()\n    return self.generic_visit(node)\n\n  def visit_UAdd(self, node):\n    # UAdd()\n    return self.generic_visit(node)\n\n  def visit_USub(self, node):\n    # USub()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Comparison Operators                                                     #\n  ############################################################################\n\n  def visit_Eq(self, node):\n    # Eq()\n    return self.generic_visit(node)\n\n  def visit_NotEq(self, node):\n    # NotEq()\n    return self.generic_visit(node)\n\n  def visit_Lt(self, node):\n    # Lt()\n    return self.generic_visit(node)\n\n  def visit_LtE(self, node):\n    # LtE()\n    return self.generic_visit(node)\n\n  def visit_Gt(self, node):\n    # Gt()\n    return self.generic_visit(node)\n\n  def visit_GtE(self, node):\n    # GtE()\n    return self.generic_visit(node)\n\n  def visit_Is(self, node):\n    # Is()\n    return self.generic_visit(node)\n\n  def visit_IsNot(self, node):\n    # IsNot()\n    return self.generic_visit(node)\n\n  def visit_In(self, node):\n    # In()\n    return self.generic_visit(node)\n\n  def visit_NotIn(self, node):\n    # NotIn()\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Exception Handler                                                        #\n  ############################################################################\n\n  def visit_ExceptionHandler(self, node):\n    # ExceptHandler(type=Expr,\n    #               name=Identifier,\n    #               body=[...])\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Matching Patterns                                                        #\n  ############################################################################\n\n  def visit_MatchValue(self, node):\n    # MatchValue(value=Expr)\n    return self.generic_visit(node)\n\n  def visit_MatchSingleton(self, node):\n    # MatchSingleton(value=Constant)\n    return self.generic_visit(node)\n\n  def visit_MatchSequence(self, node):\n    # MatchSequence(patterns=[pattern_1, pattern_2, ..., pattern_n])\n    return self.generic_visit(node)\n\n  def visit_MatchMapping(self, node):\n    # MatchMapping(keys=[Expr_1, Expr_2, ..., Expr_n],\n    #              patterns=[pattern_1, pattern_2, ..., pattern_m],\n    #              rest=Identifier)\n    return self.generic_visit(node)\n\n  def visit_MatchClass(self, node):\n    # MatchClass(cls=Expr,\n    #            patterns=[pattern_1, pattern_2, ...],\n    #            kwd_attrs=[Identifier_1, Identifier_2, ...],\n    #            kwd_patterns=[pattern_1, pattern_2, ...])\n    return self.generic_visit(node)\n\n  def visit_MatchStar(self, node):\n    # MatchStar(name=Identifier)\n    return self.generic_visit(node)\n\n  def visit_MatchAs(self, node):\n    # MatchAs(pattern=pattern,\n    #         name=Identifier)\n    return self.generic_visit(node)\n\n  def visit_MatchOr(self, node):\n    # MatchOr(patterns=[pattern_1, pattern_2, ...])\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Type Ignore                                                              #\n  ############################################################################\n\n  def visit_TypeIgnore(self, node):\n    # TypeIgnore(tag=string)\n    return self.generic_visit(node)\n\n  ############################################################################\n  # Miscellaneous                                                            #\n  ############################################################################\n\n  def visit_comprehension(self, node):\n    # comprehension(target=Expr,\n    #               iter=Expr,\n    #               ifs=[Expr_1, Expr_2, ..., Expr_n],\n    #               is_async=0)\n    return self.generic_visit(node)\n\n  def visit_arguments(self, node):\n    # arguments(posonlyargs=[arg_1, arg_2, ..., arg_a],\n    #           args=[arg_1, arg_2, ..., arg_b],\n    #           vararg=arg,\n    #           kwonlyargs=[arg_1, arg_2, ..., arg_c],\n    #           kw_defaults=[arg_1, arg_2, ..., arg_d],\n    #           kwarg=arg,\n    #           defaults=[Expr_1, Expr_2, ..., Expr_n])\n    return self.generic_visit(node)\n\n  def visit_arg(self, node):\n    # arg(arg=Identifier,\n    #     annotation=Expr,\n    #     type_comment='')\n    tokens = self._GetTokens(node)\n\n    # Process any annotations.\n    if hasattr(node, 'annotation') and node.annotation:\n      annotation = node.annotation\n      subrange = pyutils.GetTokensInSubRange(tokens, annotation)\n      _IncreasePenalty(subrange, split_penalty.ANNOTATION)\n\n    return self.generic_visit(node)\n\n  def visit_keyword(self, node):\n    # keyword(arg=Identifier,\n    #         value=Expr)\n    return self.generic_visit(node)\n\n  def visit_alias(self, node):\n    # alias(name=Identifier,\n    #       asname=Identifier)\n    return self.generic_visit(node)\n\n  def visit_withitem(self, node):\n    # withitem(context_expr=Expr,\n    #          optional_vars=Expr)\n    return self.generic_visit(node)\n\n  def visit_match_case(self, node):\n    # match_case(pattern=pattern,\n    #            guard=Expr,\n    #            body=[...])\n    return self.generic_visit(node)\n\n\ndef _IncreasePenalty(tokens, amt):\n  if not isinstance(tokens, list):\n    tokens = [tokens]\n  for token in tokens:\n    token.split_penalty += amt\n\n\ndef _DecreasePenalty(tokens, amt):\n  if not isinstance(tokens, list):\n    tokens = [tokens]\n  for token in tokens:\n    token.split_penalty -= amt\n\n\ndef _SetPenalty(tokens, amt):\n  if not isinstance(tokens, list):\n    tokens = [tokens]\n  for token in tokens:\n    token.split_penalty = amt\n"
  },
  {
    "path": "yapf/pytree/__init__.py",
    "content": "# Copyright 2021 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n"
  },
  {
    "path": "yapf/pytree/blank_line_calculator.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Calculate the number of blank lines between top-level entities.\n\nCalculates how many blank lines we need between classes, functions, and other\nentities at the same level.\n\n  CalculateBlankLines(): the main function exported by this module.\n\nAnnotations:\n  newlines: The number of newlines required before the node.\n\"\"\"\n\nfrom yapf_third_party._ylib2to3.pgen2 import token as grammar_token\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.pytree import pytree_visitor\nfrom yapf.yapflib import style\n\n_NO_BLANK_LINES = 1\n_ONE_BLANK_LINE = 2\n_TWO_BLANK_LINES = 3\n\n_PYTHON_STATEMENTS = frozenset({\n    'small_stmt', 'expr_stmt', 'print_stmt', 'del_stmt', 'pass_stmt',\n    'break_stmt', 'continue_stmt', 'return_stmt', 'raise_stmt', 'yield_stmt',\n    'import_stmt', 'global_stmt', 'exec_stmt', 'assert_stmt', 'if_stmt',\n    'while_stmt', 'for_stmt', 'try_stmt', 'with_stmt', 'nonlocal_stmt',\n    'async_stmt', 'simple_stmt'\n})\n\n\ndef CalculateBlankLines(tree):\n  \"\"\"Run the blank line calculator visitor over the tree.\n\n  This modifies the tree in place.\n\n  Arguments:\n    tree: the top-level pytree node to annotate with subtypes.\n  \"\"\"\n  blank_line_calculator = _BlankLineCalculator()\n  blank_line_calculator.Visit(tree)\n\n\nclass _BlankLineCalculator(pytree_visitor.PyTreeVisitor):\n  \"\"\"_BlankLineCalculator - see file-level docstring for a description.\"\"\"\n\n  def __init__(self):\n    self.class_level = 0\n    self.function_level = 0\n    self.last_comment_lineno = 0\n    self.last_was_decorator = False\n    self.last_was_class_or_function = False\n\n  def Visit_simple_stmt(self, node):  # pylint: disable=invalid-name\n    self.DefaultNodeVisit(node)\n    if node.children[0].type == grammar_token.COMMENT:\n      self.last_comment_lineno = node.children[0].lineno\n\n  def Visit_decorator(self, node):  # pylint: disable=invalid-name\n    if (self.last_comment_lineno and\n        self.last_comment_lineno == node.children[0].lineno - 1):\n      _SetNumNewlines(node.children[0], _NO_BLANK_LINES)\n    else:\n      _SetNumNewlines(node.children[0], self._GetNumNewlines(node))\n    for child in node.children:\n      self.Visit(child)\n    self.last_was_decorator = True\n\n  def Visit_classdef(self, node):  # pylint: disable=invalid-name\n    self.last_was_class_or_function = False\n    index = self._SetBlankLinesBetweenCommentAndClassFunc(node)\n    self.last_was_decorator = False\n    self.class_level += 1\n    for child in node.children[index:]:\n      self.Visit(child)\n    self.class_level -= 1\n    self.last_was_class_or_function = True\n\n  def Visit_funcdef(self, node):  # pylint: disable=invalid-name\n    self.last_was_class_or_function = False\n    index = self._SetBlankLinesBetweenCommentAndClassFunc(node)\n    if _AsyncFunction(node):\n      index = self._SetBlankLinesBetweenCommentAndClassFunc(\n          node.prev_sibling.parent)\n      _SetNumNewlines(node.children[0], None)\n    else:\n      index = self._SetBlankLinesBetweenCommentAndClassFunc(node)\n    self.last_was_decorator = False\n    self.function_level += 1\n    for child in node.children[index:]:\n      self.Visit(child)\n    self.function_level -= 1\n    self.last_was_class_or_function = True\n\n  def DefaultNodeVisit(self, node):\n    \"\"\"Override the default visitor for Node.\n\n    This will set the blank lines required if the last entity was a class or\n    function.\n\n    Arguments:\n      node: (pytree.Node) The node to visit.\n    \"\"\"\n    if self.last_was_class_or_function:\n      if pytree_utils.NodeName(node) in _PYTHON_STATEMENTS:\n        leaf = pytree_utils.FirstLeafNode(node)\n        _SetNumNewlines(leaf, self._GetNumNewlines(leaf))\n    self.last_was_class_or_function = False\n    super(_BlankLineCalculator, self).DefaultNodeVisit(node)\n\n  def _SetBlankLinesBetweenCommentAndClassFunc(self, node):\n    \"\"\"Set the number of blanks between a comment and class or func definition.\n\n    Class and function definitions have leading comments as children of the\n    classdef and functdef nodes.\n\n    Arguments:\n      node: (pytree.Node) The classdef or funcdef node.\n\n    Returns:\n      The index of the first child past the comment nodes.\n    \"\"\"\n    index = 0\n    while pytree_utils.IsCommentStatement(node.children[index]):\n      # Standalone comments are wrapped in a simple_stmt node with the comment\n      # node as its only child.\n      self.Visit(node.children[index].children[0])\n      if not self.last_was_decorator:\n        _SetNumNewlines(node.children[index].children[0], _ONE_BLANK_LINE)\n      index += 1\n    if (index and node.children[index].lineno - 1\n        == node.children[index - 1].children[0].lineno):\n      _SetNumNewlines(node.children[index], _NO_BLANK_LINES)\n    else:\n      if self.last_comment_lineno + 1 == node.children[index].lineno:\n        num_newlines = _NO_BLANK_LINES\n      else:\n        num_newlines = self._GetNumNewlines(node)\n      _SetNumNewlines(node.children[index], num_newlines)\n    return index\n\n  def _GetNumNewlines(self, node):\n    if self.last_was_decorator:\n      return _NO_BLANK_LINES\n    elif self._IsTopLevel(node):\n      return 1 + style.Get('BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION')\n    return _ONE_BLANK_LINE\n\n  def _IsTopLevel(self, node):\n    return (not (self.class_level or self.function_level) and\n            _StartsInZerothColumn(node))\n\n\ndef _SetNumNewlines(node, num_newlines):\n  pytree_utils.SetNodeAnnotation(node, pytree_utils.Annotation.NEWLINES,\n                                 num_newlines)\n\n\ndef _StartsInZerothColumn(node):\n  return (pytree_utils.FirstLeafNode(node).column == 0 or\n          (_AsyncFunction(node) and node.prev_sibling.column == 0))\n\n\ndef _AsyncFunction(node):\n  return (node.prev_sibling and node.prev_sibling.type == grammar_token.ASYNC)\n"
  },
  {
    "path": "yapf/pytree/comment_splicer.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Comment splicer for lib2to3 trees.\n\nThe lib2to3 syntax tree produced by the parser holds comments and whitespace in\nprefix attributes of nodes, rather than nodes themselves. This module provides\nfunctionality to splice comments out of prefixes and into nodes of their own,\nmaking them easier to process.\n\n  SpliceComments(): the main function exported by this module.\n\"\"\"\n\nfrom yapf_third_party._ylib2to3 import pygram\nfrom yapf_third_party._ylib2to3 import pytree\nfrom yapf_third_party._ylib2to3.pgen2 import token\n\nfrom yapf.pytree import pytree_utils\n\n\ndef SpliceComments(tree):\n  \"\"\"Given a pytree, splice comments into nodes of their own right.\n\n  Extract comments from the prefixes where they are housed after parsing.\n  The prefixes that previously housed the comments become empty.\n\n  Args:\n    tree: a pytree.Node - the tree to work on. The tree is modified by this\n        function.\n  \"\"\"\n  # The previous leaf node encountered in the traversal.\n  # This is a list because Python 2.x doesn't have 'nonlocal' :)\n  prev_leaf = [None]\n  _AnnotateIndents(tree)\n\n  def _VisitNodeRec(node):\n    \"\"\"Recursively visit each node to splice comments into the AST.\"\"\"\n    # This loop may insert into node.children, so we'll iterate over a copy.\n    for child in node.children[:]:\n      if isinstance(child, pytree.Node):\n        # Nodes don't have prefixes.\n        _VisitNodeRec(child)\n      else:\n        if child.prefix.lstrip().startswith('#'):\n          # We have a comment prefix in this child, so splicing is needed.\n          comment_prefix = child.prefix\n          comment_lineno = child.lineno - comment_prefix.count('\\n')\n          comment_column = child.column\n\n          # Remember the leading indentation of this prefix and clear it.\n          # Mopping up the prefix is important because we may go over this same\n          # child in the next iteration...\n          child_prefix = child.prefix.lstrip('\\n')\n          prefix_indent = child_prefix[:child_prefix.find('#')]\n          if '\\n' in prefix_indent:\n            prefix_indent = prefix_indent[prefix_indent.rfind('\\n') + 1:]\n          child.prefix = ''\n\n          if child.type == token.NEWLINE:\n            # If the prefix was on a NEWLINE leaf, it's part of the line so it\n            # will be inserted after the previously encountered leaf.\n            # We can't just insert it before the NEWLINE node, because as a\n            # result of the way pytrees are organized, this node can be under\n            # an inappropriate parent.\n            comment_column -= len(comment_prefix.lstrip())\n            pytree_utils.InsertNodesAfter(\n                _CreateCommentsFromPrefix(\n                    comment_prefix,\n                    comment_lineno,\n                    comment_column,\n                    standalone=False), prev_leaf[0])\n          elif child.type == token.DEDENT:\n            # Comment prefixes on DEDENT nodes also deserve special treatment,\n            # because their final placement depends on their prefix.\n            # We'll look for an ancestor of this child with a matching\n            # indentation, and insert the comment before it if the ancestor is\n            # on a DEDENT node and after it otherwise.\n            #\n            # lib2to3 places comments that should be separated into the same\n            # DEDENT node. For example, \"comment 1\" and \"comment 2\" will be\n            # combined.\n            #\n            #   def _():\n            #     for x in y:\n            #       pass\n            #       # comment 1\n            #\n            #     # comment 2\n            #     pass\n            #\n            # In this case, we need to split them up ourselves.\n\n            # Split into groups of comments at decreasing levels of indentation\n            comment_groups = []\n            comment_column = None\n            for cmt in comment_prefix.split('\\n'):\n              col = cmt.find('#')\n              if col < 0:\n                if comment_column is None:\n                  # Skip empty lines at the top of the first comment group\n                  comment_lineno += 1\n                  continue\n              elif comment_column is None or col < comment_column:\n                comment_column = col\n                comment_indent = cmt[:comment_column]\n                comment_groups.append((comment_column, comment_indent, []))\n              comment_groups[-1][-1].append(cmt)\n\n            # Insert a node for each group\n            for comment_column, comment_indent, comment_group in comment_groups:\n              ancestor_at_indent = _FindAncestorAtIndent(child, comment_indent)\n              if ancestor_at_indent.type == token.DEDENT:\n                InsertNodes = pytree_utils.InsertNodesBefore  # pylint: disable=invalid-name # noqa\n              else:\n                InsertNodes = pytree_utils.InsertNodesAfter  # pylint: disable=invalid-name # noqa\n              InsertNodes(\n                  _CreateCommentsFromPrefix(\n                      '\\n'.join(comment_group) + '\\n',\n                      comment_lineno,\n                      comment_column,\n                      standalone=True), ancestor_at_indent)\n              comment_lineno += len(comment_group)\n          else:\n            # Otherwise there are two cases.\n            #\n            # 1. The comment is on its own line\n            # 2. The comment is part of an expression.\n            #\n            # Unfortunately, it's fairly difficult to distinguish between the\n            # two in lib2to3 trees. The algorithm here is to determine whether\n            # child is the first leaf in the statement it belongs to. If it is,\n            # then the comment (which is a prefix) belongs on a separate line.\n            # If it is not, it means the comment is buried deep in the statement\n            # and is part of some expression.\n            stmt_parent = _FindStmtParent(child)\n\n            for leaf_in_parent in stmt_parent.leaves():\n              if leaf_in_parent.type == token.NEWLINE:\n                continue\n              elif id(leaf_in_parent) == id(child):\n                # This comment stands on its own line, and it has to be inserted\n                # into the appropriate parent. We'll have to find a suitable\n                # parent to insert into. See comments above\n                # _STANDALONE_LINE_NODES for more details.\n                node_with_line_parent = _FindNodeWithStandaloneLineParent(child)\n\n                if pytree_utils.NodeName(\n                    node_with_line_parent.parent) in {'funcdef', 'classdef'}:\n                  # Keep a comment that's not attached to a function or class\n                  # next to the object it is attached to.\n                  comment_end = (\n                      comment_lineno + comment_prefix.rstrip('\\n').count('\\n'))\n                  if comment_end < node_with_line_parent.lineno - 1:\n                    node_with_line_parent = node_with_line_parent.parent\n\n                pytree_utils.InsertNodesBefore(\n                    _CreateCommentsFromPrefix(\n                        comment_prefix, comment_lineno, 0, standalone=True),\n                    node_with_line_parent)\n                break\n              else:\n                if comment_lineno == prev_leaf[0].lineno:\n                  comment_lines = comment_prefix.splitlines()\n                  value = comment_lines[0].lstrip()\n                  if value.rstrip('\\n'):\n                    comment_column = prev_leaf[0].column\n                    comment_column += len(prev_leaf[0].value)\n                    comment_column += (\n                        len(comment_lines[0]) - len(comment_lines[0].lstrip()))\n                    comment_leaf = pytree.Leaf(\n                        type=token.COMMENT,\n                        value=value.rstrip('\\n'),\n                        context=('', (comment_lineno, comment_column)))\n                    pytree_utils.InsertNodesAfter([comment_leaf], prev_leaf[0])\n                    comment_prefix = '\\n'.join(comment_lines[1:])\n                    comment_lineno += 1\n\n                rindex = (0 if '\\n' not in comment_prefix.rstrip() else\n                          comment_prefix.rstrip().rindex('\\n') + 1)\n                comment_column = (\n                    len(comment_prefix[rindex:]) -\n                    len(comment_prefix[rindex:].lstrip()))\n                comments = _CreateCommentsFromPrefix(\n                    comment_prefix,\n                    comment_lineno,\n                    comment_column,\n                    standalone=False)\n                pytree_utils.InsertNodesBefore(comments, child)\n                break\n\n        prev_leaf[0] = child\n\n  _VisitNodeRec(tree)\n\n\ndef _CreateCommentsFromPrefix(comment_prefix,\n                              comment_lineno,\n                              comment_column,\n                              standalone=False):\n  \"\"\"Create pytree nodes to represent the given comment prefix.\n\n  Args:\n    comment_prefix: (unicode) the text of the comment from the node's prefix.\n    comment_lineno: (int) the line number for the start of the comment.\n    comment_column: (int) the column for the start of the comment.\n    standalone: (bool) determines if the comment is standalone or not.\n\n  Returns:\n    The simple_stmt nodes if this is a standalone comment, otherwise a list of\n    new COMMENT leafs. The prefix may consist of multiple comment blocks,\n    separated by blank lines. Each block gets its own leaf.\n  \"\"\"\n  # The comment is stored in the prefix attribute, with no lineno of its\n  # own. So we only know at which line it ends. To find out at which line it\n  # starts, look at how many newlines the comment itself contains.\n  comments = []\n\n  lines = comment_prefix.split('\\n')\n  index = 0\n  while index < len(lines):\n    comment_block = []\n    while index < len(lines) and lines[index].lstrip().startswith('#'):\n      comment_block.append(lines[index].strip())\n      index += 1\n\n    if comment_block:\n      new_lineno = comment_lineno + index - 1\n      comment_block[0] = comment_block[0].strip()\n      comment_block[-1] = comment_block[-1].strip()\n      comment_leaf = pytree.Leaf(\n          type=token.COMMENT,\n          value='\\n'.join(comment_block),\n          context=('', (new_lineno, comment_column)))\n      comment_node = comment_leaf if not standalone else pytree.Node(\n          pygram.python_symbols.simple_stmt, [comment_leaf])\n      comments.append(comment_node)\n\n    while index < len(lines) and not lines[index].lstrip():\n      index += 1\n\n  return comments\n\n\n# \"Standalone line nodes\" are tree nodes that have to start a new line in Python\n# code (and cannot follow a ';' or ':'). Other nodes, like 'expr_stmt', serve as\n# parents of other nodes but can come later in a line. This is a list of\n# standalone line nodes in the grammar. It is meant to be exhaustive\n# *eventually*, and we'll modify it with time as we discover more corner cases\n# in the parse tree.\n#\n# When splicing a standalone comment (i.e. a comment that appears on its own\n# line, not on the same line with other code), it's important to insert it into\n# an appropriate parent of the node it's attached to. An appropriate parent\n# is the first \"standalone line node\" in the parent chain of a node.\n_STANDALONE_LINE_NODES = frozenset([\n    'suite', 'if_stmt', 'while_stmt', 'for_stmt', 'try_stmt', 'with_stmt',\n    'funcdef', 'classdef', 'decorated', 'file_input'\n])\n\n\ndef _FindNodeWithStandaloneLineParent(node):\n  \"\"\"Find a node whose parent is a 'standalone line' node.\n\n  See the comment above _STANDALONE_LINE_NODES for more details.\n\n  Arguments:\n    node: node to start from\n\n  Returns:\n    Suitable node that's either the node itself or one of its ancestors.\n  \"\"\"\n  if pytree_utils.NodeName(node.parent) in _STANDALONE_LINE_NODES:\n    return node\n  else:\n    # This is guaranteed to terminate because 'file_input' is the root node of\n    # any pytree.\n    return _FindNodeWithStandaloneLineParent(node.parent)\n\n\n# \"Statement nodes\" are standalone statements. The don't have to start a new\n# line.\n_STATEMENT_NODES = frozenset(['simple_stmt']) | _STANDALONE_LINE_NODES\n\n\ndef _FindStmtParent(node):\n  \"\"\"Find the nearest parent of node that is a statement node.\n\n  Arguments:\n    node: node to start from\n\n  Returns:\n    Nearest parent (or node itself, if suitable).\n  \"\"\"\n  if pytree_utils.NodeName(node) in _STATEMENT_NODES:\n    return node\n  else:\n    return _FindStmtParent(node.parent)\n\n\ndef _FindAncestorAtIndent(node, indent):\n  \"\"\"Find an ancestor of node with the given indentation.\n\n  Arguments:\n    node: node to start from. This must not be the tree root.\n    indent: indentation string for the ancestor we're looking for.\n        See _AnnotateIndents for more details.\n\n  Returns:\n    An ancestor node with suitable indentation. If no suitable ancestor is\n    found, the closest ancestor to the tree root is returned.\n  \"\"\"\n  if node.parent.parent is None:\n    # Our parent is the tree root, so there's nowhere else to go.\n    return node\n\n  # If the parent has an indent annotation, and it's shorter than node's\n  # indent, this is a suitable ancestor.\n  # The reason for \"shorter\" rather than \"equal\" is that comments may be\n  # improperly indented (i.e. by three spaces, where surrounding statements\n  # have either zero or two or four), and we don't want to propagate them all\n  # the way to the root.\n  parent_indent = pytree_utils.GetNodeAnnotation(\n      node.parent, pytree_utils.Annotation.CHILD_INDENT)\n  if parent_indent is not None and indent.startswith(parent_indent):\n    return node\n  else:\n    # Keep looking up the tree.\n    return _FindAncestorAtIndent(node.parent, indent)\n\n\ndef _AnnotateIndents(tree):\n  \"\"\"Annotate the tree with child_indent annotations.\n\n  A child_indent annotation on a node specifies the indentation (as a string,\n  like \"  \") of its children. It is inferred from the INDENT child of a node.\n\n  Arguments:\n    tree: root of a pytree. The pytree is modified to add annotations to nodes.\n\n  Raises:\n    RuntimeError: if the tree is malformed.\n  \"\"\"\n  # Annotate the root of the tree with zero indent.\n  if tree.parent is None:\n    pytree_utils.SetNodeAnnotation(tree, pytree_utils.Annotation.CHILD_INDENT,\n                                   '')\n  for child in tree.children:\n    if child.type == token.INDENT:\n      child_indent = pytree_utils.GetNodeAnnotation(\n          tree, pytree_utils.Annotation.CHILD_INDENT)\n      if child_indent is not None and child_indent != child.value:\n        raise RuntimeError('inconsistent indentation for child', (tree, child))\n      pytree_utils.SetNodeAnnotation(tree, pytree_utils.Annotation.CHILD_INDENT,\n                                     child.value)\n    _AnnotateIndents(child)\n"
  },
  {
    "path": "yapf/pytree/continuation_splicer.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Insert \"continuation\" nodes into lib2to3 tree.\n\nThe \"backslash-newline\" continuation marker is shoved into the node's prefix.\nPull them out and make it into nodes of their own.\n\n  SpliceContinuations(): the main function exported by this module.\n\"\"\"\n\nfrom yapf_third_party._ylib2to3 import pytree\n\nfrom yapf.yapflib import format_token\n\n\ndef SpliceContinuations(tree):\n  \"\"\"Given a pytree, splice the continuation marker into nodes.\n\n  Arguments:\n    tree: (pytree.Node) The tree to work on. The tree is modified by this\n      function.\n  \"\"\"\n\n  def RecSplicer(node):\n    \"\"\"Inserts a continuation marker into the node.\"\"\"\n    if isinstance(node, pytree.Leaf):\n      if node.prefix.lstrip().startswith('\\\\\\n'):\n        new_lineno = node.lineno - node.prefix.count('\\n')\n        return pytree.Leaf(\n            type=format_token.CONTINUATION,\n            value=node.prefix,\n            context=('', (new_lineno, 0)))\n      return None\n    num_inserted = 0\n    for index, child in enumerate(node.children[:]):\n      continuation_node = RecSplicer(child)\n      if continuation_node:\n        node.children.insert(index + num_inserted, continuation_node)\n        num_inserted += 1\n\n  RecSplicer(tree)\n"
  },
  {
    "path": "yapf/pytree/pytree_unwrapper.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"PyTreeUnwrapper - produces a list of logical lines from a pytree.\n\n[for a description of what a logical line is, see logical_line.py]\n\nThis is a pytree visitor that goes over a parse tree and produces a list of\nLogicalLine containers from it, each with its own depth and containing all the\ntokens that could fit on the line if there were no maximal line-length\nlimitations.\n\nNote: a precondition to running this visitor and obtaining correct results is\nfor the tree to have its comments spliced in as nodes. Prefixes are ignored.\n\nFor most uses, the convenience function UnwrapPyTree should be sufficient.\n\"\"\"\n\n# The word \"token\" is overloaded within this module, so for clarity rename\n# the imported pgen2.token module.\nfrom yapf_third_party._ylib2to3 import pytree\nfrom yapf_third_party._ylib2to3.pgen2 import token as grammar_token\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.pytree import pytree_visitor\nfrom yapf.pytree import split_penalty\nfrom yapf.yapflib import format_token\nfrom yapf.yapflib import logical_line\nfrom yapf.yapflib import object_state\nfrom yapf.yapflib import style\nfrom yapf.yapflib import subtypes\n\n_OPENING_BRACKETS = frozenset({'(', '[', '{'})\n_CLOSING_BRACKETS = frozenset({')', ']', '}'})\n\n\ndef UnwrapPyTree(tree):\n  \"\"\"Create and return a list of logical lines from the given pytree.\n\n  Arguments:\n    tree: the top-level pytree node to unwrap..\n\n  Returns:\n    A list of LogicalLine objects.\n  \"\"\"\n  unwrapper = PyTreeUnwrapper()\n  unwrapper.Visit(tree)\n  llines = unwrapper.GetLogicalLines()\n  llines.sort(key=lambda x: x.lineno)\n  return llines\n\n\n# Grammar tokens considered as whitespace for the purpose of unwrapping.\n_WHITESPACE_TOKENS = frozenset([\n    grammar_token.NEWLINE, grammar_token.DEDENT, grammar_token.INDENT,\n    grammar_token.ENDMARKER\n])\n\n\nclass PyTreeUnwrapper(pytree_visitor.PyTreeVisitor):\n  \"\"\"PyTreeUnwrapper - see file-level docstring for detailed description.\n\n  Note: since this implements PyTreeVisitor and node names in lib2to3 are\n  underscore_separated, the visiting methods of this class are named as\n  Visit_node_name. invalid-name pragmas are added to each such method to silence\n  a style warning. This is forced on us by the usage of lib2to3, and re-munging\n  method names to make them different from actual node names sounded like a\n  confusing and brittle affair that wasn't worth it for this small & controlled\n  deviation from the style guide.\n\n  To understand the connection between visitor methods in this class, some\n  familiarity with the Python grammar is required.\n  \"\"\"\n\n  def __init__(self):\n    # A list of all logical lines finished visiting so far.\n    self._logical_lines = []\n\n    # Builds up a \"current\" logical line while visiting pytree nodes. Some nodes\n    # will finish a line and start a new one.\n    self._cur_logical_line = logical_line.LogicalLine(0)\n\n    # Current indentation depth.\n    self._cur_depth = 0\n\n  def GetLogicalLines(self):\n    \"\"\"Fetch the result of the tree walk.\n\n    Note: only call this after visiting the whole tree.\n\n    Returns:\n      A list of LogicalLine objects.\n    \"\"\"\n    # Make sure the last line that was being populated is flushed.\n    self._StartNewLine()\n    return self._logical_lines\n\n  def _StartNewLine(self):\n    \"\"\"Finish current line and start a new one.\n\n    Place the currently accumulated line into the _logical_lines list and\n    start a new one.\n    \"\"\"\n    if self._cur_logical_line.tokens:\n      self._logical_lines.append(self._cur_logical_line)\n      _MatchBrackets(self._cur_logical_line)\n      _IdentifyParameterLists(self._cur_logical_line)\n      _AdjustSplitPenalty(self._cur_logical_line)\n    self._cur_logical_line = logical_line.LogicalLine(self._cur_depth)\n\n  _STMT_TYPES = frozenset({\n      'if_stmt',\n      'while_stmt',\n      'for_stmt',\n      'try_stmt',\n      'expect_clause',\n      'with_stmt',\n      'match_stmt',\n      'case_block',\n      'funcdef',\n      'classdef',\n  })\n\n  # pylint: disable=invalid-name,missing-docstring\n  def Visit_simple_stmt(self, node):\n    # A 'simple_stmt' conveniently represents a non-compound Python statement,\n    # i.e. a statement that does not contain other statements.\n\n    # When compound nodes have a single statement as their suite, the parser\n    # can leave it in the tree directly without creating a suite. But we have\n    # to increase depth in these cases as well. However, don't increase the\n    # depth of we have a simple_stmt that's a comment node. This represents a\n    # standalone comment and in the case of it coming directly after the\n    # funcdef, it is a \"top\" comment for the whole function.\n    # TODO(eliben): add more relevant compound statements here.\n    single_stmt_suite = (\n        node.parent and pytree_utils.NodeName(node.parent) in self._STMT_TYPES)\n    is_comment_stmt = pytree_utils.IsCommentStatement(node)\n    is_inside_match = node.parent and pytree_utils.NodeName(\n        node.parent) == 'match_stmt'\n    if (single_stmt_suite and not is_comment_stmt) or is_inside_match:\n      self._cur_depth += 1\n    self._StartNewLine()\n    self.DefaultNodeVisit(node)\n    if (single_stmt_suite and not is_comment_stmt) or is_inside_match:\n      self._cur_depth -= 1\n\n  def _VisitCompoundStatement(self, node, substatement_names):\n    \"\"\"Helper for visiting compound statements.\n\n    Python compound statements serve as containers for other statements. Thus,\n    when we encounter a new compound statement, we start a new logical line.\n\n    Arguments:\n      node: the node to visit.\n      substatement_names: set of node names. A compound statement will be\n        recognized as a NAME node with a name in this set.\n    \"\"\"\n    for child in node.children:\n      # A pytree is structured in such a way that a single 'if_stmt' node will\n      # contain all the 'if', 'elif' and 'else' nodes as children (similar\n      # structure applies to 'while' statements, 'try' blocks, etc). Therefore,\n      # we visit all children here and create a new line before the requested\n      # set of nodes.\n      if (child.type == grammar_token.NAME and\n          child.value in substatement_names):\n        self._StartNewLine()\n      self.Visit(child)\n\n  _IF_STMT_ELEMS = frozenset({'if', 'else', 'elif'})\n\n  def Visit_if_stmt(self, node):  # pylint: disable=invalid-name\n    self._VisitCompoundStatement(node, self._IF_STMT_ELEMS)\n\n  _WHILE_STMT_ELEMS = frozenset({'while', 'else'})\n\n  def Visit_while_stmt(self, node):  # pylint: disable=invalid-name\n    self._VisitCompoundStatement(node, self._WHILE_STMT_ELEMS)\n\n  _FOR_STMT_ELEMS = frozenset({'for', 'else'})\n\n  def Visit_for_stmt(self, node):  # pylint: disable=invalid-name\n    self._VisitCompoundStatement(node, self._FOR_STMT_ELEMS)\n\n  _TRY_STMT_ELEMS = frozenset({'try', 'except', 'else', 'finally'})\n\n  def Visit_try_stmt(self, node):  # pylint: disable=invalid-name\n    self._VisitCompoundStatement(node, self._TRY_STMT_ELEMS)\n\n  _EXCEPT_STMT_ELEMS = frozenset({'except'})\n\n  def Visit_except_clause(self, node):  # pylint: disable=invalid-name\n    self._VisitCompoundStatement(node, self._EXCEPT_STMT_ELEMS)\n\n  _FUNC_DEF_ELEMS = frozenset({'def'})\n\n  def Visit_funcdef(self, node):  # pylint: disable=invalid-name\n    self._VisitCompoundStatement(node, self._FUNC_DEF_ELEMS)\n\n  def Visit_async_funcdef(self, node):  # pylint: disable=invalid-name\n    self._StartNewLine()\n    index = 0\n    for child in node.children:\n      index += 1\n      self.Visit(child)\n      if child.type == grammar_token.ASYNC:\n        break\n    for child in node.children[index].children:\n      self.Visit(child)\n\n  _CLASS_DEF_ELEMS = frozenset({'class'})\n\n  def Visit_classdef(self, node):  # pylint: disable=invalid-name\n    self._VisitCompoundStatement(node, self._CLASS_DEF_ELEMS)\n\n  def Visit_async_stmt(self, node):  # pylint: disable=invalid-name\n    self._StartNewLine()\n    index = 0\n    for child in node.children:\n      index += 1\n      self.Visit(child)\n      if child.type == grammar_token.ASYNC:\n        break\n    for child in node.children[index].children:\n      if child.type == grammar_token.NAME and child.value == 'else':\n        self._StartNewLine()\n      self.Visit(child)\n\n  def Visit_decorator(self, node):  # pylint: disable=invalid-name\n    for child in node.children:\n      self.Visit(child)\n      if child.type == grammar_token.COMMENT and child == node.children[0]:\n        self._StartNewLine()\n\n  def Visit_decorators(self, node):  # pylint: disable=invalid-name\n    for child in node.children:\n      self._StartNewLine()\n      self.Visit(child)\n\n  def Visit_decorated(self, node):  # pylint: disable=invalid-name\n    for child in node.children:\n      self._StartNewLine()\n      self.Visit(child)\n\n  _WITH_STMT_ELEMS = frozenset({'with'})\n\n  def Visit_with_stmt(self, node):  # pylint: disable=invalid-name\n    self._VisitCompoundStatement(node, self._WITH_STMT_ELEMS)\n\n  _MATCH_STMT_ELEMS = frozenset({'match', 'case'})\n\n  def Visit_match_stmt(self, node):  # pylint: disable=invalid-name\n    self._VisitCompoundStatement(node, self._MATCH_STMT_ELEMS)\n\n  # case_block refers to the grammar element name in Grammar.txt\n  _CASE_BLOCK_ELEMS = frozenset({'case'})\n\n  def Visit_case_block(self, node):\n    self._cur_depth += 1\n    self._StartNewLine()\n    self._VisitCompoundStatement(node, self._CASE_BLOCK_ELEMS)\n    self._cur_depth -= 1\n\n  def Visit_suite(self, node):  # pylint: disable=invalid-name\n    # A 'suite' starts a new indentation level in Python.\n    self._cur_depth += 1\n    self._StartNewLine()\n    self.DefaultNodeVisit(node)\n    self._cur_depth -= 1\n\n  def Visit_listmaker(self, node):  # pylint: disable=invalid-name\n    _DetermineMustSplitAnnotation(node)\n    self.DefaultNodeVisit(node)\n\n  def Visit_dictsetmaker(self, node):  # pylint: disable=invalid-name\n    _DetermineMustSplitAnnotation(node)\n    self.DefaultNodeVisit(node)\n\n  def Visit_import_as_names(self, node):  # pylint: disable=invalid-name\n    if node.prev_sibling.value == '(':\n      _DetermineMustSplitAnnotation(node)\n    self.DefaultNodeVisit(node)\n\n  def Visit_testlist_gexp(self, node):  # pylint: disable=invalid-name\n    _DetermineMustSplitAnnotation(node)\n    self.DefaultNodeVisit(node)\n\n  def Visit_arglist(self, node):  # pylint: disable=invalid-name\n    _DetermineMustSplitAnnotation(node)\n    self.DefaultNodeVisit(node)\n\n  def Visit_typedargslist(self, node):  # pylint: disable=invalid-name\n    _DetermineMustSplitAnnotation(node)\n    self.DefaultNodeVisit(node)\n\n  def Visit_subscriptlist(self, node):  # pylint: disable=invalid-name\n    _DetermineMustSplitAnnotation(node)\n    self.DefaultNodeVisit(node)\n\n  def DefaultLeafVisit(self, leaf):\n    \"\"\"Default visitor for tree leaves.\n\n    A tree leaf is always just gets appended to the current logical line.\n\n    Arguments:\n      leaf: the leaf to visit.\n    \"\"\"\n    if leaf.type in _WHITESPACE_TOKENS:\n      self._StartNewLine()\n    elif leaf.type != grammar_token.COMMENT or leaf.value.strip():\n      # Add non-whitespace tokens and comments that aren't empty.\n      self._cur_logical_line.AppendToken(\n          format_token.FormatToken(leaf, pytree_utils.NodeName(leaf)))\n\n\n_BRACKET_MATCH = {')': '(', '}': '{', ']': '['}\n\n\ndef _MatchBrackets(line):\n  \"\"\"Visit the node and match the brackets.\n\n  For every open bracket ('[', '{', or '('), find the associated closing bracket\n  and \"match\" them up. I.e., save in the token a pointer to its associated open\n  or close bracket.\n\n  Arguments:\n    line: (LogicalLine) A logical line.\n  \"\"\"\n  bracket_stack = []\n  for token in line.tokens:\n    if token.value in _OPENING_BRACKETS:\n      bracket_stack.append(token)\n    elif token.value in _CLOSING_BRACKETS:\n      bracket_stack[-1].matching_bracket = token\n      token.matching_bracket = bracket_stack[-1]\n      bracket_stack.pop()\n\n    for bracket in bracket_stack:\n      if id(pytree_utils.GetOpeningBracket(token.node)) == id(bracket.node):\n        bracket.container_elements.append(token)\n        token.container_opening = bracket\n\n\ndef _IdentifyParameterLists(line):\n  \"\"\"Visit the node to create a state for parameter lists.\n\n  For instance, a parameter is considered an \"object\" with its first and last\n  token uniquely identifying the object.\n\n  Arguments:\n    line: (LogicalLine) A logical line.\n  \"\"\"\n  func_stack = []\n  param_stack = []\n  for tok in line.tokens:\n    # Identify parameter list objects.\n    if subtypes.FUNC_DEF in tok.subtypes:\n      assert tok.next_token.value == '('\n      func_stack.append(tok.next_token)\n      continue\n\n    if func_stack and tok.value == ')':\n      if tok == func_stack[-1].matching_bracket:\n        func_stack.pop()\n      continue\n\n    # Identify parameter objects.\n    if subtypes.PARAMETER_START in tok.subtypes:\n      param_stack.append(tok)\n\n    # Not \"elif\", a parameter could be a single token.\n    if param_stack and subtypes.PARAMETER_STOP in tok.subtypes:\n      start = param_stack.pop()\n      func_stack[-1].parameters.append(object_state.Parameter(start, tok))\n\n\ndef _AdjustSplitPenalty(line):\n  \"\"\"Visit the node and adjust the split penalties if needed.\n\n  A token shouldn't be split if it's not within a bracket pair. Mark any token\n  that's not within a bracket pair as \"unbreakable\".\n\n  Arguments:\n    line: (LogicalLine) An logical line.\n  \"\"\"\n  bracket_level = 0\n  for index, token in enumerate(line.tokens):\n    if index and not bracket_level:\n      pytree_utils.SetNodeAnnotation(token.node,\n                                     pytree_utils.Annotation.SPLIT_PENALTY,\n                                     split_penalty.UNBREAKABLE)\n    if token.value in _OPENING_BRACKETS:\n      bracket_level += 1\n    elif token.value in _CLOSING_BRACKETS:\n      bracket_level -= 1\n\n\ndef _DetermineMustSplitAnnotation(node):\n  \"\"\"Enforce a split in the list if the list ends with a comma.\"\"\"\n\n  def SplitBecauseTrailingComma():\n    if style.Get('DISABLE_ENDING_COMMA_HEURISTIC'):\n      return False\n    token = next(node.parent.leaves())\n    if token.value == '(':\n      if sum(1 for ch in node.children if ch.type == grammar_token.COMMA) < 2:\n        return False\n    if (not isinstance(node.children[-1], pytree.Leaf) or\n        node.children[-1].value != ','):\n      return False\n    return True\n\n  def SplitBecauseListContainsComment():\n    return (not style.Get('DISABLE_SPLIT_LIST_WITH_COMMENT') and\n            _ContainsComments(node))\n\n  if (not SplitBecauseTrailingComma() and\n      not SplitBecauseListContainsComment()):\n    return\n\n  num_children = len(node.children)\n  index = 0\n  _SetMustSplitOnFirstLeaf(node.children[0])\n  while index < num_children - 1:\n    child = node.children[index]\n    if isinstance(child, pytree.Leaf) and child.value == ',':\n      next_child = node.children[index + 1]\n      if next_child.type == grammar_token.COMMENT:\n        index += 1\n        if index >= num_children - 1:\n          break\n      _SetMustSplitOnFirstLeaf(node.children[index + 1])\n    index += 1\n\n\ndef _ContainsComments(node):\n  \"\"\"Return True if the list has a comment in it.\"\"\"\n  if isinstance(node, pytree.Leaf):\n    return node.type == grammar_token.COMMENT\n  for child in node.children:\n    if _ContainsComments(child):\n      return True\n  return False\n\n\ndef _SetMustSplitOnFirstLeaf(node):\n  \"\"\"Set the \"must split\" annotation on the first leaf node.\"\"\"\n  pytree_utils.SetNodeAnnotation(\n      pytree_utils.FirstLeafNode(node), pytree_utils.Annotation.MUST_SPLIT,\n      True)\n"
  },
  {
    "path": "yapf/pytree/pytree_utils.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"pytree-related utilities.\n\nThis module collects various utilities related to the parse trees produced by\nthe lib2to3 library.\n\n  NodeName(): produces a string name for pytree nodes.\n  ParseCodeToTree(): convenience wrapper around lib2to3 interfaces to parse\n                     a given string with code to a pytree.\n  InsertNodeBefore(): insert a node before another in a pytree.\n  InsertNodeAfter(): insert a node after another in a pytree.\n  {Get,Set}NodeAnnotation(): manage custom annotations on pytree nodes.\n\"\"\"\n\nimport ast\nimport os\n\nfrom yapf_third_party._ylib2to3 import pygram\nfrom yapf_third_party._ylib2to3 import pytree\nfrom yapf_third_party._ylib2to3.pgen2 import driver\nfrom yapf_third_party._ylib2to3.pgen2 import parse\nfrom yapf_third_party._ylib2to3.pgen2 import token\n\n# TODO(eliben): We may want to get rid of this filtering at some point once we\n# have a better understanding of what information we need from the tree. Then,\n# these tokens may be filtered out from the tree before the tree gets to the\n# unwrapper.\nNONSEMANTIC_TOKENS = frozenset(['DEDENT', 'INDENT', 'NEWLINE', 'ENDMARKER'])\n\n\nclass Annotation(object):\n  \"\"\"Annotation names associated with pytrees.\"\"\"\n  CHILD_INDENT = 'child_indent'\n  NEWLINES = 'newlines'\n  MUST_SPLIT = 'must_split'\n  SPLIT_PENALTY = 'split_penalty'\n  SUBTYPE = 'subtype'\n\n\ndef NodeName(node):\n  \"\"\"Produce a string name for a given node.\n\n  For a Leaf this is the token name, and for a Node this is the type.\n\n  Arguments:\n    node: a tree node\n\n  Returns:\n    Name as a string.\n  \"\"\"\n  # Nodes with values < 256 are tokens. Values >= 256 are grammar symbols.\n  if node.type < 256:\n    return token.tok_name[node.type]\n  else:\n    return pygram.python_grammar.number2symbol[node.type]\n\n\ndef FirstLeafNode(node):\n  if isinstance(node, pytree.Leaf):\n    return node\n  return FirstLeafNode(node.children[0])\n\n\ndef LastLeafNode(node):\n  if isinstance(node, pytree.Leaf):\n    return node\n  return LastLeafNode(node.children[-1])\n\n\n# lib2to3 thoughtfully provides pygram.python_grammar_no_print_statement for\n# parsing Python 3 code that wouldn't parse otherwise (when 'print' is used in a\n# context where a keyword is disallowed).\n# It forgets to do the same for 'exec' though. Luckily, Python is amenable to\n# monkey-patching.\n# Note that pygram.python_grammar_no_print_and_exec_statement with \"_and_exec\"\n# will require Python >=3.8.\n_PYTHON_GRAMMAR = pygram.python_grammar_no_print_statement.copy()\ndel _PYTHON_GRAMMAR.keywords['exec']\n\n\ndef ParseCodeToTree(code):\n  \"\"\"Parse the given code to a lib2to3 pytree.\n\n  Arguments:\n    code: a string with the code to parse.\n\n  Raises:\n    SyntaxError if the code is invalid syntax.\n    parse.ParseError if some other parsing failure.\n\n  Returns:\n    The root node of the parsed tree.\n  \"\"\"\n  # This function is tiny, but the incantation for invoking the parser correctly\n  # is sufficiently magical to be worth abstracting away.\n  if not code.endswith(os.linesep):\n    code += os.linesep\n\n  try:\n    parser_driver = driver.Driver(_PYTHON_GRAMMAR, convert=pytree.convert)\n    tree = parser_driver.parse_string(code, debug=False)\n  except parse.ParseError:\n    # Raise a syntax error if the code is invalid python syntax.\n    ast.parse(code)\n    raise\n  return _WrapEndMarker(tree)\n\n\ndef _WrapEndMarker(tree):\n  \"\"\"Wrap a single ENDMARKER token in a \"file_input\" node.\n\n  Arguments:\n    tree: (pytree.Node) The root node of the parsed tree.\n\n  Returns:\n    The root node of the parsed tree. If the tree is a single ENDMARKER node,\n    then that node is wrapped in a \"file_input\" node. That will ensure we don't\n    skip comments attached to that node.\n  \"\"\"\n  if isinstance(tree, pytree.Leaf) and tree.type == token.ENDMARKER:\n    return pytree.Node(pygram.python_symbols.file_input, [tree])\n  return tree\n\n\ndef InsertNodesBefore(new_nodes, target):\n  \"\"\"Insert new_nodes before the given target location in the tree.\n\n  Arguments:\n    new_nodes: a sequence of new nodes to insert (the nodes should not be in the\n      tree).\n    target: the target node before which the new node node will be inserted.\n\n  Raises:\n    RuntimeError: if the tree is corrupted, or the insertion would corrupt it.\n  \"\"\"\n  for node in new_nodes:\n    _InsertNodeAt(node, target, after=False)\n\n\ndef InsertNodesAfter(new_nodes, target):\n  \"\"\"Insert new_nodes after the given target location in the tree.\n\n  Arguments:\n    new_nodes: a sequence of new nodes to insert (the nodes should not be in the\n      tree).\n    target: the target node after which the new node node will be inserted.\n\n  Raises:\n    RuntimeError: if the tree is corrupted, or the insertion would corrupt it.\n  \"\"\"\n  for node in reversed(new_nodes):\n    _InsertNodeAt(node, target, after=True)\n\n\ndef _InsertNodeAt(new_node, target, after=False):\n  \"\"\"Underlying implementation for node insertion.\n\n  Arguments:\n    new_node: a new node to insert (this node should not be in the tree).\n    target: the target node.\n    after: if True, new_node is inserted after target. Otherwise, it's inserted\n      before target.\n\n  Returns:\n    nothing\n\n  Raises:\n    RuntimeError: if the tree is corrupted, or the insertion would corrupt it.\n  \"\"\"\n\n  # Protect against attempts to insert nodes which already belong to some tree.\n  if new_node.parent is not None:\n    raise RuntimeError('inserting node which already has a parent',\n                       (new_node, new_node.parent))\n\n  # The code here is based on pytree.Base.next_sibling\n  parent_of_target = target.parent\n  if parent_of_target is None:\n    raise RuntimeError('expected target node to have a parent', (target,))\n\n  for i, child in enumerate(parent_of_target.children):\n    if child is target:\n      insertion_index = i + 1 if after else i\n      parent_of_target.insert_child(insertion_index, new_node)\n      return\n\n  raise RuntimeError('unable to find insertion point for target node',\n                     (target,))\n\n\n# The following constant and functions implement a simple custom annotation\n# mechanism for pytree nodes. We attach new attributes to nodes. Each attribute\n# is prefixed with _NODE_ANNOTATION_PREFIX. These annotations should only be\n# managed through GetNodeAnnotation and SetNodeAnnotation.\n_NODE_ANNOTATION_PREFIX = '_yapf_annotation_'\n\n\ndef CopyYapfAnnotations(src, dst):\n  \"\"\"Copy all YAPF annotations from the source node to the destination node.\n\n  Arguments:\n    src: the source node.\n    dst: the destination node.\n  \"\"\"\n  for annotation in dir(src):\n    if annotation.startswith(_NODE_ANNOTATION_PREFIX):\n      setattr(dst, annotation, getattr(src, annotation, None))\n\n\ndef GetNodeAnnotation(node, annotation, default=None):\n  \"\"\"Get annotation value from a node.\n\n  Arguments:\n    node: the node.\n    annotation: annotation name - a string.\n    default: the default value to return if there's no annotation.\n\n  Returns:\n    Value of the annotation in the given node. If the node doesn't have this\n    particular annotation name yet, returns default.\n  \"\"\"\n  return getattr(node, _NODE_ANNOTATION_PREFIX + annotation, default)\n\n\ndef SetNodeAnnotation(node, annotation, value):\n  \"\"\"Set annotation value on a node.\n\n  Arguments:\n    node: the node.\n    annotation: annotation name - a string.\n    value: annotation value to set.\n  \"\"\"\n  setattr(node, _NODE_ANNOTATION_PREFIX + annotation, value)\n\n\ndef AppendNodeAnnotation(node, annotation, value):\n  \"\"\"Appends an annotation value to a list of annotations on the node.\n\n  Arguments:\n    node: the node.\n    annotation: annotation name - a string.\n    value: annotation value to set.\n  \"\"\"\n  attr = GetNodeAnnotation(node, annotation, set())\n  attr.add(value)\n  SetNodeAnnotation(node, annotation, attr)\n\n\ndef RemoveSubtypeAnnotation(node, value):\n  \"\"\"Removes an annotation value from the subtype annotations on the node.\n\n  Arguments:\n    node: the node.\n    value: annotation value to remove.\n  \"\"\"\n  attr = GetNodeAnnotation(node, Annotation.SUBTYPE)\n  if attr and value in attr:\n    attr.remove(value)\n    SetNodeAnnotation(node, Annotation.SUBTYPE, attr)\n\n\ndef GetOpeningBracket(node):\n  \"\"\"Get opening bracket value from a node.\n\n  Arguments:\n    node: the node.\n\n  Returns:\n    The opening bracket node or None if it couldn't find one.\n  \"\"\"\n  return getattr(node, _NODE_ANNOTATION_PREFIX + 'container_bracket', None)\n\n\ndef SetOpeningBracket(node, bracket):\n  \"\"\"Set opening bracket value for a node.\n\n  Arguments:\n    node: the node.\n    bracket: opening bracket to set.\n  \"\"\"\n  setattr(node, _NODE_ANNOTATION_PREFIX + 'container_bracket', bracket)\n\n\ndef DumpNodeToString(node):\n  \"\"\"Dump a string representation of the given node. For debugging.\n\n  Arguments:\n    node: the node.\n\n  Returns:\n    The string representation.\n  \"\"\"\n  if isinstance(node, pytree.Leaf):\n    fmt = ('{name}({value}) [lineno={lineno}, column={column}, '\n           'prefix={prefix}, penalty={penalty}]')\n    return fmt.format(\n        name=NodeName(node),\n        value=_PytreeNodeRepr(node),\n        lineno=node.lineno,\n        column=node.column,\n        prefix=repr(node.prefix),\n        penalty=GetNodeAnnotation(node, Annotation.SPLIT_PENALTY, None))\n  else:\n    fmt = '{node} [{len} children] [child_indent=\"{indent}\"]'\n    return fmt.format(\n        node=NodeName(node),\n        len=len(node.children),\n        indent=GetNodeAnnotation(node, Annotation.CHILD_INDENT))\n\n\ndef _PytreeNodeRepr(node):\n  \"\"\"Like pytree.Node.__repr__, but names instead of numbers for tokens.\"\"\"\n  if isinstance(node, pytree.Node):\n    return '%s(%s, %r)' % (node.__class__.__name__, NodeName(node),\n                           [_PytreeNodeRepr(c) for c in node.children])\n  if isinstance(node, pytree.Leaf):\n    return '%s(%s, %r)' % (node.__class__.__name__, NodeName(node), node.value)\n\n\ndef IsCommentStatement(node):\n  return (NodeName(node) == 'simple_stmt' and\n          node.children[0].type == token.COMMENT)\n"
  },
  {
    "path": "yapf/pytree/pytree_visitor.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Generic visitor pattern for pytrees.\n\nThe lib2to3 parser produces a \"pytree\" - syntax tree consisting of Node\nand Leaf types. This module implements a visitor pattern for such trees.\n\nIt also exports a basic \"dumping\" visitor that dumps a textual representation of\na pytree into a stream.\n\n  PyTreeVisitor: a generic visitor pattern for pytrees.\n  PyTreeDumper: a configurable \"dumper\" for displaying pytrees.\n  DumpPyTree(): a convenience function to dump a pytree.\n\"\"\"\n\nimport sys\n\nfrom yapf_third_party._ylib2to3 import pytree\n\nfrom yapf.pytree import pytree_utils\n\n\nclass PyTreeVisitor(object):\n  \"\"\"Visitor pattern for pytree trees.\n\n  Methods named Visit_XXX will be invoked when a node with type XXX is\n  encountered in the tree. The type is either a token type (for Leaf nodes) or\n  grammar symbols (for Node nodes). The return value of Visit_XXX methods is\n  ignored by the visitor.\n\n  Visitors can modify node contents but must not change the tree structure\n  (e.g. add/remove children and move nodes around).\n\n  This is a very common visitor pattern in Python code; it's also used in the\n  Python standard library ast module for providing AST visitors.\n\n  Note: this makes names that aren't style conformant, so such visitor methods\n  need to be marked with # pylint: disable=invalid-name We don't have a choice\n  here, because lib2to3 nodes have under_separated names.\n\n  For more complex behavior, the visit, DefaultNodeVisit and DefaultLeafVisit\n  methods can be overridden. Don't forget to invoke DefaultNodeVisit for nodes\n  that may have children - otherwise the children will not be visited.\n  \"\"\"\n\n  def Visit(self, node):\n    \"\"\"Visit a node.\"\"\"\n    method = 'Visit_{0}'.format(pytree_utils.NodeName(node))\n    if hasattr(self, method):\n      # Found a specific visitor for this node\n      getattr(self, method)(node)\n    else:\n      if isinstance(node, pytree.Leaf):\n        self.DefaultLeafVisit(node)\n      else:\n        self.DefaultNodeVisit(node)\n\n  def DefaultNodeVisit(self, node):\n    \"\"\"Default visitor for Node: visits the node's children depth-first.\n\n    This method is invoked when no specific visitor for the node is defined.\n\n    Arguments:\n      node: the node to visit\n    \"\"\"\n    for child in node.children:\n      self.Visit(child)\n\n  def DefaultLeafVisit(self, leaf):\n    \"\"\"Default visitor for Leaf: no-op.\n\n    This method is invoked when no specific visitor for the leaf is defined.\n\n    Arguments:\n      leaf: the leaf to visit\n    \"\"\"\n    pass\n\n\ndef DumpPyTree(tree, target_stream=sys.stdout):\n  \"\"\"Convenience function for dumping a given pytree.\n\n  This function presents a very minimal interface. For more configurability (for\n  example, controlling how specific node types are displayed), use PyTreeDumper\n  directly.\n\n  Arguments:\n    tree: the tree to dump.\n    target_stream: the stream to dump the tree to. A file-like object. By\n      default will dump into stdout.\n  \"\"\"\n  dumper = PyTreeDumper(target_stream)\n  dumper.Visit(tree)\n\n\nclass PyTreeDumper(PyTreeVisitor):\n  \"\"\"Visitor that dumps the tree to a stream.\n\n  Implements the PyTreeVisitor interface.\n  \"\"\"\n\n  def __init__(self, target_stream=sys.stdout):\n    \"\"\"Create a tree dumper.\n\n    Arguments:\n      target_stream: the stream to dump the tree to. A file-like object. By\n        default will dump into stdout.\n    \"\"\"\n    self._target_stream = target_stream\n    self._current_indent = 0\n\n  def _DumpString(self, s):\n    self._target_stream.write('{0}{1}\\n'.format(' ' * self._current_indent, s))\n\n  def DefaultNodeVisit(self, node):\n    # Dump information about the current node, and then use the generic\n    # DefaultNodeVisit visitor to dump each of its children.\n    self._DumpString(pytree_utils.DumpNodeToString(node))\n    self._current_indent += 2\n    super(PyTreeDumper, self).DefaultNodeVisit(node)\n    self._current_indent -= 2\n\n  def DefaultLeafVisit(self, leaf):\n    self._DumpString(pytree_utils.DumpNodeToString(leaf))\n"
  },
  {
    "path": "yapf/pytree/split_penalty.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Computation of split penalties before/between tokens.\"\"\"\n\nimport re\n\nfrom yapf_third_party._ylib2to3 import pytree\nfrom yapf_third_party._ylib2to3.pgen2 import token as grammar_token\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.pytree import pytree_visitor\nfrom yapf.yapflib import style\nfrom yapf.yapflib import subtypes\n\n# TODO(morbo): Document the annotations in a centralized place. E.g., the\n# README file.\nUNBREAKABLE = 1000 * 1000\nNAMED_ASSIGN = 15000\nDOTTED_NAME = 4000\nVERY_STRONGLY_CONNECTED = 3500\nSTRONGLY_CONNECTED = 3000\nCONNECTED = 500\nTOGETHER = 100\n\nOR_TEST = 1000\nAND_TEST = 1100\nNOT_TEST = 1200\nCOMPARISON = 1300\nSTAR_EXPR = 1300\nEXPR = 1400\nXOR_EXPR = 1500\nAND_EXPR = 1700\nSHIFT_EXPR = 1800\nARITH_EXPR = 1900\nTERM = 2000\nFACTOR = 2100\nPOWER = 2200\nATOM = 2300\nONE_ELEMENT_ARGUMENT = 500\nSUBSCRIPT = 6000\n\n\ndef ComputeSplitPenalties(tree):\n  \"\"\"Compute split penalties on tokens in the given parse tree.\n\n  Arguments:\n    tree: the top-level pytree node to annotate with penalties.\n  \"\"\"\n  _SplitPenaltyAssigner().Visit(tree)\n\n\nclass _SplitPenaltyAssigner(pytree_visitor.PyTreeVisitor):\n  \"\"\"Assigns split penalties to tokens, based on parse tree structure.\n\n  Split penalties are attached as annotations to tokens.\n  \"\"\"\n\n  def Visit(self, node):\n    if not hasattr(node, 'is_pseudo'):  # Ignore pseudo tokens.\n      super(_SplitPenaltyAssigner, self).Visit(node)\n\n  def Visit_import_as_names(self, node):  # pyline: disable=invalid-name\n    # import_as_names ::= import_as_name (',' import_as_name)* [',']\n    self.DefaultNodeVisit(node)\n    prev_child = None\n    for child in node.children:\n      if (prev_child and isinstance(prev_child, pytree.Leaf) and\n          prev_child.value == ','):\n        _SetSplitPenalty(child, style.Get('SPLIT_PENALTY_IMPORT_NAMES'))\n      prev_child = child\n\n  def Visit_classdef(self, node):  # pylint: disable=invalid-name\n    # classdef ::= 'class' NAME ['(' [arglist] ')'] ':' suite\n    #\n    # NAME\n    _SetUnbreakable(node.children[1])\n    if len(node.children) > 4:\n      # opening '('\n      _SetUnbreakable(node.children[2])\n    # ':'\n    _SetUnbreakable(node.children[-2])\n    self.DefaultNodeVisit(node)\n\n  def Visit_funcdef(self, node):  # pylint: disable=invalid-name\n    # funcdef ::= 'def' NAME parameters ['->' test] ':' suite\n    #\n    # Can't break before the function name and before the colon. The parameters\n    # are handled by child iteration.\n    colon_idx = 1\n    while pytree_utils.NodeName(node.children[colon_idx]) == 'simple_stmt':\n      colon_idx += 1\n    _SetUnbreakable(node.children[colon_idx])\n    arrow_idx = -1\n    while colon_idx < len(node.children):\n      if isinstance(node.children[colon_idx], pytree.Leaf):\n        if node.children[colon_idx].value == ':':\n          break\n        if node.children[colon_idx].value == '->':\n          arrow_idx = colon_idx\n      colon_idx += 1\n    _SetUnbreakable(node.children[colon_idx])\n    self.DefaultNodeVisit(node)\n    if arrow_idx > 0:\n      _SetSplitPenalty(\n          pytree_utils.LastLeafNode(node.children[arrow_idx - 1]), 0)\n      _SetUnbreakable(node.children[arrow_idx])\n      _SetStronglyConnected(node.children[arrow_idx + 1])\n\n  def Visit_lambdef(self, node):  # pylint: disable=invalid-name\n    # lambdef ::= 'lambda' [varargslist] ':' test\n    # Loop over the lambda up to and including the colon.\n    allow_multiline_lambdas = style.Get('ALLOW_MULTILINE_LAMBDAS')\n    if not allow_multiline_lambdas:\n      for child in node.children:\n        if child.type == grammar_token.COMMENT:\n          if re.search(r'pylint:.*disable=.*\\bg-long-lambda', child.value):\n            allow_multiline_lambdas = True\n            break\n\n    if allow_multiline_lambdas:\n      _SetExpressionPenalty(node, STRONGLY_CONNECTED)\n    else:\n      _SetExpressionPenalty(node, VERY_STRONGLY_CONNECTED)\n\n  def Visit_parameters(self, node):  # pylint: disable=invalid-name\n    # parameters ::= '(' [typedargslist] ')'\n    self.DefaultNodeVisit(node)\n\n    # Can't break before the opening paren of a parameter list.\n    _SetUnbreakable(node.children[0])\n    if not (style.Get('INDENT_CLOSING_BRACKETS') or\n            style.Get('DEDENT_CLOSING_BRACKETS')):\n      _SetStronglyConnected(node.children[-1])\n\n  def Visit_arglist(self, node):  # pylint: disable=invalid-name\n    # arglist ::= argument (',' argument)* [',']\n    if node.children[0].type == grammar_token.STAR:\n      # Python 3 treats a star expression as a specific expression type.\n      # Process it in that method.\n      self.Visit_star_expr(node)\n      return\n\n    self.DefaultNodeVisit(node)\n\n    for index in range(1, len(node.children)):\n      child = node.children[index]\n      if isinstance(child, pytree.Leaf) and child.value == ',':\n        _SetUnbreakable(child)\n\n    for child in node.children:\n      if pytree_utils.NodeName(child) == 'atom':\n        _IncreasePenalty(child, CONNECTED)\n\n  def Visit_argument(self, node):  # pylint: disable=invalid-name\n    # argument ::= test [comp_for] | test '=' test  # Really [keyword '='] test\n    self.DefaultNodeVisit(node)\n\n    for index in range(1, len(node.children) - 1):\n      child = node.children[index]\n      if isinstance(child, pytree.Leaf) and child.value == '=':\n        _SetSplitPenalty(\n            pytree_utils.FirstLeafNode(node.children[index]), NAMED_ASSIGN)\n        _SetSplitPenalty(\n            pytree_utils.FirstLeafNode(node.children[index + 1]), NAMED_ASSIGN)\n\n  def Visit_tname(self, node):  # pylint: disable=invalid-name\n    # tname ::= NAME [':' test]\n    self.DefaultNodeVisit(node)\n\n    for index in range(1, len(node.children) - 1):\n      child = node.children[index]\n      if isinstance(child, pytree.Leaf) and child.value == ':':\n        _SetSplitPenalty(\n            pytree_utils.FirstLeafNode(node.children[index]), NAMED_ASSIGN)\n        _SetSplitPenalty(\n            pytree_utils.FirstLeafNode(node.children[index + 1]), NAMED_ASSIGN)\n\n  def Visit_dotted_name(self, node):  # pylint: disable=invalid-name\n    # dotted_name ::= NAME ('.' NAME)*\n    for child in node.children:\n      self.Visit(child)\n    start = 2 if hasattr(node.children[0], 'is_pseudo') else 1\n    for i in range(start, len(node.children)):\n      _SetUnbreakable(node.children[i])\n\n  def Visit_dictsetmaker(self, node):  # pylint: disable=invalid-name\n    # dictsetmaker ::= ( (test ':' test\n    #                      (comp_for | (',' test ':' test)* [','])) |\n    #                    (test (comp_for | (',' test)* [','])) )\n    for child in node.children:\n      self.Visit(child)\n      if child.type == grammar_token.COLON:\n        # This is a key to a dictionary. We don't want to split the key if at\n        # all possible.\n        _SetStronglyConnected(child)\n\n  def Visit_trailer(self, node):  # pylint: disable=invalid-name\n    # trailer ::= '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME\n    if node.children[0].value == '.':\n      before = style.Get('SPLIT_BEFORE_DOT')\n      _SetSplitPenalty(node.children[0],\n                       VERY_STRONGLY_CONNECTED if before else DOTTED_NAME)\n      _SetSplitPenalty(node.children[1],\n                       DOTTED_NAME if before else VERY_STRONGLY_CONNECTED)\n    elif len(node.children) == 2:\n      # Don't split an empty argument list if at all possible.\n      _SetSplitPenalty(node.children[1], VERY_STRONGLY_CONNECTED)\n    elif len(node.children) == 3:\n      name = pytree_utils.NodeName(node.children[1])\n      if name in {'argument', 'comparison'}:\n        # Don't split an argument list with one element if at all possible.\n        _SetStronglyConnected(node.children[1])\n        if (len(node.children[1].children) > 1 and\n            pytree_utils.NodeName(node.children[1].children[1]) == 'comp_for'):\n          # Don't penalize splitting before a comp_for expression.\n          _SetSplitPenalty(pytree_utils.FirstLeafNode(node.children[1]), 0)\n        else:\n          _SetSplitPenalty(\n              pytree_utils.FirstLeafNode(node.children[1]),\n              ONE_ELEMENT_ARGUMENT)\n      elif (node.children[0].type == grammar_token.LSQB and\n            len(node.children[1].children) > 2 and\n            (name.endswith('_test') or name.endswith('_expr'))):\n        _SetStronglyConnected(node.children[1].children[0])\n        _SetStronglyConnected(node.children[1].children[2])\n\n        # Still allow splitting around the operator.\n        split_before = ((name.endswith('_test') and\n                         style.Get('SPLIT_BEFORE_LOGICAL_OPERATOR')) or\n                        (name.endswith('_expr') and\n                         style.Get('SPLIT_BEFORE_BITWISE_OPERATOR')))\n        if split_before:\n          _SetSplitPenalty(\n              pytree_utils.LastLeafNode(node.children[1].children[1]), 0)\n        else:\n          _SetSplitPenalty(\n              pytree_utils.FirstLeafNode(node.children[1].children[2]), 0)\n\n        # Don't split the ending bracket of a subscript list.\n        _RecAnnotate(node.children[-1], pytree_utils.Annotation.SPLIT_PENALTY,\n                     VERY_STRONGLY_CONNECTED)\n      elif name not in {\n          'arglist', 'argument', 'term', 'or_test', 'and_test', 'comparison',\n          'atom', 'power'\n      }:\n        # Don't split an argument list with one element if at all possible.\n        stypes = pytree_utils.GetNodeAnnotation(\n            pytree_utils.FirstLeafNode(node), pytree_utils.Annotation.SUBTYPE)\n        if stypes and subtypes.SUBSCRIPT_BRACKET in stypes:\n          _IncreasePenalty(node, SUBSCRIPT)\n\n          # Bump up the split penalty for the first part of a subscript. We\n          # would rather not split there.\n          _IncreasePenalty(node.children[1], CONNECTED)\n        else:\n          _SetStronglyConnected(node.children[1], node.children[2])\n\n      if name == 'arglist':\n        _SetStronglyConnected(node.children[-1])\n\n    self.DefaultNodeVisit(node)\n\n  def Visit_power(self, node):  # pylint: disable=invalid-name,missing-docstring\n    # power ::= atom trailer* ['**' factor]\n    self.DefaultNodeVisit(node)\n\n    # When atom is followed by a trailer, we can not break between them.\n    # E.g. arr[idx] - no break allowed between 'arr' and '['.\n    if (len(node.children) > 1 and\n        pytree_utils.NodeName(node.children[1]) == 'trailer'):\n      # children[1] itself is a whole trailer: we don't want to\n      # mark all of it as unbreakable, only its first token: (, [ or .\n      first = pytree_utils.FirstLeafNode(node.children[1])\n      if first.value != '.':\n        _SetUnbreakable(node.children[1].children[0])\n\n      # A special case when there are more trailers in the sequence. Given:\n      #   atom tr1 tr2\n      # The last token of tr1 and the first token of tr2 comprise an unbreakable\n      # region. For example: foo.bar.baz(1)\n      # We can't put breaks between either of the '.', '(', or '[' and the names\n      # *preceding* them.\n      prev_trailer_idx = 1\n      while prev_trailer_idx < len(node.children) - 1:\n        cur_trailer_idx = prev_trailer_idx + 1\n        cur_trailer = node.children[cur_trailer_idx]\n        if pytree_utils.NodeName(cur_trailer) != 'trailer':\n          break\n\n        # Now we know we have two trailers one after the other\n        prev_trailer = node.children[prev_trailer_idx]\n        if prev_trailer.children[-1].value != ')':\n          # Set the previous node unbreakable if it's not a function call:\n          #   atom tr1() tr2\n          # It may be necessary (though undesirable) to split up a previous\n          # function call's parentheses to the next line.\n          _SetStronglyConnected(prev_trailer.children[-1])\n        _SetStronglyConnected(cur_trailer.children[0])\n        prev_trailer_idx = cur_trailer_idx\n\n    # We don't want to split before the last ')' of a function call. This also\n    # takes care of the special case of:\n    #   atom tr1 tr2 ... trn\n    # where the 'tr#' are trailers that may end in a ')'.\n    for trailer in node.children[1:]:\n      if pytree_utils.NodeName(trailer) != 'trailer':\n        break\n      if trailer.children[0].value in '([':\n        if len(trailer.children) > 2:\n          stypes = pytree_utils.GetNodeAnnotation(\n              trailer.children[0], pytree_utils.Annotation.SUBTYPE)\n          if stypes and subtypes.SUBSCRIPT_BRACKET in stypes:\n            _SetStronglyConnected(\n                pytree_utils.FirstLeafNode(trailer.children[1]))\n\n          last_child_node = pytree_utils.LastLeafNode(trailer)\n          if last_child_node.value.strip().startswith('#'):\n            last_child_node = last_child_node.prev_sibling\n          if not (style.Get('INDENT_CLOSING_BRACKETS') or\n                  style.Get('DEDENT_CLOSING_BRACKETS')):\n            last = pytree_utils.LastLeafNode(last_child_node.prev_sibling)\n            if last.value != ',':\n              if last_child_node.value == ']':\n                _SetUnbreakable(last_child_node)\n              else:\n                _SetSplitPenalty(last_child_node, VERY_STRONGLY_CONNECTED)\n        else:\n          # If the trailer's children are '()', then make it a strongly\n          # connected region.  It's sometimes necessary, though undesirable, to\n          # split the two.\n          _SetStronglyConnected(trailer.children[-1])\n\n  def Visit_subscriptlist(self, node):  # pylint: disable=invalid-name\n    # subscriptlist ::= subscript (',' subscript)* [',']\n    self.DefaultNodeVisit(node)\n    _SetSplitPenalty(pytree_utils.FirstLeafNode(node), 0)\n    prev_child = None\n    for child in node.children:\n      if prev_child and prev_child.type == grammar_token.COMMA:\n        _SetSplitPenalty(pytree_utils.FirstLeafNode(child), 0)\n      prev_child = child\n\n  def Visit_subscript(self, node):  # pylint: disable=invalid-name\n    # subscript ::= test | [test] ':' [test] [sliceop]\n    _SetStronglyConnected(*node.children)\n    self.DefaultNodeVisit(node)\n\n  def Visit_comp_for(self, node):  # pylint: disable=invalid-name\n    # comp_for ::= 'for' exprlist 'in' testlist_safe [comp_iter]\n    _SetSplitPenalty(pytree_utils.FirstLeafNode(node), 0)\n    _SetStronglyConnected(*node.children[1:])\n    self.DefaultNodeVisit(node)\n\n  def Visit_old_comp_for(self, node):  # pylint: disable=invalid-name\n    # Python 3.7\n    self.Visit_comp_for(node)\n\n  def Visit_comp_if(self, node):  # pylint: disable=invalid-name\n    # comp_if ::= 'if' old_test [comp_iter]\n    _SetSplitPenalty(node.children[0],\n                     style.Get('SPLIT_PENALTY_BEFORE_IF_EXPR'))\n    _SetStronglyConnected(*node.children[1:])\n    self.DefaultNodeVisit(node)\n\n  def Visit_old_comp_if(self, node):  # pylint: disable=invalid-name\n    # Python 3.7\n    self.Visit_comp_if(node)\n\n  def Visit_test(self, node):  # pylint: disable=invalid-name\n    # test ::= or_test ['if' or_test 'else' test] | lambdef\n    _IncreasePenalty(node, OR_TEST)\n    self.DefaultNodeVisit(node)\n\n  def Visit_or_test(self, node):  # pylint: disable=invalid-name\n    # or_test ::= and_test ('or' and_test)*\n    self.DefaultNodeVisit(node)\n    _IncreasePenalty(node, OR_TEST)\n    index = 1\n    while index + 1 < len(node.children):\n      if style.Get('SPLIT_BEFORE_LOGICAL_OPERATOR'):\n        _DecrementSplitPenalty(\n            pytree_utils.FirstLeafNode(node.children[index]), OR_TEST)\n      else:\n        _DecrementSplitPenalty(\n            pytree_utils.FirstLeafNode(node.children[index + 1]), OR_TEST)\n      index += 2\n\n  def Visit_and_test(self, node):  # pylint: disable=invalid-name\n    # and_test ::= not_test ('and' not_test)*\n    self.DefaultNodeVisit(node)\n    _IncreasePenalty(node, AND_TEST)\n    index = 1\n    while index + 1 < len(node.children):\n      if style.Get('SPLIT_BEFORE_LOGICAL_OPERATOR'):\n        _DecrementSplitPenalty(\n            pytree_utils.FirstLeafNode(node.children[index]), AND_TEST)\n      else:\n        _DecrementSplitPenalty(\n            pytree_utils.FirstLeafNode(node.children[index + 1]), AND_TEST)\n      index += 2\n\n  def Visit_not_test(self, node):  # pylint: disable=invalid-name\n    # not_test ::= 'not' not_test | comparison\n    self.DefaultNodeVisit(node)\n    _IncreasePenalty(node, NOT_TEST)\n\n  def Visit_comparison(self, node):  # pylint: disable=invalid-name\n    # comparison ::= expr (comp_op expr)*\n    self.DefaultNodeVisit(node)\n    if len(node.children) == 3 and _StronglyConnectedCompOp(node):\n      _IncreasePenalty(node.children[1], VERY_STRONGLY_CONNECTED)\n      _SetSplitPenalty(\n          pytree_utils.FirstLeafNode(node.children[2]), STRONGLY_CONNECTED)\n    else:\n      _IncreasePenalty(node, COMPARISON)\n\n  def Visit_star_expr(self, node):  # pylint: disable=invalid-name\n    # star_expr ::= '*' expr\n    self.DefaultNodeVisit(node)\n    _IncreasePenalty(node, STAR_EXPR)\n\n  def Visit_expr(self, node):  # pylint: disable=invalid-name\n    # expr ::= xor_expr ('|' xor_expr)*\n    self.DefaultNodeVisit(node)\n    _IncreasePenalty(node, EXPR)\n    _SetBitwiseOperandPenalty(node, '|')\n\n  def Visit_xor_expr(self, node):  # pylint: disable=invalid-name\n    # xor_expr ::= and_expr ('^' and_expr)*\n    self.DefaultNodeVisit(node)\n    _IncreasePenalty(node, XOR_EXPR)\n    _SetBitwiseOperandPenalty(node, '^')\n\n  def Visit_and_expr(self, node):  # pylint: disable=invalid-name\n    # and_expr ::= shift_expr ('&' shift_expr)*\n    self.DefaultNodeVisit(node)\n    _IncreasePenalty(node, AND_EXPR)\n    _SetBitwiseOperandPenalty(node, '&')\n\n  def Visit_shift_expr(self, node):  # pylint: disable=invalid-name\n    # shift_expr ::= arith_expr (('<<'|'>>') arith_expr)*\n    self.DefaultNodeVisit(node)\n    _IncreasePenalty(node, SHIFT_EXPR)\n\n  _ARITH_OPS = frozenset({'PLUS', 'MINUS'})\n\n  def Visit_arith_expr(self, node):  # pylint: disable=invalid-name\n    # arith_expr ::= term (('+'|'-') term)*\n    self.DefaultNodeVisit(node)\n    _IncreasePenalty(node, ARITH_EXPR)\n    _SetExpressionOperandPenalty(node, self._ARITH_OPS)\n\n  _TERM_OPS = frozenset({'STAR', 'AT', 'SLASH', 'PERCENT', 'DOUBLESLASH'})\n\n  def Visit_term(self, node):  # pylint: disable=invalid-name\n    # term ::= factor (('*'|'@'|'/'|'%'|'//') factor)*\n    self.DefaultNodeVisit(node)\n    _IncreasePenalty(node, TERM)\n    _SetExpressionOperandPenalty(node, self._TERM_OPS)\n\n  def Visit_factor(self, node):  # pyline: disable=invalid-name\n    # factor ::= ('+'|'-'|'~') factor | power\n    self.DefaultNodeVisit(node)\n    _IncreasePenalty(node, FACTOR)\n\n  def Visit_atom(self, node):  # pylint: disable=invalid-name\n    # atom ::= ('(' [yield_expr|testlist_gexp] ')'\n    #           '[' [listmaker] ']' |\n    #           '{' [dictsetmaker] '}')\n    self.DefaultNodeVisit(node)\n    if (node.children[0].value == '(' and\n        not hasattr(node.children[0], 'is_pseudo')):\n      if node.children[-1].value == ')':\n        if pytree_utils.NodeName(node.parent) == 'if_stmt':\n          _SetSplitPenalty(node.children[-1], STRONGLY_CONNECTED)\n        else:\n          if len(node.children) > 2:\n            _SetSplitPenalty(pytree_utils.FirstLeafNode(node.children[1]), EXPR)\n          _SetSplitPenalty(node.children[-1], ATOM)\n    elif node.children[0].value in '[{' and len(node.children) == 2:\n      # Keep empty containers together if we can.\n      _SetUnbreakable(node.children[-1])\n\n  def Visit_testlist_gexp(self, node):  # pylint: disable=invalid-name\n    self.DefaultNodeVisit(node)\n    prev_was_comma = False\n    for child in node.children:\n      if isinstance(child, pytree.Leaf) and child.value == ',':\n        _SetUnbreakable(child)\n        prev_was_comma = True\n      else:\n        if prev_was_comma:\n          _SetSplitPenalty(pytree_utils.FirstLeafNode(child), TOGETHER)\n        prev_was_comma = False\n\n\ndef _SetUnbreakable(node):\n  \"\"\"Set an UNBREAKABLE penalty annotation for the given node.\"\"\"\n  _RecAnnotate(node, pytree_utils.Annotation.SPLIT_PENALTY, UNBREAKABLE)\n\n\ndef _SetStronglyConnected(*nodes):\n  \"\"\"Set a STRONGLY_CONNECTED penalty annotation for the given nodes.\"\"\"\n  for node in nodes:\n    _RecAnnotate(node, pytree_utils.Annotation.SPLIT_PENALTY,\n                 STRONGLY_CONNECTED)\n\n\ndef _SetExpressionPenalty(node, penalty):\n  \"\"\"Set a penalty annotation on children nodes.\"\"\"\n\n  def RecExpression(node, first_child_leaf):\n    if node is first_child_leaf:\n      return\n\n    if isinstance(node, pytree.Leaf):\n      if node.value in {'(', 'for', 'if'}:\n        return\n      penalty_annotation = pytree_utils.GetNodeAnnotation(\n          node, pytree_utils.Annotation.SPLIT_PENALTY, default=0)\n      if penalty_annotation < penalty:\n        _SetSplitPenalty(node, penalty)\n    else:\n      for child in node.children:\n        RecExpression(child, first_child_leaf)\n\n  RecExpression(node, pytree_utils.FirstLeafNode(node))\n\n\ndef _SetBitwiseOperandPenalty(node, op):\n  for index in range(1, len(node.children) - 1):\n    child = node.children[index]\n    if isinstance(child, pytree.Leaf) and child.value == op:\n      if style.Get('SPLIT_BEFORE_BITWISE_OPERATOR'):\n        _SetSplitPenalty(child, style.Get('SPLIT_PENALTY_BITWISE_OPERATOR'))\n      else:\n        _SetSplitPenalty(\n            pytree_utils.FirstLeafNode(node.children[index + 1]),\n            style.Get('SPLIT_PENALTY_BITWISE_OPERATOR'))\n\n\ndef _SetExpressionOperandPenalty(node, ops):\n  for index in range(1, len(node.children) - 1):\n    child = node.children[index]\n    if pytree_utils.NodeName(child) in ops:\n      if style.Get('SPLIT_BEFORE_ARITHMETIC_OPERATOR'):\n        _SetSplitPenalty(child, style.Get('SPLIT_PENALTY_ARITHMETIC_OPERATOR'))\n      else:\n        _SetSplitPenalty(\n            pytree_utils.FirstLeafNode(node.children[index + 1]),\n            style.Get('SPLIT_PENALTY_ARITHMETIC_OPERATOR'))\n\n\ndef _IncreasePenalty(node, amt):\n  \"\"\"Increase a penalty annotation on children nodes.\"\"\"\n\n  def RecExpression(node, first_child_leaf):\n    if node is first_child_leaf:\n      return\n\n    if isinstance(node, pytree.Leaf):\n      if node.value in {'(', 'for'}:\n        return\n      penalty = pytree_utils.GetNodeAnnotation(\n          node, pytree_utils.Annotation.SPLIT_PENALTY, default=0)\n      _SetSplitPenalty(node, penalty + amt)\n    else:\n      for child in node.children:\n        RecExpression(child, first_child_leaf)\n\n  RecExpression(node, pytree_utils.FirstLeafNode(node))\n\n\ndef _RecAnnotate(tree, annotate_name, annotate_value):\n  \"\"\"Recursively set the given annotation on all leafs of the subtree.\n\n  Takes care to only increase the penalty. If the node already has a higher\n  or equal penalty associated with it, this is a no-op.\n\n  Args:\n    tree: subtree to annotate\n    annotate_name: name of the annotation to set\n    annotate_value: value of the annotation to set\n  \"\"\"\n  for child in tree.children:\n    _RecAnnotate(child, annotate_name, annotate_value)\n  if isinstance(tree, pytree.Leaf):\n    cur_annotate = pytree_utils.GetNodeAnnotation(\n        tree, annotate_name, default=0)\n    if cur_annotate < annotate_value:\n      pytree_utils.SetNodeAnnotation(tree, annotate_name, annotate_value)\n\n\n_COMP_OPS = frozenset({'==', '!=', '<=', '<', '>', '>=', '<>', 'in', 'is'})\n\n\ndef _StronglyConnectedCompOp(op):\n  if (len(op.children[1].children) == 2 and\n      pytree_utils.NodeName(op.children[1]) == 'comp_op'):\n    if (pytree_utils.FirstLeafNode(op.children[1]).value == 'not' and\n        pytree_utils.LastLeafNode(op.children[1]).value == 'in'):\n      return True\n    if (pytree_utils.FirstLeafNode(op.children[1]).value == 'is' and\n        pytree_utils.LastLeafNode(op.children[1]).value == 'not'):\n      return True\n  if (isinstance(op.children[1], pytree.Leaf) and\n      op.children[1].value in _COMP_OPS):\n    return True\n  return False\n\n\ndef _DecrementSplitPenalty(node, amt):\n  penalty = pytree_utils.GetNodeAnnotation(\n      node, pytree_utils.Annotation.SPLIT_PENALTY, default=amt)\n  penalty = penalty - amt if amt < penalty else 0\n  _SetSplitPenalty(node, penalty)\n\n\ndef _SetSplitPenalty(node, penalty):\n  pytree_utils.SetNodeAnnotation(node, pytree_utils.Annotation.SPLIT_PENALTY,\n                                 penalty)\n"
  },
  {
    "path": "yapf/pytree/subtype_assigner.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Subtype assigner for format tokens.\n\nThis module assigns extra type information to format tokens. This information is\nmore specific than whether something is an operator or an identifier. For\ninstance, it can specify if a node in the tree is part of a subscript.\n\n  AssignSubtypes(): the main function exported by this module.\n\nAnnotations:\n  subtype: The subtype of a pytree token. See 'subtypes' module for a list of\n      subtypes.\n\"\"\"\n\nfrom yapf_third_party._ylib2to3 import pytree\nfrom yapf_third_party._ylib2to3.pgen2 import token as grammar_token\nfrom yapf_third_party._ylib2to3.pygram import python_symbols as syms\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.pytree import pytree_visitor\nfrom yapf.yapflib import style\nfrom yapf.yapflib import subtypes\n\n\ndef AssignSubtypes(tree):\n  \"\"\"Run the subtype assigner visitor over the tree, modifying it in place.\n\n  Arguments:\n    tree: the top-level pytree node to annotate with subtypes.\n  \"\"\"\n  subtype_assigner = _SubtypeAssigner()\n  subtype_assigner.Visit(tree)\n\n\n# Map tokens in argument lists to their respective subtype.\n_ARGLIST_TOKEN_TO_SUBTYPE = {\n    '=': subtypes.DEFAULT_OR_NAMED_ASSIGN,\n    ':': subtypes.TYPED_NAME,\n    '*': subtypes.VARARGS_STAR,\n    '**': subtypes.KWARGS_STAR_STAR,\n}\n\n\nclass _SubtypeAssigner(pytree_visitor.PyTreeVisitor):\n  \"\"\"_SubtypeAssigner - see file-level docstring for detailed description.\n\n  The subtype is added as an annotation to the pytree token.\n  \"\"\"\n\n  def Visit_dictsetmaker(self, node):  # pylint: disable=invalid-name\n    # dictsetmaker ::= (test ':' test (comp_for |\n    #                                   (',' test ':' test)* [','])) |\n    #                  (test (comp_for | (',' test)* [',']))\n    for child in node.children:\n      self.Visit(child)\n\n    dict_maker = False\n\n    def markAsDictSetGenerator(node):\n      _AppendFirstLeafTokenSubtype(node, subtypes.DICT_SET_GENERATOR)\n      for child in node.children:\n        if pytree_utils.NodeName(child) == 'comp_for':\n          markAsDictSetGenerator(child)\n\n    for child in node.children:\n      if pytree_utils.NodeName(child) == 'comp_for':\n        markAsDictSetGenerator(child)\n      elif child.type in (grammar_token.COLON, grammar_token.DOUBLESTAR):\n        dict_maker = True\n\n    if dict_maker:\n      last_was_colon = False\n      unpacking = False\n      for child in node.children:\n        if pytree_utils.NodeName(child) == 'comp_for':\n          break\n        if child.type == grammar_token.DOUBLESTAR:\n          _AppendFirstLeafTokenSubtype(child, subtypes.KWARGS_STAR_STAR)\n        if last_was_colon:\n          if style.Get('INDENT_DICTIONARY_VALUE'):\n            _InsertPseudoParentheses(child)\n          else:\n            _AppendFirstLeafTokenSubtype(child, subtypes.DICTIONARY_VALUE)\n        elif (isinstance(child, pytree.Node) or\n              (not child.value.startswith('#') and child.value not in '{:,')):\n          # Mark the first leaf of a key entry as a DICTIONARY_KEY. We\n          # normally want to split before them if the dictionary cannot exist\n          # on a single line.\n          if not unpacking or pytree_utils.FirstLeafNode(child).value == '**':\n            _AppendFirstLeafTokenSubtype(child, subtypes.DICTIONARY_KEY)\n          _AppendSubtypeRec(child, subtypes.DICTIONARY_KEY_PART)\n        last_was_colon = child.type == grammar_token.COLON\n        if child.type == grammar_token.DOUBLESTAR:\n          unpacking = True\n        elif last_was_colon:\n          unpacking = False\n\n  def Visit_expr_stmt(self, node):  # pylint: disable=invalid-name\n    # expr_stmt ::= testlist_star_expr (augassign (yield_expr|testlist)\n    #               | ('=' (yield_expr|testlist_star_expr))*)\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == '=':\n        _AppendTokenSubtype(child, subtypes.ASSIGN_OPERATOR)\n\n  def Visit_or_test(self, node):  # pylint: disable=invalid-name\n    # or_test ::= and_test ('or' and_test)*\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == 'or':\n        _AppendTokenSubtype(child, subtypes.BINARY_OPERATOR)\n\n  def Visit_and_test(self, node):  # pylint: disable=invalid-name\n    # and_test ::= not_test ('and' not_test)*\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == 'and':\n        _AppendTokenSubtype(child, subtypes.BINARY_OPERATOR)\n\n  def Visit_not_test(self, node):  # pylint: disable=invalid-name\n    # not_test ::= 'not' not_test | comparison\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == 'not':\n        _AppendTokenSubtype(child, subtypes.UNARY_OPERATOR)\n\n  def Visit_comparison(self, node):  # pylint: disable=invalid-name\n    # comparison ::= expr (comp_op expr)*\n    # comp_op ::= '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not in'|'is'|'is not'\n    for child in node.children:\n      self.Visit(child)\n      if (isinstance(child, pytree.Leaf) and\n          child.value in {'<', '>', '==', '>=', '<=', '<>', '!=', 'in', 'is'}):\n        _AppendTokenSubtype(child, subtypes.BINARY_OPERATOR)\n      elif pytree_utils.NodeName(child) == 'comp_op':\n        for grandchild in child.children:\n          _AppendTokenSubtype(grandchild, subtypes.BINARY_OPERATOR)\n\n  def Visit_star_expr(self, node):  # pylint: disable=invalid-name\n    # star_expr ::= '*' expr\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == '*':\n        _AppendTokenSubtype(child, subtypes.UNARY_OPERATOR)\n        _AppendTokenSubtype(child, subtypes.VARARGS_STAR)\n\n  def Visit_expr(self, node):  # pylint: disable=invalid-name\n    # expr ::= xor_expr ('|' xor_expr)*\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == '|':\n        _AppendTokenSubtype(child, subtypes.BINARY_OPERATOR)\n\n  def Visit_xor_expr(self, node):  # pylint: disable=invalid-name\n    # xor_expr ::= and_expr ('^' and_expr)*\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == '^':\n        _AppendTokenSubtype(child, subtypes.BINARY_OPERATOR)\n\n  def Visit_and_expr(self, node):  # pylint: disable=invalid-name\n    # and_expr ::= shift_expr ('&' shift_expr)*\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == '&':\n        _AppendTokenSubtype(child, subtypes.BINARY_OPERATOR)\n\n  def Visit_shift_expr(self, node):  # pylint: disable=invalid-name\n    # shift_expr ::= arith_expr (('<<'|'>>') arith_expr)*\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value in {'<<', '>>'}:\n        _AppendTokenSubtype(child, subtypes.BINARY_OPERATOR)\n\n  def Visit_arith_expr(self, node):  # pylint: disable=invalid-name\n    # arith_expr ::= term (('+'|'-') term)*\n    for child in node.children:\n      self.Visit(child)\n      if _IsAExprOperator(child):\n        _AppendTokenSubtype(child, subtypes.BINARY_OPERATOR)\n\n    if _IsSimpleExpression(node):\n      for child in node.children:\n        if _IsAExprOperator(child):\n          _AppendTokenSubtype(child, subtypes.SIMPLE_EXPRESSION)\n\n  def Visit_term(self, node):  # pylint: disable=invalid-name\n    # term ::= factor (('*'|'/'|'%'|'//'|'@') factor)*\n    for child in node.children:\n      self.Visit(child)\n      if _IsMExprOperator(child):\n        _AppendTokenSubtype(child, subtypes.BINARY_OPERATOR)\n\n    if _IsSimpleExpression(node):\n      for child in node.children:\n        if _IsMExprOperator(child):\n          _AppendTokenSubtype(child, subtypes.SIMPLE_EXPRESSION)\n\n  def Visit_factor(self, node):  # pylint: disable=invalid-name\n    # factor ::= ('+'|'-'|'~') factor | power\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value in '+-~':\n        _AppendTokenSubtype(child, subtypes.UNARY_OPERATOR)\n\n  def Visit_power(self, node):  # pylint: disable=invalid-name\n    # power ::= atom trailer* ['**' factor]\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == '**':\n        _AppendTokenSubtype(child, subtypes.BINARY_OPERATOR)\n\n  def Visit_lambdef(self, node):  # pylint: disable=invalid-name\n    # trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME\n    _AppendSubtypeRec(node, subtypes.LAMBDEF)\n    self.DefaultNodeVisit(node)\n\n  def Visit_trailer(self, node):  # pylint: disable=invalid-name\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value in '[]':\n        _AppendTokenSubtype(child, subtypes.SUBSCRIPT_BRACKET)\n\n  def Visit_subscript(self, node):  # pylint: disable=invalid-name\n    # subscript ::= test | [test] ':' [test] [sliceop]\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == ':':\n        _AppendTokenSubtype(child, subtypes.SUBSCRIPT_COLON)\n\n  def Visit_sliceop(self, node):  # pylint: disable=invalid-name\n    # sliceop ::= ':' [test]\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == ':':\n        _AppendTokenSubtype(child, subtypes.SUBSCRIPT_COLON)\n\n  def Visit_argument(self, node):  # pylint: disable=invalid-name\n    # argument ::=\n    #     test [comp_for] | test '=' test\n    self._ProcessArgLists(node)\n\n  def Visit_arglist(self, node):  # pylint: disable=invalid-name\n    # arglist ::=\n    #     (argument ',')* (argument [',']\n    #                     | '*' test (',' argument)* [',' '**' test]\n    #                     | '**' test)\n    self._ProcessArgLists(node)\n    _SetArgListSubtype(node, subtypes.DEFAULT_OR_NAMED_ASSIGN,\n                       subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST)\n\n  def Visit_tname(self, node):  # pylint: disable=invalid-name\n    self._ProcessArgLists(node)\n    _SetArgListSubtype(node, subtypes.DEFAULT_OR_NAMED_ASSIGN,\n                       subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST)\n\n  def Visit_decorator(self, node):  # pylint: disable=invalid-name\n    # decorator ::=\n    #     '@' dotted_name [ '(' [arglist] ')' ] NEWLINE\n    for child in node.children:\n      if isinstance(child, pytree.Leaf) and child.value == '@':\n        _AppendTokenSubtype(child, subtype=subtypes.DECORATOR)\n      self.Visit(child)\n\n  def Visit_funcdef(self, node):  # pylint: disable=invalid-name\n    # funcdef ::=\n    #     'def' NAME parameters ['->' test] ':' suite\n    for child in node.children:\n      if child.type == grammar_token.NAME and child.value != 'def':\n        _AppendTokenSubtype(child, subtypes.FUNC_DEF)\n        break\n    for child in node.children:\n      self.Visit(child)\n\n  def Visit_parameters(self, node):  # pylint: disable=invalid-name\n    # parameters ::= '(' [typedargslist] ')'\n    self._ProcessArgLists(node)\n    if len(node.children) > 2:\n      _AppendFirstLeafTokenSubtype(node.children[1], subtypes.PARAMETER_START)\n      _AppendLastLeafTokenSubtype(node.children[-2], subtypes.PARAMETER_STOP)\n\n  def Visit_typedargslist(self, node):  # pylint: disable=invalid-name\n    # typedargslist ::=\n    #     ((tfpdef ['=' test] ',')*\n    #          ('*' [tname] (',' tname ['=' test])* [',' '**' tname]\n    #           | '**' tname)\n    #     | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])\n    self._ProcessArgLists(node)\n    _SetArgListSubtype(node, subtypes.DEFAULT_OR_NAMED_ASSIGN,\n                       subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST)\n    tname = False\n    if not node.children:\n      return\n\n    _AppendFirstLeafTokenSubtype(node.children[0], subtypes.PARAMETER_START)\n    _AppendLastLeafTokenSubtype(node.children[-1], subtypes.PARAMETER_STOP)\n\n    tname = pytree_utils.NodeName(node.children[0]) == 'tname'\n    for i in range(1, len(node.children)):\n      prev_child = node.children[i - 1]\n      child = node.children[i]\n      if prev_child.type == grammar_token.COMMA:\n        _AppendFirstLeafTokenSubtype(child, subtypes.PARAMETER_START)\n      elif child.type == grammar_token.COMMA:\n        _AppendLastLeafTokenSubtype(prev_child, subtypes.PARAMETER_STOP)\n\n      if pytree_utils.NodeName(child) == 'tname':\n        tname = True\n        _SetArgListSubtype(child, subtypes.TYPED_NAME,\n                           subtypes.TYPED_NAME_ARG_LIST)\n      elif child.type == grammar_token.COMMA:\n        tname = False\n      elif child.type == grammar_token.EQUAL and tname:\n        _AppendTokenSubtype(child, subtype=subtypes.TYPED_NAME)\n        tname = False\n\n  def Visit_varargslist(self, node):  # pylint: disable=invalid-name\n    # varargslist ::=\n    #     ((vfpdef ['=' test] ',')*\n    #          ('*' [vname] (',' vname ['=' test])*  [',' '**' vname]\n    #           | '**' vname)\n    #      | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])\n    self._ProcessArgLists(node)\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf) and child.value == '=':\n        _AppendTokenSubtype(child, subtypes.VARARGS_LIST)\n\n  def Visit_comp_for(self, node):  # pylint: disable=invalid-name\n    # comp_for ::= 'for' exprlist 'in' testlist_safe [comp_iter]\n    _AppendSubtypeRec(node, subtypes.COMP_FOR)\n    # Mark the previous node as COMP_EXPR unless this is a nested comprehension\n    # as these will have the outer comprehension as their previous node.\n    attr = pytree_utils.GetNodeAnnotation(node.parent,\n                                          pytree_utils.Annotation.SUBTYPE)\n    if not attr or subtypes.COMP_FOR not in attr:\n      sibling = node.prev_sibling\n      while sibling:\n        _AppendSubtypeRec(sibling, subtypes.COMP_EXPR)\n        sibling = sibling.prev_sibling\n    self.DefaultNodeVisit(node)\n\n  def Visit_old_comp_for(self, node):  # pylint: disable=invalid-name\n    # Python 3.7\n    self.Visit_comp_for(node)\n\n  def Visit_comp_if(self, node):  # pylint: disable=invalid-name\n    # comp_if ::= 'if' old_test [comp_iter]\n    _AppendSubtypeRec(node, subtypes.COMP_IF)\n    self.DefaultNodeVisit(node)\n\n  def Visit_old_comp_if(self, node):  # pylint: disable=invalid-name\n    # Python 3.7\n    self.Visit_comp_if(node)\n\n  def _ProcessArgLists(self, node):\n    \"\"\"Common method for processing argument lists.\"\"\"\n    for child in node.children:\n      self.Visit(child)\n      if isinstance(child, pytree.Leaf):\n        _AppendTokenSubtype(\n            child,\n            subtype=_ARGLIST_TOKEN_TO_SUBTYPE.get(child.value, subtypes.NONE))\n\n\ndef _SetArgListSubtype(node, node_subtype, list_subtype):\n  \"\"\"Set named assign subtype on elements in a arg list.\"\"\"\n\n  def HasSubtype(node):\n    \"\"\"Return True if the arg list has a named assign subtype.\"\"\"\n    if isinstance(node, pytree.Leaf):\n      return node_subtype in pytree_utils.GetNodeAnnotation(\n          node, pytree_utils.Annotation.SUBTYPE, set())\n\n    for child in node.children:\n      node_name = pytree_utils.NodeName(child)\n      if node_name not in {'atom', 'arglist', 'power'}:\n        if HasSubtype(child):\n          return True\n\n    return False\n\n  if not HasSubtype(node):\n    return\n\n  for child in node.children:\n    node_name = pytree_utils.NodeName(child)\n    if node_name not in {'atom', 'COMMA'}:\n      _AppendFirstLeafTokenSubtype(child, list_subtype)\n\n\ndef _AppendTokenSubtype(node, subtype):\n  \"\"\"Append the token's subtype only if it's not already set.\"\"\"\n  pytree_utils.AppendNodeAnnotation(node, pytree_utils.Annotation.SUBTYPE,\n                                    subtype)\n\n\ndef _AppendFirstLeafTokenSubtype(node, subtype):\n  \"\"\"Append the first leaf token's subtypes.\"\"\"\n  if isinstance(node, pytree.Leaf):\n    _AppendTokenSubtype(node, subtype)\n    return\n  _AppendFirstLeafTokenSubtype(node.children[0], subtype)\n\n\ndef _AppendLastLeafTokenSubtype(node, subtype):\n  \"\"\"Append the last leaf token's subtypes.\"\"\"\n  if isinstance(node, pytree.Leaf):\n    _AppendTokenSubtype(node, subtype)\n    return\n  _AppendLastLeafTokenSubtype(node.children[-1], subtype)\n\n\ndef _AppendSubtypeRec(node, subtype, force=True):\n  \"\"\"Append the leafs in the node to the given subtype.\"\"\"\n  if isinstance(node, pytree.Leaf):\n    _AppendTokenSubtype(node, subtype)\n    return\n  for child in node.children:\n    _AppendSubtypeRec(child, subtype, force=force)\n\n\ndef _InsertPseudoParentheses(node):\n  \"\"\"Insert pseudo parentheses so that dicts can be formatted correctly.\"\"\"\n  comment_node = None\n  if isinstance(node, pytree.Node):\n    if node.children[-1].type == grammar_token.COMMENT:\n      comment_node = node.children[-1].clone()\n      node.children[-1].remove()\n\n  first = pytree_utils.FirstLeafNode(node)\n  last = pytree_utils.LastLeafNode(node)\n\n  if first == last and first.type == grammar_token.COMMENT:\n    # A comment was inserted before the value, which is a pytree.Leaf.\n    # Encompass the dictionary's value into an ATOM node.\n    last = first.next_sibling\n    last_clone = last.clone()\n    new_node = pytree.Node(syms.atom, [first.clone(), last_clone])\n    for orig_leaf, clone_leaf in zip(last.leaves(), last_clone.leaves()):\n      pytree_utils.CopyYapfAnnotations(orig_leaf, clone_leaf)\n      if hasattr(orig_leaf, 'is_pseudo'):\n        clone_leaf.is_pseudo = orig_leaf.is_pseudo\n\n    node.replace(new_node)\n    node = new_node\n    last.remove()\n\n    first = pytree_utils.FirstLeafNode(node)\n    last = pytree_utils.LastLeafNode(node)\n\n  lparen = pytree.Leaf(\n      grammar_token.LPAR,\n      '(',\n      context=('', (first.get_lineno(), first.column - 1)))\n  last_lineno = last.get_lineno()\n  if last.type == grammar_token.STRING and '\\n' in last.value:\n    last_lineno += last.value.count('\\n')\n\n  if last.type == grammar_token.STRING and '\\n' in last.value:\n    last_column = len(last.value.split('\\n')[-1]) + 1\n  else:\n    last_column = last.column + len(last.value) + 1\n  rparen = pytree.Leaf(\n      grammar_token.RPAR, ')', context=('', (last_lineno, last_column)))\n\n  lparen.is_pseudo = True\n  rparen.is_pseudo = True\n\n  if isinstance(node, pytree.Node):\n    node.insert_child(0, lparen)\n    node.append_child(rparen)\n    if comment_node:\n      node.append_child(comment_node)\n    _AppendFirstLeafTokenSubtype(node, subtypes.DICTIONARY_VALUE)\n  else:\n    clone = node.clone()\n    for orig_leaf, clone_leaf in zip(node.leaves(), clone.leaves()):\n      pytree_utils.CopyYapfAnnotations(orig_leaf, clone_leaf)\n    new_node = pytree.Node(syms.atom, [lparen, clone, rparen])\n    node.replace(new_node)\n    _AppendFirstLeafTokenSubtype(clone, subtypes.DICTIONARY_VALUE)\n\n\ndef _IsAExprOperator(node):\n  return isinstance(node, pytree.Leaf) and node.value in {'+', '-'}\n\n\ndef _IsMExprOperator(node):\n  return isinstance(node,\n                    pytree.Leaf) and node.value in {'*', '/', '%', '//', '@'}\n\n\ndef _IsSimpleExpression(node):\n  \"\"\"A node with only leafs as children.\"\"\"\n  return all(isinstance(child, pytree.Leaf) for child in node.children)\n"
  },
  {
    "path": "yapf/yapflib/__init__.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n"
  },
  {
    "path": "yapf/yapflib/errors.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"YAPF error objects.\"\"\"\n\nfrom yapf_third_party._ylib2to3.pgen2 import tokenize\n\n\ndef FormatErrorMsg(e):\n  \"\"\"Convert an exception into a standard format.\n\n  The standard error message format is:\n\n      <filename>:<lineno>:<column>: <msg>\n\n  Arguments:\n    e: An exception.\n\n  Returns:\n    A properly formatted error message string.\n  \"\"\"\n  if isinstance(e, SyntaxError):\n    return '{}:{}:{}: {}'.format(e.filename, e.lineno, e.offset, e.msg)\n  if isinstance(e, tokenize.TokenError):\n    return '{}:{}:{}: {}'.format(e.filename, e.args[1][0], e.args[1][1],\n                                 e.args[0])\n  return '{}:{}:{}: {}'.format(e.args[1][0], e.args[1][1], e.args[1][2], e.msg)\n\n\nclass YapfError(Exception):\n  \"\"\"Parent class for user errors or input errors.\n\n  Exceptions of this type are handled by the command line tool\n  and result in clear error messages, as opposed to backtraces.\n  \"\"\"\n  pass\n"
  },
  {
    "path": "yapf/yapflib/file_resources.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Interface to file resources.\n\nThis module provides functions for interfacing with files: opening, writing, and\nquerying.\n\"\"\"\n\nimport codecs\nimport fnmatch\nimport os\nimport re\nimport sys\nfrom configparser import ConfigParser\nfrom tokenize import detect_encoding\n\nfrom yapf.yapflib import errors\nfrom yapf.yapflib import style\n\nif sys.version_info >= (3, 11):\n  import tomllib\nelse:\n  import tomli as tomllib\n\nCR = '\\r'\nLF = '\\n'\nCRLF = '\\r\\n'\n\n\ndef _GetExcludePatternsFromYapfIgnore(filename):\n  \"\"\"Get a list of file patterns to ignore from .yapfignore.\"\"\"\n  ignore_patterns = []\n  if os.path.isfile(filename) and os.access(filename, os.R_OK):\n    with open(filename, 'r') as fd:\n      for line in fd:\n        if line.strip() and not line.startswith('#'):\n          ignore_patterns.append(line.strip())\n\n    if any(e.startswith('./') for e in ignore_patterns):\n      raise errors.YapfError('path in .yapfignore should not start with ./')\n\n  return ignore_patterns\n\n\ndef _GetExcludePatternsFromPyprojectToml(filename):\n  \"\"\"Get a list of file patterns to ignore from pyproject.toml.\"\"\"\n  ignore_patterns = []\n\n  if os.path.isfile(filename) and os.access(filename, os.R_OK):\n    with open(filename, 'rb') as fd:\n      pyproject_toml = tomllib.load(fd)\n    ignore_patterns = pyproject_toml.get('tool',\n                                         {}).get('yapfignore',\n                                                 {}).get('ignore_patterns', [])\n    if any(e.startswith('./') for e in ignore_patterns):\n      raise errors.YapfError('path in pyproject.toml should not start with ./')\n\n  return ignore_patterns\n\n\ndef GetExcludePatternsForDir(dirname):\n  \"\"\"Return patterns of files to exclude from ignorefile in a given directory.\n\n  Looks for .yapfignore in the directory dirname.\n\n  Arguments:\n    dirname: (unicode) The name of the directory.\n\n  Returns:\n    A List of file patterns to exclude if ignore file is found, otherwise empty\n    List.\n  \"\"\"\n  ignore_patterns = []\n\n  yapfignore_file = os.path.join(dirname, '.yapfignore')\n  if os.path.exists(yapfignore_file):\n    ignore_patterns += _GetExcludePatternsFromYapfIgnore(yapfignore_file)\n\n  pyproject_toml_file = os.path.join(dirname, 'pyproject.toml')\n  if os.path.exists(pyproject_toml_file):\n    ignore_patterns += _GetExcludePatternsFromPyprojectToml(pyproject_toml_file)\n  return ignore_patterns\n\n\ndef GetDefaultStyleForDir(dirname, default_style=style.DEFAULT_STYLE):\n  \"\"\"Return default style name for a given directory.\n\n  Looks for .style.yapf or setup.cfg or pyproject.toml in the parent\n  directories.\n\n  Arguments:\n    dirname: (unicode) The name of the directory.\n    default_style: The style to return if nothing is found. Defaults to the\n                   global default style ('pep8') unless otherwise specified.\n\n  Returns:\n    The filename if found, otherwise return the default style.\n  \"\"\"\n  dirname = os.path.abspath(dirname)\n  while True:\n    # See if we have a .style.yapf file.\n    style_file = os.path.join(dirname, style.LOCAL_STYLE)\n    if os.path.exists(style_file):\n      return style_file\n\n    # See if we have a setup.cfg file with a '[yapf]' section.\n    config_file = os.path.join(dirname, style.SETUP_CONFIG)\n    try:\n      fd = open(config_file)\n    except IOError:\n      pass  # It's okay if it's not there.\n    else:\n      with fd:\n        config = ConfigParser()\n        config.read_file(fd)\n        if config.has_section('yapf'):\n          return config_file\n\n    # See if we have a pyproject.toml file with a '[tool.yapf]'  section.\n    config_file = os.path.join(dirname, style.PYPROJECT_TOML)\n    try:\n      fd = open(config_file, 'rb')\n    except IOError:\n      pass  # It's okay if it's not there.\n    else:\n      with fd:\n        pyproject_toml = tomllib.load(fd)\n        style_dict = pyproject_toml.get('tool', {}).get('yapf', None)\n        if style_dict is not None:\n          return config_file\n\n    if (not dirname or not os.path.basename(dirname) or\n        dirname == os.path.abspath(os.path.sep)):\n      break\n    dirname = os.path.dirname(dirname)\n\n  global_file = os.path.expanduser(style.GLOBAL_STYLE)\n  if os.path.exists(global_file):\n    return global_file\n\n  return default_style\n\n\ndef GetCommandLineFiles(command_line_file_list, recursive, exclude):\n  \"\"\"Return the list of files specified on the command line.\"\"\"\n  return _FindPythonFiles(command_line_file_list, recursive, exclude)\n\n\ndef WriteReformattedCode(filename,\n                         reformatted_code,\n                         encoding='',\n                         in_place=False):\n  \"\"\"Emit the reformatted code.\n\n  Write the reformatted code into the file, if in_place is True. Otherwise,\n  write to stdout.\n\n  Arguments:\n    filename: (unicode) The name of the unformatted file.\n    reformatted_code: (unicode) The reformatted code.\n    encoding: (unicode) The encoding of the file.\n    in_place: (bool) If True, then write the reformatted code to the file.\n  \"\"\"\n  if in_place:\n    with codecs.open(filename, mode='w', encoding=encoding) as fd:\n      fd.write(reformatted_code)\n  else:\n    sys.stdout.buffer.write(reformatted_code.encode('utf-8'))\n\n\ndef LineEnding(lines):\n  \"\"\"Retrieve the line ending of the original source.\"\"\"\n  endings = {CRLF: 0, CR: 0, LF: 0}\n  for line in lines:\n    if line.endswith(CRLF):\n      endings[CRLF] += 1\n    elif line.endswith(CR):\n      endings[CR] += 1\n    elif line.endswith(LF):\n      endings[LF] += 1\n  return sorted((LF, CRLF, CR), key=endings.get, reverse=True)[0]\n\n\ndef _FindPythonFiles(filenames, recursive, exclude):\n  \"\"\"Find all Python files.\"\"\"\n  if exclude and any(e.startswith('./') for e in exclude):\n    raise errors.YapfError(\"path in '--exclude' should not start with ./\")\n  exclude = exclude and [e.rstrip('/' + os.path.sep) for e in exclude]\n\n  python_files = []\n  for filename in filenames:\n    if filename != '.' and exclude and IsIgnored(filename, exclude):\n      continue\n    if os.path.isdir(filename):\n      if not recursive:\n        raise errors.YapfError(\n            \"directory specified without '--recursive' flag: %s\" % filename)\n\n      # TODO(morbo): Look into a version of os.walk that can handle recursion.\n      excluded_dirs = []\n      for dirpath, dirnames, filelist in os.walk(filename):\n        if dirpath != '.' and exclude and IsIgnored(dirpath, exclude):\n          excluded_dirs.append(dirpath)\n          continue\n        elif any(dirpath.startswith(e) for e in excluded_dirs):\n          continue\n        for f in filelist:\n          filepath = os.path.join(dirpath, f)\n          if exclude and IsIgnored(filepath, exclude):\n            continue\n          if IsPythonFile(filepath):\n            python_files.append(filepath)\n        # To prevent it from scanning the contents excluded folders, os.walk()\n        # lets you amend its list of child dirs `dirnames`. These edits must be\n        # made in-place instead of creating a modified copy of `dirnames`.\n        # list.remove() is slow and list.pop() is a headache. Instead clear\n        # `dirnames` then repopulate it.\n        dirnames_ = [dirnames.pop(0) for i in range(len(dirnames))]\n        for dirname in dirnames_:\n          dir_ = os.path.join(dirpath, dirname)\n          if IsIgnored(dir_, exclude):\n            excluded_dirs.append(dir_)\n          else:\n            dirnames.append(dirname)\n\n    elif os.path.isfile(filename):\n      python_files.append(filename)\n\n  return python_files\n\n\ndef IsIgnored(path, exclude):\n  \"\"\"Return True if filename matches any patterns in exclude.\"\"\"\n  if exclude is None:\n    return False\n  path = path.lstrip(os.path.sep)\n  while path.startswith('.' + os.path.sep):\n    path = path[2:]\n  return any(fnmatch.fnmatch(path, e.rstrip(os.path.sep)) for e in exclude)\n\n\ndef IsPythonFile(filename):\n  \"\"\"Return True if filename is a Python file.\"\"\"\n  if os.path.splitext(filename)[1] in frozenset({'.py', '.pyi'}):\n    return True\n\n  try:\n    with open(filename, 'rb') as fd:\n      encoding = detect_encoding(fd.readline)[0]\n\n    # Check for correctness of encoding.\n    with codecs.open(filename, mode='r', encoding=encoding) as fd:\n      fd.read()\n  except UnicodeDecodeError:\n    encoding = 'latin-1'\n  except (IOError, SyntaxError):\n    # If we fail to detect encoding (or the encoding cookie is incorrect - which\n    # will make detect_encoding raise SyntaxError), assume it's not a Python\n    # file.\n    return False\n\n  try:\n    with codecs.open(filename, mode='r', encoding=encoding) as fd:\n      first_line = fd.readline(256)\n  except IOError:\n    return False\n\n  return re.match(r'^#!.*\\bpython[23]?\\b', first_line)\n\n\ndef FileEncoding(filename):\n  \"\"\"Return the file's encoding.\"\"\"\n  with open(filename, 'rb') as fd:\n    return detect_encoding(fd.readline)[0]\n"
  },
  {
    "path": "yapf/yapflib/format_decision_state.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Implements a format decision state object that manages whitespace decisions.\n\nEach token is processed one at a time, at which point its whitespace formatting\ndecisions are made. A graph of potential whitespace formatting is created,\nwhere each node in the graph is a format decision state object. The heuristic\ntries formatting the token with and without a newline before it to determine\nwhich one has the least penalty. Therefore, the format decision state object for\neach decision needs to be its own unique copy.\n\nOnce the heuristic determines the best formatting, it makes a non-dry run pass\nthrough the code to commit the whitespace formatting.\n\n  FormatDecisionState: main class exported by this module.\n\"\"\"\n\nfrom yapf.pytree import split_penalty\nfrom yapf.pytree.pytree_utils import NodeName\nfrom yapf.yapflib import logical_line\nfrom yapf.yapflib import object_state\nfrom yapf.yapflib import style\nfrom yapf.yapflib import subtypes\n\n\nclass FormatDecisionState(object):\n  \"\"\"The current state when indenting a logical line.\n\n  The FormatDecisionState object is meant to be copied instead of referenced.\n\n  Attributes:\n    first_indent: The indent of the first token.\n    column: The number of used columns in the current line.\n    line: The logical line we're currently processing.\n    next_token: The next token to be formatted.\n    paren_level: The level of nesting inside (), [], and {}.\n    lowest_level_on_line: The lowest paren_level on the current line.\n    stack: A stack (of _ParenState) keeping track of properties applying to\n      parenthesis levels.\n    comp_stack: A stack (of ComprehensionState) keeping track of properties\n      applying to comprehensions.\n    param_list_stack: A stack (of ParameterListState) keeping track of\n      properties applying to function parameter lists.\n    ignore_stack_for_comparison: Ignore the stack of _ParenState for state\n      comparison.\n    column_limit: The column limit specified by the style.\n  \"\"\"\n\n  def __init__(self, line, first_indent):\n    \"\"\"Initializer.\n\n    Initializes to the state after placing the first token from 'line' at\n    'first_indent'.\n\n    Arguments:\n      line: (LogicalLine) The logical line we're currently processing.\n      first_indent: (int) The indent of the first token.\n    \"\"\"\n    self.next_token = line.first\n    self.column = first_indent\n    self.line = line\n    self.paren_level = 0\n    self.lowest_level_on_line = 0\n    self.ignore_stack_for_comparison = False\n    self.stack = [_ParenState(first_indent, first_indent)]\n    self.comp_stack = []\n    self.param_list_stack = []\n    self.first_indent = first_indent\n    self.column_limit = style.Get('COLUMN_LIMIT')\n\n  def Clone(self):\n    \"\"\"Clones a FormatDecisionState object.\"\"\"\n    new = FormatDecisionState(self.line, self.first_indent)\n    new.next_token = self.next_token\n    new.column = self.column\n    new.line = self.line\n    new.paren_level = self.paren_level\n    new.line.depth = self.line.depth\n    new.lowest_level_on_line = self.lowest_level_on_line\n    new.ignore_stack_for_comparison = self.ignore_stack_for_comparison\n    new.first_indent = self.first_indent\n    new.stack = [state.Clone() for state in self.stack]\n    new.comp_stack = [state.Clone() for state in self.comp_stack]\n    new.param_list_stack = [state.Clone() for state in self.param_list_stack]\n    return new\n\n  def __eq__(self, other):\n    # Note: 'first_indent' is implicit in the stack. Also, we ignore 'previous',\n    # because it shouldn't have a bearing on this comparison. (I.e., it will\n    # report equal if 'next_token' does.)\n    return (self.next_token == other.next_token and\n            self.column == other.column and\n            self.paren_level == other.paren_level and\n            self.line.depth == other.line.depth and\n            self.lowest_level_on_line == other.lowest_level_on_line and\n            (self.ignore_stack_for_comparison or\n             other.ignore_stack_for_comparison or self.stack == other.stack and\n             self.comp_stack == other.comp_stack and\n             self.param_list_stack == other.param_list_stack))\n\n  def __ne__(self, other):\n    return not self == other\n\n  def __hash__(self):\n    return hash((self.next_token, self.column, self.paren_level,\n                 self.line.depth, self.lowest_level_on_line))\n\n  def __repr__(self):\n    return ('column::%d, next_token::%s, paren_level::%d, stack::[\\n\\t%s' %\n            (self.column, repr(self.next_token), self.paren_level,\n             '\\n\\t'.join(repr(s) for s in self.stack) + ']'))\n\n  def CanSplit(self, must_split):\n    \"\"\"Determine if we can split before the next token.\n\n    Arguments:\n      must_split: (bool) A newline was required before this token.\n\n    Returns:\n      True if the line can be split before the next token.\n    \"\"\"\n    current = self.next_token\n    previous = current.previous_token\n\n    if current.is_pseudo:\n      return False\n\n    if (not must_split and subtypes.DICTIONARY_KEY_PART in current.subtypes and\n        subtypes.DICTIONARY_KEY not in current.subtypes and\n        not style.Get('ALLOW_MULTILINE_DICTIONARY_KEYS')):\n      # In some situations, a dictionary may be multiline, but pylint doesn't\n      # like it. So don't allow it unless forced to.\n      return False\n\n    if (not must_split and subtypes.DICTIONARY_VALUE in current.subtypes and\n        not style.Get('ALLOW_SPLIT_BEFORE_DICT_VALUE')):\n      return False\n\n    if previous and previous.value == '(' and current.value == ')':\n      # Don't split an empty function call list if we aren't splitting before\n      # dict values.\n      token = previous.previous_token\n      while token:\n        prev = token.previous_token\n        if not prev or prev.name not in {'NAME', 'DOT'}:\n          break\n        token = token.previous_token\n      if token and subtypes.DICTIONARY_VALUE in token.subtypes:\n        if not style.Get('ALLOW_SPLIT_BEFORE_DICT_VALUE'):\n          return False\n\n    if previous and previous.value == '.' and current.value == '.':\n      return False\n\n    return current.can_break_before\n\n  def MustSplit(self):\n    \"\"\"Returns True if the line must split before the next token.\"\"\"\n    current = self.next_token\n    previous = current.previous_token\n\n    if current.is_pseudo:\n      return False\n\n    if current.must_break_before:\n      return True\n\n    if not previous:\n      return False\n\n    if style.Get('SPLIT_ALL_COMMA_SEPARATED_VALUES') and previous.value == ',':\n      if (subtypes.COMP_FOR in current.subtypes or\n          subtypes.LAMBDEF in current.subtypes):\n        return False\n\n      return True\n\n    if (style.Get('FORCE_MULTILINE_DICT') and\n        subtypes.DICTIONARY_KEY in current.subtypes and not current.is_comment):\n      return True\n\n    if (style.Get('SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES') and\n        previous.value == ','):\n\n      if (subtypes.COMP_FOR in current.subtypes or\n          subtypes.LAMBDEF in current.subtypes):\n        return False\n\n      # Avoid breaking in a container that fits in the current line if possible\n      opening = _GetOpeningBracket(current)\n\n      # Can't find opening bracket, behave the same way as\n      # SPLIT_ALL_COMMA_SEPARATED_VALUES.\n      if not opening:\n        return True\n\n      if current.is_comment:\n        # Don't require splitting before a comment, since it may be related to\n        # the current line.\n        return False\n\n      # Allow the fallthrough code to handle the closing bracket.\n      if current != opening.matching_bracket:\n        # If the container doesn't fit in the current line, must split\n        return not self._ContainerFitsOnStartLine(opening)\n\n    if (self.stack[-1].split_before_closing_bracket and\n        (current.value in '}]' and style.Get('SPLIT_BEFORE_CLOSING_BRACKET') or\n         current.value in '}])' and style.Get('INDENT_CLOSING_BRACKETS'))):\n      # Split before the closing bracket if we can.\n      if (subtypes.SUBSCRIPT_BRACKET not in current.subtypes or\n          (previous.value == ',' and\n           not style.Get('DISABLE_ENDING_COMMA_HEURISTIC'))):\n        return current.node_split_penalty != split_penalty.UNBREAKABLE\n\n    if (current.value == ')' and previous.value == ',' and\n        not _IsSingleElementTuple(current.matching_bracket)):\n      return True\n\n    # Prevent splitting before the first argument in compound statements\n    # with the exception of function declarations.\n    if (style.Get('SPLIT_BEFORE_FIRST_ARGUMENT') and\n        _IsCompoundStatement(self.line.first) and\n        not _IsFunctionDef(self.line.first)):\n      return False\n\n    ###########################################################################\n    # List Splitting\n    if (style.Get('DEDENT_CLOSING_BRACKETS') or\n        style.Get('INDENT_CLOSING_BRACKETS') or\n        style.Get('SPLIT_BEFORE_FIRST_ARGUMENT')):\n      bracket = current if current.ClosesScope() else previous\n      if subtypes.SUBSCRIPT_BRACKET not in bracket.subtypes:\n        if bracket.OpensScope():\n          if style.Get('COALESCE_BRACKETS'):\n            if current.OpensScope():\n              # Prefer to keep all opening brackets together.\n              return False\n\n          if (not _IsLastScopeInLine(bracket) or\n              logical_line.IsSurroundedByBrackets(bracket)):\n            last_token = bracket.matching_bracket\n          else:\n            last_token = _LastTokenInLine(bracket.matching_bracket)\n\n          if not self._FitsOnLine(bracket, last_token):\n            # Split before the first element if the whole list can't fit on a\n            # single line.\n            self.stack[-1].split_before_closing_bracket = True\n            return True\n\n        elif (style.Get('DEDENT_CLOSING_BRACKETS') or\n              style.Get('INDENT_CLOSING_BRACKETS')) and current.ClosesScope():\n          # Split before and dedent the closing bracket.\n          return self.stack[-1].split_before_closing_bracket\n\n    if (style.Get('SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN') and\n        current.is_name):\n      # An expression that's surrounded by parens gets split after the opening\n      # parenthesis.\n      def SurroundedByParens(token):\n        \"\"\"Check if it's an expression surrounded by parentheses.\"\"\"\n        while token:\n          if token.value == ',':\n            return False\n          if token.value == ')':\n            return not token.next_token\n          if token.OpensScope():\n            token = token.matching_bracket.next_token\n          else:\n            token = token.next_token\n        return False\n\n      if (previous.value == '(' and not previous.is_pseudo and\n          not logical_line.IsSurroundedByBrackets(previous)):\n        pptoken = previous.previous_token\n        if (pptoken and not pptoken.is_name and not pptoken.is_keyword and\n            SurroundedByParens(current)):\n          return True\n\n    if (current.is_name or current.is_string) and previous.value == ',':\n      # If the list has function calls in it and the full list itself cannot\n      # fit on the line, then we want to split. Otherwise, we'll get something\n      # like this:\n      #\n      #     X = [\n      #         Bar(xxx='some string',\n      #             yyy='another long string',\n      #             zzz='a third long string'), Bar(\n      #                 xxx='some string',\n      #                 yyy='another long string',\n      #                 zzz='a third long string')\n      #     ]\n      #\n      # or when a string formatting syntax.\n      func_call_or_string_format = False\n      tok = current.next_token\n      if current.is_name:\n        while tok and (tok.is_name or tok.value == '.'):\n          tok = tok.next_token\n        func_call_or_string_format = tok and tok.value == '('\n      elif current.is_string:\n        while tok and tok.is_string:\n          tok = tok.next_token\n        func_call_or_string_format = tok and tok.value == '%'\n      if func_call_or_string_format:\n        open_bracket = logical_line.IsSurroundedByBrackets(current)\n        if open_bracket:\n          if open_bracket.value in '[{':\n            if not self._FitsOnLine(open_bracket,\n                                    open_bracket.matching_bracket):\n              return True\n          elif tok.value == '(':\n            if not self._FitsOnLine(current, tok.matching_bracket):\n              return True\n\n    if (current.OpensScope() and previous.value == ',' and\n        subtypes.DICTIONARY_KEY not in current.next_token.subtypes):\n      # If we have a list of tuples, then we can get a similar look as above. If\n      # the full list cannot fit on the line, then we want a split.\n      open_bracket = logical_line.IsSurroundedByBrackets(current)\n      if (open_bracket and open_bracket.value in '[{' and\n          subtypes.SUBSCRIPT_BRACKET not in open_bracket.subtypes):\n        if not self._FitsOnLine(current, current.matching_bracket):\n          return True\n\n    ###########################################################################\n    # Dict/Set Splitting\n    if (style.Get('EACH_DICT_ENTRY_ON_SEPARATE_LINE') and\n        subtypes.DICTIONARY_KEY in current.subtypes and not current.is_comment):\n      # Place each dictionary entry onto its own line.\n      if previous.value == '{' and previous.previous_token:\n        opening = _GetOpeningBracket(previous.previous_token)\n        if (opening and opening.value == '(' and opening.previous_token and\n            opening.previous_token.is_name):\n          # This is a dictionary that's an argument to a function.\n          if (self._FitsOnLine(previous, previous.matching_bracket) and\n              previous.matching_bracket.next_token and\n              (not opening.matching_bracket.next_token or\n               opening.matching_bracket.next_token.value != '.') and\n              _ScopeHasNoCommas(previous)):\n            # Don't split before the key if:\n            #   - The dictionary fits on a line, and\n            #   - The function call isn't part of a builder-style call and\n            #   - The dictionary has one entry and no trailing comma\n            return False\n      return True\n\n    if (style.Get('SPLIT_BEFORE_DICT_SET_GENERATOR') and\n        subtypes.DICT_SET_GENERATOR in current.subtypes):\n      # Split before a dict/set generator.\n      return True\n\n    if (subtypes.DICTIONARY_VALUE in current.subtypes or\n        (previous.is_pseudo and previous.value == '(' and\n         not current.is_comment)):\n      # Split before the dictionary value if we can't fit every dictionary\n      # entry on its own line.\n      if not current.OpensScope():\n        opening = _GetOpeningBracket(current)\n        if not self._EachDictEntryFitsOnOneLine(opening):\n          return style.Get('ALLOW_SPLIT_BEFORE_DICT_VALUE')\n\n    if previous.value == '{':\n      # Split if the dict/set cannot fit on one line and ends in a comma.\n      closing = previous.matching_bracket\n      if (not self._FitsOnLine(previous, closing) and\n          closing.previous_token.value == ','):\n        self.stack[-1].split_before_closing_bracket = True\n        return True\n\n    ###########################################################################\n    # Argument List Splitting\n\n    if style.Get('SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED'):\n      # Split before arguments in a function call or definition if the\n      # arguments are terminated by a comma.\n      opening = _GetOpeningBracket(current)\n      if opening and opening.previous_token and opening.previous_token.is_name:\n        if previous.value in '(,':\n          if opening.matching_bracket.previous_token.value == ',':\n            return True\n\n    if (style.Get('SPLIT_BEFORE_NAMED_ASSIGNS') and not current.is_comment and\n        subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST in current.subtypes):\n      if (previous.value not in {'=', ':', '*', '**'} and\n          current.value not in ':=,)' and not _IsFunctionDefinition(previous)):\n        # If we're going to split the lines because of named arguments, then we\n        # want to split after the opening bracket as well. But not when this is\n        # part of a function definition.\n        if previous.value == '(':\n          # Make sure we don't split after the opening bracket if the\n          # continuation indent is greater than the opening bracket:\n          #\n          #  a(\n          #      b=1,\n          #      c=2)\n          if (self._FitsOnLine(previous, previous.matching_bracket) and\n              logical_line.IsSurroundedByBrackets(previous)):\n            # An argument to a function is a function call with named\n            # assigns.\n            return False\n\n          # Don't split if not required\n          if (not style.Get('SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN') and\n              not style.Get('SPLIT_BEFORE_FIRST_ARGUMENT')):\n            return False\n\n          column = self.column - self.stack[-1].last_space\n          return column > style.Get('CONTINUATION_INDENT_WIDTH')\n\n        opening = _GetOpeningBracket(current)\n        if opening:\n          return not self._ContainerFitsOnStartLine(opening)\n\n    if (current.value not in '{)' and previous.value == '(' and\n        self._ArgumentListHasDictionaryEntry(current)):\n      return True\n\n    if ((current.is_name or current.value in {'*', '**'}) and\n        previous.value == ','):\n      # If we have a function call within an argument list and it won't fit on\n      # the remaining line, but it will fit on a line by itself, then go ahead\n      # and split before the call.\n      opening = _GetOpeningBracket(current)\n      if (opening and opening.value == '(' and opening.previous_token and\n          (opening.previous_token.is_name or\n           opening.previous_token.value in {'*', '**'})):\n        is_func_call = False\n        opening = current\n        while opening:\n          if opening.value == '(':\n            is_func_call = True\n            break\n          if (not (opening.is_name or opening.value in {'*', '**'}) and\n              opening.value != '.'):\n            break\n          opening = opening.next_token\n\n        if is_func_call:\n          if (not self._FitsOnLine(current, opening.matching_bracket) or\n              (opening.matching_bracket.next_token and\n               opening.matching_bracket.next_token.value != ',' and\n               not opening.matching_bracket.next_token.ClosesScope())):\n            return True\n\n    pprevious = previous.previous_token\n\n    # A function call with a dictionary as its first argument may result in\n    # unreadable formatting if the dictionary spans multiple lines. The\n    # dictionary itself is formatted just fine, but the remaining arguments are\n    # indented too far:\n    #\n    #     function_call({\n    #         KEY_1: 'value one',\n    #         KEY_2: 'value two',\n    #     },\n    #                   default=False)\n    if (current.value == '{' and previous.value == '(' and pprevious and\n        pprevious.is_name):\n      dict_end = current.matching_bracket\n      next_token = dict_end.next_token\n      if next_token.value == ',' and not self._FitsOnLine(current, dict_end):\n        return True\n\n    if (current.is_name and pprevious and pprevious.is_name and\n        previous.value == '('):\n\n      if (not self._FitsOnLine(previous, previous.matching_bracket) and\n          _IsFunctionCallWithArguments(current)):\n        # There is a function call, with more than 1 argument, where the first\n        # argument is itself a function call with arguments that does not fit\n        # into the line.  In this specific case, if we split after the first\n        # argument's opening '(', then the formatting will look bad for the\n        # rest of the arguments. E.g.:\n        #\n        #     outer_function_call(inner_function_call(\n        #         inner_arg1, inner_arg2),\n        #                         outer_arg1, outer_arg2)\n        #\n        # Instead, enforce a split before that argument to keep things looking\n        # good.\n        if (style.Get('SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN') or\n            style.Get('SPLIT_BEFORE_FIRST_ARGUMENT')):\n          return True\n\n        opening = _GetOpeningBracket(current)\n        if (opening and opening.value == '(' and opening.previous_token and\n            (opening.previous_token.is_name or\n             opening.previous_token.value in {'*', '**'})):\n          is_func_call = False\n          opening = current\n          while opening:\n            if opening.value == '(':\n              is_func_call = True\n              break\n            if (not (opening.is_name or opening.value in {'*', '**'}) and\n                opening.value != '.'):\n              break\n            opening = opening.next_token\n\n          if is_func_call:\n            if (not self._FitsOnLine(current, opening.matching_bracket) or\n                (opening.matching_bracket.next_token and\n                 opening.matching_bracket.next_token.value != ',' and\n                 not opening.matching_bracket.next_token.ClosesScope())):\n              return True\n\n    if (previous.OpensScope() and not current.OpensScope() and\n        not current.is_comment and\n        subtypes.SUBSCRIPT_BRACKET not in previous.subtypes):\n      if pprevious and not pprevious.is_keyword and not pprevious.is_name:\n        # We want to split if there's a comment in the container.\n        token = current\n        while token != previous.matching_bracket:\n          if token.is_comment:\n            return True\n          token = token.next_token\n      if previous.value == '(':\n        pptoken = previous.previous_token\n        if not pptoken or not pptoken.is_name:\n          # Split after the opening of a tuple if it doesn't fit on the current\n          # line and it's not a function call.\n          if self._FitsOnLine(previous, previous.matching_bracket):\n            return False\n        elif not self._FitsOnLine(previous, previous.matching_bracket):\n          if len(previous.container_elements) == 1:\n            return False\n\n          elements = previous.container_elements + [previous.matching_bracket]\n          i = 1\n          while i < len(elements):\n            if (not elements[i - 1].OpensScope() and\n                not self._FitsOnLine(elements[i - 1], elements[i])):\n              return True\n            i += 1\n\n          if (self.column_limit - self.column) / float(self.column_limit) < 0.3:\n            # Try not to squish all of the arguments off to the right.\n            return True\n      else:\n        # Split after the opening of a container if it doesn't fit on the\n        # current line.\n        if not self._FitsOnLine(previous, previous.matching_bracket):\n          return True\n\n    ###########################################################################\n    # Original Formatting Splitting\n    # These checks rely upon the original formatting. This is in order to\n    # attempt to keep hand-written code in the same condition as it was before.\n    # However, this may cause the formatter to fail to be idempotent.\n    if (style.Get('SPLIT_BEFORE_BITWISE_OPERATOR') and current.value in '&|' and\n        previous.lineno < current.lineno):\n      # Retain the split before a bitwise operator.\n      return True\n\n    if (current.is_comment and\n        previous.lineno < current.lineno - current.value.count('\\n')):\n      # If a comment comes in the middle of a logical line (like an if\n      # conditional with comments interspersed), then we want to split if the\n      # original comments were on a separate line.\n      return True\n\n    return False\n\n  def AddTokenToState(self, newline, dry_run, must_split=False):\n    \"\"\"Add a token to the format decision state.\n\n    Allow the heuristic to try out adding the token with and without a newline.\n    Later on, the algorithm will determine which one has the lowest penalty.\n\n    Arguments:\n      newline: (bool) Add the token on a new line if True.\n      dry_run: (bool) Don't commit whitespace changes to the FormatToken if\n        True.\n      must_split: (bool) A newline was required before this token.\n\n    Returns:\n      The penalty of splitting after the current token.\n    \"\"\"\n    self._PushParameterListState(newline)\n\n    penalty = 0\n    if newline:\n      penalty = self._AddTokenOnNewline(dry_run, must_split)\n    else:\n      self._AddTokenOnCurrentLine(dry_run)\n\n    penalty += self._CalculateComprehensionState(newline)\n    penalty += self._CalculateParameterListState(newline)\n\n    return self.MoveStateToNextToken() + penalty\n\n  def _AddTokenOnCurrentLine(self, dry_run):\n    \"\"\"Puts the token on the current line.\n\n    Appends the next token to the state and updates information necessary for\n    indentation.\n\n    Arguments:\n      dry_run: (bool) Commit whitespace changes to the FormatToken if True.\n    \"\"\"\n    current = self.next_token\n    previous = current.previous_token\n\n    spaces = current.spaces_required_before\n    if isinstance(spaces, list):\n      # Don't set the value here, as we need to look at the lines near\n      # this one to determine the actual horizontal alignment value.\n      spaces = 0\n\n    if not dry_run:\n      current.AddWhitespacePrefix(newlines_before=0, spaces=spaces)\n\n    if previous.OpensScope():\n      if not current.is_comment:\n        # Align closing scopes that are on a newline with the opening scope:\n        #\n        #     foo = [a,\n        #            b,\n        #           ]\n        self.stack[-1].closing_scope_indent = self.column - 1\n        if style.Get('ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT'):\n          self.stack[-1].closing_scope_indent += 1\n        self.stack[-1].indent = self.column + spaces\n      else:\n        self.stack[-1].closing_scope_indent = (\n            self.stack[-1].indent - style.Get('CONTINUATION_INDENT_WIDTH'))\n\n    self.column += spaces\n\n  def _AddTokenOnNewline(self, dry_run, must_split):\n    \"\"\"Adds a line break and necessary indentation.\n\n    Appends the next token to the state and updates information necessary for\n    indentation.\n\n    Arguments:\n      dry_run: (bool) Don't commit whitespace changes to the FormatToken if\n        True.\n      must_split: (bool) A newline was required before this token.\n\n    Returns:\n      The split penalty for splitting after the current state.\n    \"\"\"\n    current = self.next_token\n    previous = current.previous_token\n\n    self.column = self._GetNewlineColumn()\n\n    if not dry_run:\n      indent_level = self.line.depth\n      spaces = self.column\n      if spaces:\n        spaces -= indent_level * style.Get('INDENT_WIDTH')\n      current.AddWhitespacePrefix(\n          newlines_before=1, spaces=spaces, indent_level=indent_level)\n\n    if not current.is_comment:\n      self.stack[-1].last_space = self.column\n    self.lowest_level_on_line = self.paren_level\n\n    if (previous.OpensScope() or\n        (previous.is_comment and previous.previous_token is not None and\n         previous.previous_token.OpensScope())):\n      dedent = (style.Get('CONTINUATION_INDENT_WIDTH'),\n                0)[style.Get('INDENT_CLOSING_BRACKETS')]\n      self.stack[-1].closing_scope_indent = (\n          max(0, self.stack[-1].indent - dedent))\n      self.stack[-1].split_before_closing_bracket = True\n\n    # Calculate the split penalty.\n    penalty = current.split_penalty\n\n    if must_split:\n      # Don't penalize for a must split.\n      return penalty\n\n    if previous.is_pseudo and previous.value == '(':\n      # Small penalty for splitting after a pseudo paren.\n      penalty += 50\n\n    # Add a penalty for each increasing newline we add, but don't penalize for\n    # splitting before an if-expression or list comprehension.\n    if current.value not in {'if', 'for'}:\n      last = self.stack[-1]\n      last.num_line_splits += 1\n      penalty += (\n          style.Get('SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT') *\n          last.num_line_splits)\n\n    if current.OpensScope() and previous.OpensScope():\n      # Prefer to keep opening brackets coalesced (unless it's at the beginning\n      # of a function call).\n      pprev = previous.previous_token\n      if not pprev or not pprev.is_name:\n        penalty += 10\n\n    return penalty + 10\n\n  def MoveStateToNextToken(self):\n    \"\"\"Calculate format decision state information and move onto the next token.\n\n    Before moving onto the next token, we first calculate the format decision\n    state given the current token and its formatting decisions. Then the format\n    decision state is set up so that the next token can be added.\n\n    Returns:\n      The penalty for the number of characters over the column limit.\n    \"\"\"\n    current = self.next_token\n    if not current.OpensScope() and not current.ClosesScope():\n      self.lowest_level_on_line = min(self.lowest_level_on_line,\n                                      self.paren_level)\n\n    # If we encounter an opening bracket, we add a level to our stack to prepare\n    # for the subsequent tokens.\n    if current.OpensScope():\n      last = self.stack[-1]\n      new_indent = style.Get('CONTINUATION_INDENT_WIDTH') + last.last_space\n\n      self.stack.append(_ParenState(new_indent, self.stack[-1].last_space))\n      self.paren_level += 1\n\n    # If we encounter a closing bracket, we can remove a level from our\n    # parenthesis stack.\n    if len(self.stack) > 1 and current.ClosesScope():\n      if subtypes.DICTIONARY_KEY_PART in current.subtypes:\n        self.stack[-2].last_space = self.stack[-2].indent\n      else:\n        self.stack[-2].last_space = self.stack[-1].last_space\n      self.stack.pop()\n      self.paren_level -= 1\n\n    is_multiline_string = current.is_string and '\\n' in current.value\n    if is_multiline_string:\n      # This is a multiline string. Only look at the first line.\n      self.column += len(current.value.split('\\n')[0])\n    elif not current.is_pseudo:\n      self.column += len(current.value)\n\n    self.next_token = self.next_token.next_token\n\n    # Calculate the penalty for overflowing the column limit.\n    penalty = 0\n    if (not current.is_pylint_comment and not current.is_pytype_comment and\n        not current.is_copybara_comment and self.column > self.column_limit):\n      excess_characters = self.column - self.column_limit\n      penalty += style.Get('SPLIT_PENALTY_EXCESS_CHARACTER') * excess_characters\n\n    if is_multiline_string:\n      # If this is a multiline string, the column is actually the\n      # end of the last line in the string.\n      self.column = len(current.value.split('\\n')[-1])\n\n    return penalty\n\n  def _CalculateComprehensionState(self, newline):\n    \"\"\"Makes required changes to comprehension state.\n\n    Args:\n      newline: Whether the current token is to be added on a newline.\n\n    Returns:\n      The penalty for the token-newline combination given the current\n      comprehension state.\n    \"\"\"\n    current = self.next_token\n    previous = current.previous_token\n    top_of_stack = self.comp_stack[-1] if self.comp_stack else None\n    penalty = 0\n\n    if top_of_stack is not None:\n      # Check if the token terminates the current comprehension.\n      if current == top_of_stack.closing_bracket:\n        last = self.comp_stack.pop()\n        # Lightly penalize comprehensions that are split across multiple lines.\n        if last.has_interior_split:\n          penalty += style.Get('SPLIT_PENALTY_COMPREHENSION')\n\n        return penalty\n\n      if newline:\n        top_of_stack.has_interior_split = True\n\n    if (subtypes.COMP_EXPR in current.subtypes and\n        subtypes.COMP_EXPR not in previous.subtypes):\n      self.comp_stack.append(object_state.ComprehensionState(current))\n      return penalty\n\n    if current.value == 'for' and subtypes.COMP_FOR in current.subtypes:\n      if top_of_stack.for_token is not None:\n        # Treat nested comprehensions like normal comp_if expressions.\n        # Example:\n        #     my_comp = [\n        #         a.qux + b.qux\n        #         for a in foo\n        #   -->   for b in bar   <--\n        #         if a.zut + b.zut\n        #     ]\n        if (style.Get('SPLIT_COMPLEX_COMPREHENSION') and\n            top_of_stack.has_split_at_for != newline and\n            (top_of_stack.has_split_at_for or\n             not top_of_stack.HasTrivialExpr())):\n          penalty += split_penalty.UNBREAKABLE\n      else:\n        top_of_stack.for_token = current\n        top_of_stack.has_split_at_for = newline\n\n        # Try to keep trivial expressions on the same line as the comp_for.\n        if (style.Get('SPLIT_COMPLEX_COMPREHENSION') and newline and\n            top_of_stack.HasTrivialExpr()):\n          penalty += split_penalty.CONNECTED\n\n    if (subtypes.COMP_IF in current.subtypes and\n        subtypes.COMP_IF not in previous.subtypes):\n      # Penalize breaking at comp_if when it doesn't match the newline structure\n      # in the rest of the comprehension.\n      if (style.Get('SPLIT_COMPLEX_COMPREHENSION') and\n          top_of_stack.has_split_at_for != newline and\n          (top_of_stack.has_split_at_for or not top_of_stack.HasTrivialExpr())):\n        penalty += split_penalty.UNBREAKABLE\n\n    return penalty\n\n  def _PushParameterListState(self, newline):\n    \"\"\"Push a new parameter list state for a function definition.\n\n    Args:\n      newline: Whether the current token is to be added on a newline.\n    \"\"\"\n    current = self.next_token\n    previous = current.previous_token\n\n    if _IsFunctionDefinition(previous):\n      first_param_column = previous.total_length + self.stack[-2].indent\n      self.param_list_stack.append(\n          object_state.ParameterListState(previous, newline,\n                                          first_param_column))\n\n  def _CalculateParameterListState(self, newline):\n    \"\"\"Makes required changes to parameter list state.\n\n    Args:\n      newline: Whether the current token is to be added on a newline.\n\n    Returns:\n      The penalty for the token-newline combination given the current\n      parameter state.\n    \"\"\"\n    current = self.next_token\n    previous = current.previous_token\n    penalty = 0\n\n    if _IsFunctionDefinition(previous):\n      first_param_column = previous.total_length + self.stack[-2].indent\n      if not newline:\n        param_list = self.param_list_stack[-1]\n        if param_list.parameters and param_list.has_typed_return:\n          last_param = param_list.parameters[-1].first_token\n          last_token = _LastTokenInLine(previous.matching_bracket)\n          total_length = last_token.total_length\n          total_length -= last_param.total_length - len(last_param.value)\n          if total_length + self.column > self.column_limit:\n            # If we need to split before the trailing code of a function\n            # definition with return types, then also split before the opening\n            # parameter so that the trailing bit isn't indented on a line by\n            # itself:\n            #\n            #   def rrrrrrrrrrrrrrrrrrrrrr(ccccccccccccccccccccccc: Tuple[Text]\n            #                              ) -> List[Tuple[Text, Text]]:\n            #       pass\n            penalty += split_penalty.VERY_STRONGLY_CONNECTED\n        return penalty\n\n      if first_param_column <= self.column:\n        # Make sure we don't split after the opening bracket if the\n        # continuation indent is greater than the opening bracket:\n        #\n        #   a(\n        #       b=1,\n        #       c=2)\n        penalty += split_penalty.VERY_STRONGLY_CONNECTED\n      return penalty\n\n    if not self.param_list_stack:\n      return penalty\n\n    param_list = self.param_list_stack[-1]\n    if current == self.param_list_stack[-1].closing_bracket:\n      self.param_list_stack.pop()  # We're done with this state.\n      if newline and param_list.has_typed_return:\n        if param_list.split_before_closing_bracket:\n          penalty -= split_penalty.STRONGLY_CONNECTED\n        elif param_list.LastParamFitsOnLine(self.column):\n          penalty += split_penalty.STRONGLY_CONNECTED\n\n      if (not newline and param_list.has_typed_return and\n          param_list.has_split_before_first_param):\n        # Prefer splitting before the closing bracket if there's a return type\n        # and we've already split before the first parameter.\n        penalty += split_penalty.STRONGLY_CONNECTED\n\n      return penalty\n\n    if not param_list.parameters:\n      return penalty\n\n    if newline:\n      if self._FitsOnLine(param_list.parameters[0].first_token,\n                          _LastTokenInLine(param_list.closing_bracket)):\n        penalty += split_penalty.STRONGLY_CONNECTED\n\n    if (not newline and style.Get('SPLIT_BEFORE_NAMED_ASSIGNS') and\n        param_list.has_default_values and\n        current != param_list.parameters[0].first_token and\n        current != param_list.closing_bracket and\n        subtypes.PARAMETER_START in current.subtypes):\n      # If we want to split before parameters when there are named assigns,\n      # then add a penalty for not splitting.\n      penalty += split_penalty.STRONGLY_CONNECTED\n\n    return penalty\n\n  def _IndentWithContinuationAlignStyle(self, column):\n    if column == 0:\n      return column\n    align_style = style.Get('CONTINUATION_ALIGN_STYLE')\n    if align_style == 'FIXED':\n      return ((self.line.depth * style.Get('INDENT_WIDTH')) +\n              style.Get('CONTINUATION_INDENT_WIDTH'))\n    if align_style == 'VALIGN-RIGHT':\n      indent_width = style.Get('INDENT_WIDTH')\n      return indent_width * int((column + indent_width - 1) / indent_width)\n    return column\n\n  def _GetNewlineColumn(self):\n    \"\"\"Return the new column on the newline.\"\"\"\n    current = self.next_token\n    previous = current.previous_token\n    top_of_stack = self.stack[-1]\n\n    if isinstance(current.spaces_required_before, list):\n      # Don't set the value here, as we need to look at the lines near\n      # this one to determine the actual horizontal alignment value.\n      return 0\n    elif current.spaces_required_before > 2 or self.line.disable:\n      return current.spaces_required_before\n\n    cont_aligned_indent = self._IndentWithContinuationAlignStyle(\n        top_of_stack.indent)\n\n    if current.OpensScope():\n      return cont_aligned_indent if self.paren_level else self.first_indent\n\n    if current.ClosesScope():\n      if (previous.OpensScope() or\n          (previous.is_comment and previous.previous_token is not None and\n           previous.previous_token.OpensScope())):\n        return max(0,\n                   top_of_stack.indent - style.Get('CONTINUATION_INDENT_WIDTH'))\n      return top_of_stack.closing_scope_indent\n\n    if (previous and previous.is_string and current.is_string and\n        subtypes.DICTIONARY_VALUE in current.subtypes):\n      return previous.column\n\n    if style.Get('INDENT_DICTIONARY_VALUE'):\n      if previous and (previous.value == ':' or previous.is_pseudo):\n        if subtypes.DICTIONARY_VALUE in current.subtypes:\n          return top_of_stack.indent\n\n    if (not self.param_list_stack and _IsCompoundStatement(self.line.first) and\n        (not (style.Get('DEDENT_CLOSING_BRACKETS') or\n              style.Get('INDENT_CLOSING_BRACKETS')) or\n         style.Get('SPLIT_BEFORE_FIRST_ARGUMENT'))):\n      token_indent = (\n          len(self.line.first.whitespace_prefix.split('\\n')[-1]) +\n          style.Get('INDENT_WIDTH'))\n      if token_indent == top_of_stack.indent:\n        return token_indent + style.Get('CONTINUATION_INDENT_WIDTH')\n\n    if (self.param_list_stack and\n        not self.param_list_stack[-1].SplitBeforeClosingBracket(\n            top_of_stack.indent) and top_of_stack.indent\n        == ((self.line.depth + 1) * style.Get('INDENT_WIDTH'))):\n      if (subtypes.PARAMETER_START in current.subtypes or\n          (previous.is_comment and\n           subtypes.PARAMETER_START in previous.subtypes)):\n        return top_of_stack.indent + style.Get('CONTINUATION_INDENT_WIDTH')\n\n    return cont_aligned_indent\n\n  def _FitsOnLine(self, start, end):\n    \"\"\"Determines if line between start and end can fit on the current line.\"\"\"\n    length = end.total_length - start.total_length\n    if not start.is_pseudo:\n      length += len(start.value)\n    return length + self.column <= self.column_limit\n\n  def _EachDictEntryFitsOnOneLine(self, opening):\n    \"\"\"Determine if each dict elems can fit on one line.\"\"\"\n\n    def PreviousNonCommentToken(tok):\n      tok = tok.previous_token\n      while tok.is_comment:\n        tok = tok.previous_token\n      return tok\n\n    def ImplicitStringConcatenation(tok):\n      num_strings = 0\n      if tok.is_pseudo:\n        tok = tok.next_token\n      while tok.is_string:\n        num_strings += 1\n        tok = tok.next_token\n      return num_strings > 1\n\n    def DictValueIsContainer(opening, closing):\n      \"\"\"Return true if the dictionary value is a container.\"\"\"\n      if not opening or not closing:\n        return False\n      colon = opening.previous_token\n      while colon:\n        if not colon.is_pseudo:\n          break\n        colon = colon.previous_token\n      if not colon or colon.value != ':':\n        return False\n      key = colon.previous_token\n      if not key:\n        return False\n      return subtypes.DICTIONARY_KEY_PART in key.subtypes\n\n    closing = opening.matching_bracket\n    entry_start = opening.next_token\n    current = opening.next_token.next_token\n\n    while current and current != closing:\n      if subtypes.DICT_SET_GENERATOR in current.subtypes:\n        break\n      if subtypes.DICTIONARY_KEY in current.subtypes:\n        prev = PreviousNonCommentToken(current)\n        if prev.value == ',':\n          prev = PreviousNonCommentToken(prev.previous_token)\n        if not DictValueIsContainer(prev.matching_bracket, prev):\n          length = prev.total_length - entry_start.total_length\n          length += len(entry_start.value)\n          if length + self.stack[-2].indent >= self.column_limit:\n            return False\n        entry_start = current\n      if current.OpensScope():\n        if ((current.value == '{' or\n             (current.is_pseudo and current.next_token.value == '{') and\n             subtypes.DICTIONARY_VALUE in current.subtypes) or\n            ImplicitStringConcatenation(current)):\n          # A dictionary entry that cannot fit on a single line shouldn't matter\n          # to this calculation. If it can't fit on a single line, then the\n          # opening should be on the same line as the key and the rest on\n          # newlines after it. But the other entries should be on single lines\n          # if possible.\n          if current.matching_bracket:\n            current = current.matching_bracket\n          while current:\n            if current == closing:\n              return True\n            if subtypes.DICTIONARY_KEY in current.subtypes:\n              entry_start = current\n              break\n            current = current.next_token\n        else:\n          current = current.matching_bracket\n      else:\n        current = current.next_token\n\n    # At this point, current is the closing bracket. Go back one to get the end\n    # of the dictionary entry.\n    current = PreviousNonCommentToken(current)\n    length = current.total_length - entry_start.total_length\n    length += len(entry_start.value)\n    return length + self.stack[-2].indent <= self.column_limit\n\n  def _ArgumentListHasDictionaryEntry(self, token):\n    \"\"\"Check if the function argument list has a dictionary as an arg.\"\"\"\n    if _IsArgumentToFunction(token):\n      while token:\n        if token.value == '{':\n          length = token.matching_bracket.total_length - token.total_length\n          return length + self.stack[-2].indent > self.column_limit\n        if token.ClosesScope():\n          break\n        if token.OpensScope():\n          token = token.matching_bracket\n        token = token.next_token\n    return False\n\n  def _ContainerFitsOnStartLine(self, opening):\n    \"\"\"Check if the container can fit on its starting line.\"\"\"\n    return (opening.matching_bracket.total_length - opening.total_length +\n            self.stack[-1].indent) <= self.column_limit\n\n\n_COMPOUND_STMTS = frozenset({\n    'for',\n    'while',\n    'if',\n    'elif',\n    'with',\n    'except',\n    'def',\n    'class',\n})\n\n\ndef _IsCompoundStatement(token):\n  value = token.value\n  if value == 'async':\n    token = token.next_token\n  if token.value in _COMPOUND_STMTS:\n    return True\n  parent_name = NodeName(token.node.parent)\n  return value == 'match' and parent_name == 'match_stmt' or \\\n    value == 'case' and parent_name == 'case_stmt'\n\n\ndef _IsFunctionDef(token):\n  if token.value == 'async':\n    token = token.next_token\n  return token.value == 'def'\n\n\ndef _IsFunctionCallWithArguments(token):\n  while token:\n    if token.value == '(':\n      token = token.next_token\n      return token and token.value != ')'\n    elif token.name not in {'NAME', 'DOT', 'EQUAL'}:\n      break\n    token = token.next_token\n  return False\n\n\ndef _IsArgumentToFunction(token):\n  bracket = logical_line.IsSurroundedByBrackets(token)\n  if not bracket or bracket.value != '(':\n    return False\n  previous = bracket.previous_token\n  return previous and previous.is_name\n\n\ndef _GetOpeningBracket(current):\n  \"\"\"Get the opening bracket containing the current token.\"\"\"\n  if current.matching_bracket and not current.is_pseudo:\n    return current if current.OpensScope() else current.matching_bracket\n\n  while current:\n    if current.ClosesScope():\n      current = current.matching_bracket\n    elif current.is_pseudo:\n      current = current.previous_token\n    elif current.OpensScope():\n      return current\n    current = current.previous_token\n  return None\n\n\ndef _LastTokenInLine(current):\n  while not current.is_comment and current.next_token:\n    current = current.next_token\n  return current\n\n\ndef _IsFunctionDefinition(current):\n  prev = current.previous_token\n  return current.value == '(' and prev and subtypes.FUNC_DEF in prev.subtypes\n\n\ndef _IsLastScopeInLine(current):\n  current = current.matching_bracket\n  while current:\n    current = current.next_token\n    if current and current.OpensScope():\n      return False\n  return True\n\n\ndef _IsSingleElementTuple(token):\n  \"\"\"Check if it's a single-element tuple.\"\"\"\n  close = token.matching_bracket\n  token = token.next_token\n  num_commas = 0\n  while token != close:\n    if token.value == ',':\n      num_commas += 1\n    token = token.matching_bracket if token.OpensScope() else token.next_token\n  return num_commas == 1\n\n\ndef _ScopeHasNoCommas(token):\n  \"\"\"Check if the scope has no commas.\"\"\"\n  close = token.matching_bracket\n  token = token.next_token\n  while token != close:\n    if token.value == ',':\n      return False\n    token = token.matching_bracket if token.OpensScope() else token.next_token\n  return True\n\n\nclass _ParenState(object):\n  \"\"\"Maintains the state of the bracket enclosures.\n\n  A stack of _ParenState objects are kept so that we know how to indent relative\n  to the brackets.\n\n  Attributes:\n    indent: The column position to which a specified parenthesis level needs to\n      be indented.\n    last_space: The column position of the last space on each level.\n    closing_scope_indent: The column position of the closing indentation.\n    split_before_closing_bracket: Whether a newline needs to be inserted before\n      the closing bracket. We only want to insert a newline before the closing\n      bracket if there also was a newline after the beginning left bracket.\n    num_line_splits: Number of line splits this _ParenState contains already.\n      Each subsequent line split gets an increasing penalty.\n  \"\"\"\n\n  # TODO(morbo): This doesn't track \"bin packing.\"\n\n  def __init__(self, indent, last_space):\n    self.indent = indent\n    self.last_space = last_space\n    self.closing_scope_indent = 0\n    self.split_before_closing_bracket = False\n    self.num_line_splits = 0\n\n  def Clone(self):\n    state = _ParenState(self.indent, self.last_space)\n    state.closing_scope_indent = self.closing_scope_indent\n    state.split_before_closing_bracket = self.split_before_closing_bracket\n    state.num_line_splits = self.num_line_splits\n    return state\n\n  def __repr__(self):\n    return '[indent::%d, last_space::%d, closing_scope_indent::%d]' % (\n        self.indent, self.last_space, self.closing_scope_indent)\n\n  def __eq__(self, other):\n    return hash(self) == hash(other)\n\n  def __ne__(self, other):\n    return not self == other\n\n  def __hash__(self, *args, **kwargs):\n    return hash((self.indent, self.last_space, self.closing_scope_indent,\n                 self.split_before_closing_bracket, self.num_line_splits))\n"
  },
  {
    "path": "yapf/yapflib/format_token.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Enhanced token information for formatting.\"\"\"\n\nimport keyword\nimport re\nfrom functools import lru_cache\n\nfrom yapf_third_party._ylib2to3.pgen2 import token\nfrom yapf_third_party._ylib2to3.pytree import type_repr\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.yapflib import style\nfrom yapf.yapflib import subtypes\n\nCONTINUATION = token.N_TOKENS\n\n_OPENING_BRACKETS = frozenset({'(', '[', '{'})\n_CLOSING_BRACKETS = frozenset({')', ']', '}'})\n\n\ndef _TabbedContinuationAlignPadding(spaces, align_style, tab_width):\n  \"\"\"Build padding string for continuation alignment in tabbed indentation.\n\n  Arguments:\n    spaces: (int) The number of spaces to place before the token for alignment.\n    align_style: (str) The alignment style for continuation lines.\n    tab_width: (int) Number of columns of each tab character.\n\n  Returns:\n    A padding string for alignment with style specified by align_style option.\n  \"\"\"\n  if align_style in ('FIXED', 'VALIGN-RIGHT'):\n    if spaces > 0:\n      return '\\t' * int((spaces + tab_width - 1) / tab_width)\n    return ''\n  return ' ' * spaces\n\n\nclass FormatToken(object):\n  \"\"\"Enhanced token information for formatting.\n\n  This represents the token plus additional information useful for reformatting\n  the code.\n\n  Attributes:\n    node: The original token node.\n    next_token: The token in the logical line after this token or None if this\n      is the last token in the logical line.\n    previous_token: The token in the logical line before this token or None if\n      this is the first token in the logical line.\n    matching_bracket: If a bracket token ('[', '{', or '(') the matching\n      bracket.\n    parameters: If this and its following tokens make up a parameter list, then\n      this is a list of those parameters.\n    container_opening: If the object is in a container, this points to its\n      opening bracket.\n    container_elements: If this is the start of a container, a list of the\n      elements in the container.\n    whitespace_prefix: The prefix for the whitespace.\n    spaces_required_before: The number of spaces required before a token. This\n      is a lower-bound for the formatter and not a hard requirement. For\n      instance, a comment may have n required spaces before it. But the\n      formatter won't place n spaces before all comments. Only those that are\n      moved to the end of a line of code. The formatter may use different\n      spacing when appropriate.\n    total_length: The total length of the logical line up to and including\n      whitespace and this token. However, this doesn't include the initial\n      indentation amount.\n    split_penalty: The penalty for splitting the line before this token.\n    can_break_before: True if we're allowed to break before this token.\n    must_break_before: True if we're required to break before this token.\n    newlines: The number of newlines needed before this token.\n  \"\"\"\n\n  def __init__(self, node, name):\n    \"\"\"Constructor.\n\n    Arguments:\n      node: (pytree.Leaf) The node that's being wrapped.\n      name: (string) The name of the node.\n    \"\"\"\n    self.node = node\n    self.name = name\n    self.type = node.type\n    self.column = node.column\n    self.lineno = node.lineno\n    self.value = node.value\n\n    if self.is_continuation:\n      self.value = node.value.rstrip()\n\n    self.next_token = None\n    self.previous_token = None\n    self.matching_bracket = None\n    self.parameters = []\n    self.container_opening = None\n    self.container_elements = []\n    self.whitespace_prefix = ''\n    self.total_length = 0\n    self.split_penalty = 0\n    self.can_break_before = False\n    self.must_break_before = pytree_utils.GetNodeAnnotation(\n        node, pytree_utils.Annotation.MUST_SPLIT, default=False)\n    self.newlines = pytree_utils.GetNodeAnnotation(\n        node, pytree_utils.Annotation.NEWLINES)\n    self.spaces_required_before = 0\n\n    if self.is_comment:\n      self.spaces_required_before = style.Get('SPACES_BEFORE_COMMENT')\n\n    stypes = pytree_utils.GetNodeAnnotation(node,\n                                            pytree_utils.Annotation.SUBTYPE)\n    self.subtypes = {subtypes.NONE} if not stypes else stypes\n    self.is_pseudo = hasattr(node, 'is_pseudo') and node.is_pseudo\n\n  @property\n  def formatted_whitespace_prefix(self):\n    if style.Get('INDENT_BLANK_LINES'):\n      without_newlines = self.whitespace_prefix.lstrip('\\n')\n      height = len(self.whitespace_prefix) - len(without_newlines)\n      if height:\n        return ('\\n' + without_newlines) * height\n    return self.whitespace_prefix\n\n  def AddWhitespacePrefix(self, newlines_before, spaces=0, indent_level=0):\n    \"\"\"Register a token's whitespace prefix.\n\n    This is the whitespace that will be output before a token's string.\n\n    Arguments:\n      newlines_before: (int) The number of newlines to place before the token.\n      spaces: (int) The number of spaces to place before the token.\n      indent_level: (int) The indentation level.\n    \"\"\"\n    if style.Get('USE_TABS'):\n      if newlines_before > 0:\n        indent_before = '\\t' * indent_level + _TabbedContinuationAlignPadding(\n            spaces, style.Get('CONTINUATION_ALIGN_STYLE'),\n            style.Get('INDENT_WIDTH'))\n      else:\n        indent_before = '\\t' * indent_level + ' ' * spaces\n    else:\n      indent_before = (' ' * indent_level * style.Get('INDENT_WIDTH') +\n                       ' ' * spaces)\n\n    if self.is_comment:\n      comment_lines = [s.lstrip() for s in self.value.splitlines()]\n      self.value = ('\\n' + indent_before).join(comment_lines)\n\n      # Update our own value since we are changing node value\n      self.value = self.value\n\n    if not self.whitespace_prefix:\n      self.whitespace_prefix = ('\\n' * (self.newlines or newlines_before) +\n                                indent_before)\n    else:\n      self.whitespace_prefix += indent_before\n\n  def AdjustNewlinesBefore(self, newlines_before):\n    \"\"\"Change the number of newlines before this token.\"\"\"\n    self.whitespace_prefix = ('\\n' * newlines_before +\n                              self.whitespace_prefix.lstrip('\\n'))\n\n  def RetainHorizontalSpacing(self, first_column, depth):\n    \"\"\"Retains a token's horizontal spacing.\"\"\"\n    previous = self.previous_token\n    if not previous:\n      return\n\n    if previous.is_pseudo:\n      previous = previous.previous_token\n      if not previous:\n        return\n\n    cur_lineno = self.lineno\n    prev_lineno = previous.lineno\n    if previous.is_multiline_string:\n      prev_lineno += previous.value.count('\\n')\n\n    if (cur_lineno != prev_lineno or\n        (previous.is_pseudo and previous.value != ')' and\n         cur_lineno != previous.previous_token.lineno)):\n      self.spaces_required_before = (\n          self.column - first_column + depth * style.Get('INDENT_WIDTH'))\n      return\n\n    cur_column = self.column\n    prev_column = previous.column\n    prev_len = len(previous.value)\n\n    if previous.is_pseudo and previous.value == ')':\n      prev_column -= 1\n      prev_len = 0\n\n    if previous.is_multiline_string:\n      prev_len = len(previous.value.split('\\n')[-1])\n      if '\\n' in previous.value:\n        prev_column = 0  # Last line starts in column 0.\n\n    self.spaces_required_before = cur_column - (prev_column + prev_len)\n\n  def OpensScope(self):\n    return self.value in _OPENING_BRACKETS\n\n  def ClosesScope(self):\n    return self.value in _CLOSING_BRACKETS\n\n  def AddSubtype(self, subtype):\n    self.subtypes.add(subtype)\n\n  def __repr__(self):\n    msg = ('FormatToken(name={0}, value={1}, column={2}, lineno={3}, '\n           'splitpenalty={4}'.format(\n               'DOCSTRING' if self.is_docstring else self.name, self.value,\n               self.column, self.lineno, self.split_penalty))\n    msg += ', pseudo)' if self.is_pseudo else ')'\n    return msg\n\n  @property\n  def node_split_penalty(self):\n    \"\"\"Split penalty attached to the pytree node of this token.\"\"\"\n    return pytree_utils.GetNodeAnnotation(\n        self.node, pytree_utils.Annotation.SPLIT_PENALTY, default=0)\n\n  @property\n  def is_binary_op(self):\n    \"\"\"Token is a binary operator.\"\"\"\n    return subtypes.BINARY_OPERATOR in self.subtypes\n\n  @property\n  @lru_cache()\n  def is_arithmetic_op(self):\n    \"\"\"Token is an arithmetic operator.\"\"\"\n    return self.value in frozenset({\n        '+',  # Add\n        '-',  # Subtract\n        '*',  # Multiply\n        '@',  # Matrix Multiply\n        '/',  # Divide\n        '//',  # Floor Divide\n        '%',  # Modulo\n        '<<',  # Left Shift\n        '>>',  # Right Shift\n        '|',  # Bitwise Or\n        '&',  # Bitwise Add\n        '^',  # Bitwise Xor\n        '**',  # Power\n    })\n\n  @property\n  def is_simple_expr(self):\n    \"\"\"Token is an operator in a simple expression.\"\"\"\n    return subtypes.SIMPLE_EXPRESSION in self.subtypes\n\n  @property\n  def is_subscript_colon(self):\n    \"\"\"Token is a subscript colon.\"\"\"\n    return subtypes.SUBSCRIPT_COLON in self.subtypes\n\n  @property\n  def is_comment(self):\n    return self.type == token.COMMENT\n\n  @property\n  def is_continuation(self):\n    return self.type == CONTINUATION\n\n  @property\n  @lru_cache()\n  def is_keyword(self):\n    return keyword.iskeyword(\n        self.value) or (self.value == 'match' and\n                        type_repr(self.node.parent.type) == 'match_stmt') or (\n                            self.value == 'case' and\n                            type_repr(self.node.parent.type) == 'case_block')\n\n  @property\n  def is_name(self):\n    return self.type == token.NAME and not self.is_keyword\n\n  @property\n  def is_number(self):\n    return self.type == token.NUMBER\n\n  @property\n  def is_string(self):\n    return self.type == token.STRING\n\n  @property\n  def is_multiline_string(self):\n    \"\"\"Test if this string is a multiline string.\n\n    Returns:\n      A multiline string always ends with triple quotes, so if it is a string\n      token, inspect the last 3 characters and return True if it is a triple\n      double or triple single quote mark.\n    \"\"\"\n    return self.is_string and self.value.endswith(('\"\"\"', \"'''\"))\n\n  @property\n  def is_docstring(self):\n    return self.is_string and self.previous_token is None\n\n  @property\n  def is_pylint_comment(self):\n    return self.is_comment and re.match(r'#.*\\bpylint:\\s*(disable|enable)=',\n                                        self.value)\n\n  @property\n  def is_pytype_comment(self):\n    return self.is_comment and re.match(r'#.*\\bpytype:\\s*(disable|enable)=',\n                                        self.value)\n\n  @property\n  def is_copybara_comment(self):\n    return self.is_comment and re.match(\n        r'#.*\\bcopybara:\\s*(strip|insert|replace)', self.value)\n"
  },
  {
    "path": "yapf/yapflib/identify_container.py",
    "content": "# Copyright 2018 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Identify containers for lib2to3 trees.\n\nThis module identifies containers and the elements in them. Each element points\nto the opening bracket and vice-versa.\n\n  IdentifyContainers(): the main function exported by this module.\n\"\"\"\n\nfrom yapf_third_party._ylib2to3.pgen2 import token as grammar_token\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.pytree import pytree_visitor\n\n\ndef IdentifyContainers(tree):\n  \"\"\"Run the identify containers visitor over the tree, modifying it in place.\n\n  Arguments:\n    tree: the top-level pytree node to annotate with subtypes.\n  \"\"\"\n  identify_containers = _IdentifyContainers()\n  identify_containers.Visit(tree)\n\n\nclass _IdentifyContainers(pytree_visitor.PyTreeVisitor):\n  \"\"\"_IdentifyContainers - see file-level docstring for detailed description.\"\"\"\n\n  def Visit_trailer(self, node):  # pylint: disable=invalid-name\n    for child in node.children:\n      self.Visit(child)\n\n    if len(node.children) != 3:\n      return\n    if node.children[0].type != grammar_token.LPAR:\n      return\n\n    if pytree_utils.NodeName(node.children[1]) == 'arglist':\n      for child in node.children[1].children:\n        pytree_utils.SetOpeningBracket(\n            pytree_utils.FirstLeafNode(child), node.children[0])\n    else:\n      pytree_utils.SetOpeningBracket(\n          pytree_utils.FirstLeafNode(node.children[1]), node.children[0])\n\n  def Visit_atom(self, node):  # pylint: disable=invalid-name\n    for child in node.children:\n      self.Visit(child)\n\n    if len(node.children) != 3:\n      return\n    if node.children[0].type != grammar_token.LPAR:\n      return\n\n    for child in node.children[1].children:\n      pytree_utils.SetOpeningBracket(\n          pytree_utils.FirstLeafNode(child), node.children[0])\n"
  },
  {
    "path": "yapf/yapflib/line_joiner.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Join logical lines together.\n\nDetermine how many lines can be joined into one line. For instance, we could\njoin these statements into one line:\n\n  if a == 42:\n    continue\n\nlike this:\n\n  if a == 42: continue\n\nThere are a few restrictions:\n\n  1. The lines should have been joined in the original source.\n  2. The joined lines must not go over the column boundary if placed on the same\n     line.\n  3. They need to be very simple statements.\n\nNote: Because we don't allow the use of a semicolon to separate statements, it\nfollows that there can only be at most two lines to join.\n\"\"\"\n\nfrom yapf.yapflib import style\n\n_CLASS_OR_FUNC = frozenset({'def', 'class'})\n\n\ndef CanMergeMultipleLines(lines, last_was_merged=False):\n  \"\"\"Determine if multiple lines can be joined into one.\n\n  Arguments:\n    lines: (list of LogicalLine) This is a splice of LogicalLines from the full\n      code base.\n    last_was_merged: (bool) The last line was merged.\n\n  Returns:\n    True if two consecutive lines can be joined together. In reality, this will\n    only happen if two consecutive lines can be joined, due to the style guide.\n  \"\"\"\n  # The indentation amount for the starting line (number of spaces).\n  indent_amt = lines[0].depth * style.Get('INDENT_WIDTH')\n  if len(lines) == 1 or indent_amt > style.Get('COLUMN_LIMIT'):\n    return False\n\n  if (len(lines) >= 3 and lines[2].depth >= lines[1].depth and\n      lines[0].depth != lines[2].depth):\n    # If lines[2]'s depth is greater than or equal to line[1]'s depth, we're not\n    # looking at a single statement (e.g., if-then, while, etc.). A following\n    # line with the same depth as the first line isn't part of the lines we\n    # would want to combine.\n    return False  # Don't merge more than two lines together.\n\n  if lines[0].first.value in _CLASS_OR_FUNC:\n    # Don't join lines onto the starting line of a class or function.\n    return False\n\n  limit = style.Get('COLUMN_LIMIT') - indent_amt\n  if lines[0].last.total_length < limit:\n    limit -= lines[0].last.total_length\n\n    if lines[0].first.value == 'if':\n      return _CanMergeLineIntoIfStatement(lines, limit)\n    if last_was_merged and lines[0].first.value in {'elif', 'else'}:\n      return _CanMergeLineIntoIfStatement(lines, limit)\n\n  # TODO(morbo): Other control statements?\n\n  return False\n\n\ndef _CanMergeLineIntoIfStatement(lines, limit):\n  \"\"\"Determine if we can merge a short if-then statement into one line.\n\n  Two lines of an if-then statement can be merged if they were that way in the\n  original source, fit on the line without going over the column limit, and are\n  considered \"simple\" statements --- typically statements like 'pass',\n  'continue', and 'break'.\n\n  Arguments:\n    lines: (list of LogicalLine) The lines we are wanting to merge.\n    limit: (int) The amount of space remaining on the line.\n\n  Returns:\n    True if the lines can be merged, False otherwise.\n  \"\"\"\n  if len(lines[1].tokens) == 1 and lines[1].last.is_multiline_string:\n    # This might be part of a multiline shebang.\n    return True\n  if lines[0].lineno != lines[1].lineno:\n    # Don't merge lines if the original lines weren't merged.\n    return False\n  if lines[1].last.total_length >= limit:\n    # Don't merge lines if the result goes over the column limit.\n    return False\n  return style.Get('JOIN_MULTIPLE_LINES')\n"
  },
  {
    "path": "yapf/yapflib/logical_line.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"LogicalLine primitive for formatting.\n\nA logical line is the containing data structure produced by the parser. It\ncollects all nodes (stored in FormatToken objects) that could appear on a single\nline if there were no line length restrictions. It's then used by the parser to\nperform the wrapping required to comply with the style guide.\n\"\"\"\n\nfrom yapf_third_party._ylib2to3.fixer_util import syms as python_symbols\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.pytree import split_penalty\nfrom yapf.yapflib import format_token\nfrom yapf.yapflib import style\nfrom yapf.yapflib import subtypes\n\n\nclass LogicalLine(object):\n  \"\"\"Represents a single logical line in the output.\n\n  Attributes:\n    depth: indentation depth of this line. This is just a numeric value used to\n      distinguish lines that are more deeply nested than others. It is not the\n      actual amount of spaces, which is style-dependent.\n  \"\"\"\n\n  def __init__(self, depth, tokens=None):\n    \"\"\"Constructor.\n\n    Creates a new logical line with the given depth an initial list of tokens.\n    Constructs the doubly-linked lists for format tokens using their built-in\n    next_token and previous_token attributes.\n\n    Arguments:\n      depth: indentation depth of this line\n      tokens: initial list of tokens\n    \"\"\"\n    self.depth = depth\n    self._tokens = tokens or []\n    self.disable = False\n\n    if self._tokens:\n      # Set up a doubly linked list.\n      for index, tok in enumerate(self._tokens[1:]):\n        # Note, 'index' is the index to the previous token.\n        tok.previous_token = self._tokens[index]\n        self._tokens[index].next_token = tok\n\n  def CalculateFormattingInformation(self):\n    \"\"\"Calculate the split penalty and total length for the tokens.\"\"\"\n    # Say that the first token in the line should have a space before it. This\n    # means only that if this logical line is joined with a predecessor line,\n    # then there will be a space between them.\n    self.first.spaces_required_before = 1\n    self.first.total_length = len(self.first.value)\n\n    prev_token = self.first\n    prev_length = self.first.total_length\n    for token in self._tokens[1:]:\n      if (token.spaces_required_before == 0 and\n          _SpaceRequiredBetween(prev_token, token, self.disable)):\n        token.spaces_required_before = 1\n\n      tok_len = len(token.value) if not token.is_pseudo else 0\n\n      spaces_required_before = token.spaces_required_before\n      if isinstance(spaces_required_before, list):\n        assert token.is_comment, token\n\n        # If here, we are looking at a comment token that appears on a line\n        # with other tokens (but because it is a comment, it is always the last\n        # token).  Rather than specifying the actual number of spaces here,\n        # hard code a value of 0 and then set it later. This logic only works\n        # because this comment token is guaranteed to be the last token in the\n        # list.\n        spaces_required_before = 0\n\n      token.total_length = prev_length + tok_len + spaces_required_before\n\n      # The split penalty has to be computed before {must|can}_break_before,\n      # because these may use it for their decision.\n      token.split_penalty += _SplitPenalty(prev_token, token)\n      token.must_break_before = _MustBreakBefore(prev_token, token)\n      token.can_break_before = (\n          token.must_break_before or _CanBreakBefore(prev_token, token))\n\n      prev_length = token.total_length\n      prev_token = token\n\n  def Split(self):\n    \"\"\"Split the line at semicolons.\"\"\"\n    if not self.has_semicolon or self.disable:\n      return [self]\n\n    llines = []\n    lline = LogicalLine(self.depth)\n    for tok in self._tokens:\n      if tok.value == ';':\n        llines.append(lline)\n        lline = LogicalLine(self.depth)\n      else:\n        lline.AppendToken(tok)\n\n    if lline.tokens:\n      llines.append(lline)\n\n    for lline in llines:\n      lline.first.previous_token = None\n      lline.last.next_token = None\n\n    return llines\n\n  ############################################################################\n  # Token Access and Manipulation Methods                                    #\n  ############################################################################\n\n  def AppendToken(self, token):\n    \"\"\"Append a new FormatToken to the tokens contained in this line.\"\"\"\n    if self._tokens:\n      token.previous_token = self.last\n      self.last.next_token = token\n    self._tokens.append(token)\n\n  @property\n  def first(self):\n    \"\"\"Returns the first non-whitespace token.\"\"\"\n    return self._tokens[0]\n\n  @property\n  def last(self):\n    \"\"\"Returns the last non-whitespace token.\"\"\"\n    return self._tokens[-1]\n\n  ############################################################################\n  # Token -> String Methods                                                  #\n  ############################################################################\n\n  def AsCode(self, indent_per_depth=2):\n    \"\"\"Return a \"code\" representation of this line.\n\n    The code representation shows how the line would be printed out as code.\n\n    TODO(eliben): for now this is rudimentary for debugging - once we add\n    formatting capabilities, this method will have other uses (not all tokens\n    have spaces around them, for example).\n\n    Arguments:\n      indent_per_depth: how much spaces to indent per depth level.\n\n    Returns:\n      A string representing the line as code.\n    \"\"\"\n    indent = ' ' * indent_per_depth * self.depth\n    tokens_str = ' '.join(tok.value for tok in self._tokens)\n    return indent + tokens_str\n\n  def __str__(self):  # pragma: no cover\n    return self.AsCode()\n\n  def __repr__(self):  # pragma: no cover\n    tokens_repr = ','.join(\n        '{0}({1!r})'.format(tok.name, tok.value) for tok in self._tokens)\n    return 'LogicalLine(depth={0}, tokens=[{1}])'.format(\n        self.depth, tokens_repr)\n\n  ############################################################################\n  # Properties                                                               #\n  ############################################################################\n\n  @property\n  def tokens(self):\n    \"\"\"Access the tokens contained within this line.\n\n    The caller must not modify the tokens list returned by this method.\n\n    Returns:\n      List of tokens in this line.\n    \"\"\"\n    return self._tokens\n\n  @property\n  def lineno(self):\n    \"\"\"Return the line number of this logical line.\n\n    Returns:\n      The line number of the first token in this logical line.\n    \"\"\"\n    return self.first.lineno\n\n  @property\n  def start(self):\n    \"\"\"The start of the logical line.\n\n    Returns:\n      A tuple of the starting line number and column.\n    \"\"\"\n    return (self.first.lineno, self.first.column)\n\n  @property\n  def end(self):\n    \"\"\"The end of the logical line.\n\n    Returns:\n      A tuple of the ending line number and column.\n    \"\"\"\n    return (self.last.lineno, self.last.column + len(self.last.value))\n\n  @property\n  def is_comment(self):\n    return self.first.is_comment\n\n  @property\n  def has_semicolon(self):\n    return any(tok.value == ';' for tok in self._tokens)\n\n\ndef _IsIdNumberStringToken(tok):\n  return tok.is_keyword or tok.is_name or tok.is_number or tok.is_string\n\n\ndef _IsUnaryOperator(tok):\n  return subtypes.UNARY_OPERATOR in tok.subtypes\n\n\ndef _HasPrecedence(tok):\n  \"\"\"Whether a binary operation has precedence within its context.\"\"\"\n  node = tok.node\n\n  # We let ancestor be the statement surrounding the operation that tok is the\n  # operator in.\n  ancestor = node.parent.parent\n\n  while ancestor is not None:\n    # Search through the ancestor nodes in the parse tree for operators with\n    # lower precedence.\n    predecessor_type = pytree_utils.NodeName(ancestor)\n    if predecessor_type in ['arith_expr', 'term']:\n      # An ancestor \"arith_expr\" or \"term\" means we have found an operator\n      # with lower precedence than our tok.\n      return True\n    if predecessor_type != 'atom':\n      # We understand the context to look for precedence within as an\n      # arbitrary nesting of \"arith_expr\", \"term\", and \"atom\" nodes. If we\n      # leave this context we have not found a lower precedence operator.\n      return False\n    # Under normal usage we expect a complete parse tree to be available and\n    # we will return before we get an AttributeError from the root.\n    ancestor = ancestor.parent\n\n\ndef _PriorityIndicatingNoSpace(tok):\n  \"\"\"Whether to remove spaces around an operator due to precedence.\"\"\"\n  if not tok.is_arithmetic_op or not tok.is_simple_expr:\n    # Limit space removal to highest priority arithmetic operators\n    return False\n  return _HasPrecedence(tok)\n\n\ndef _IsSubscriptColonAndValuePair(token1, token2):\n  return (token1.is_number or token1.is_name) and token2.is_subscript_colon\n\n\ndef _SpaceRequiredBetween(left, right, is_line_disabled):\n  \"\"\"Return True if a space is required between the left and right token.\"\"\"\n  lval = left.value\n  rval = right.value\n  if (left.is_pseudo and _IsIdNumberStringToken(right) and\n      left.previous_token and _IsIdNumberStringToken(left.previous_token)):\n    # Space between keyword... tokens and pseudo parens.\n    return True\n  if left.is_pseudo or right.is_pseudo:\n    # There should be a space after the ':' in a dictionary.\n    if left.OpensScope():\n      return True\n    # The closing pseudo-paren shouldn't affect spacing.\n    return False\n  if left.is_continuation or right.is_continuation:\n    # The continuation node's value has all of the spaces it needs.\n    return False\n  if right.name in pytree_utils.NONSEMANTIC_TOKENS:\n    # No space before a non-semantic token.\n    return False\n  if _IsIdNumberStringToken(left) and _IsIdNumberStringToken(right):\n    # Spaces between keyword, string, number, and identifier tokens.\n    return True\n  if lval == ',' and rval == ':':\n    # We do want a space between a comma and colon.\n    return True\n  if style.Get('SPACE_INSIDE_BRACKETS'):\n    # Supersede the \"no space before a colon or comma\" check.\n    if left.OpensScope() and rval == ':':\n      return True\n    if right.ClosesScope() and lval == ':':\n      return True\n  if (style.Get('SPACES_AROUND_SUBSCRIPT_COLON') and\n      (_IsSubscriptColonAndValuePair(left, right) or\n       _IsSubscriptColonAndValuePair(right, left))):\n    # Supersede the \"never want a space before a colon or comma\" check.\n    return True\n  if rval in ':,':\n    # Otherwise, we never want a space before a colon or comma.\n    return False\n  if lval == ',' and rval in ']})':\n    # Add a space between ending ',' and closing bracket if requested.\n    return style.Get('SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET')\n  if lval == ',':\n    # We want a space after a comma.\n    return True\n  if lval == 'from' and rval == '.':\n    # Space before the '.' in an import statement.\n    return True\n  if lval == '.' and rval == 'import':\n    # Space after the '.' in an import statement.\n    return True\n  if (lval == '=' and rval in {'.', ',,,'} and\n      subtypes.DEFAULT_OR_NAMED_ASSIGN not in left.subtypes):\n    # Space between equal and '.' as in \"X = ...\".\n    return True\n  if lval == ':' and rval in {'.', '...'}:\n    # Space between : and ...\n    return True\n  if ((right.is_keyword or right.is_name) and\n      (left.is_keyword or left.is_name)):\n    # Don't merge two keywords/identifiers.\n    return True\n  if (subtypes.SUBSCRIPT_COLON in left.subtypes or\n      subtypes.SUBSCRIPT_COLON in right.subtypes):\n    # A subscript shouldn't have spaces separating its colons.\n    return False\n  if (subtypes.TYPED_NAME in left.subtypes or\n      subtypes.TYPED_NAME in right.subtypes):\n    # A typed argument should have a space after the colon.\n    return True\n  if left.is_string:\n    if (rval == '=' and\n        subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST in right.subtypes):\n      # If there is a type hint, then we don't want to add a space between the\n      # equal sign and the hint.\n      return False\n    if rval not in '[)]}.' and not right.is_binary_op:\n      # A string followed by something other than a subscript, closing bracket,\n      # dot, or a binary op should have a space after it.\n      return True\n    if right.ClosesScope():\n      # A string followed by closing brackets should have a space after it\n      # depending on SPACE_INSIDE_BRACKETS.  A string followed by opening\n      # brackets, however, should not.\n      return style.Get('SPACE_INSIDE_BRACKETS')\n    if subtypes.SUBSCRIPT_BRACKET in right.subtypes:\n      # It's legal to do this in Python: 'hello'[a]\n      return False\n  if left.is_binary_op and lval != '**' and _IsUnaryOperator(right):\n    # Space between the binary operator and the unary operator.\n    return True\n  if left.is_keyword and _IsUnaryOperator(right):\n    # Handle things like \"not -3 < x\".\n    return True\n  if _IsUnaryOperator(left) and _IsUnaryOperator(right):\n    # No space between two unary operators.\n    return False\n  if left.is_binary_op or right.is_binary_op:\n    if lval == '**' or rval == '**':\n      # Space around the \"power\" operator.\n      return style.Get('SPACES_AROUND_POWER_OPERATOR')\n    # Enforce spaces around binary operators except the blocked ones.\n    block_list = style.Get('NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS')\n    if lval in block_list or rval in block_list:\n      return False\n    if style.Get('ARITHMETIC_PRECEDENCE_INDICATION'):\n      if _PriorityIndicatingNoSpace(left) or _PriorityIndicatingNoSpace(right):\n        return False\n      else:\n        return True\n    else:\n      return True\n  if (_IsUnaryOperator(left) and lval != 'not' and\n      (right.is_name or right.is_number or rval == '(')):\n    # The previous token was a unary op. No space is desired between it and\n    # the current token.\n    return False\n  if (subtypes.DEFAULT_OR_NAMED_ASSIGN in left.subtypes and\n      subtypes.TYPED_NAME not in right.subtypes):\n    # A named argument or default parameter shouldn't have spaces around it.\n    return style.Get('SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN')\n  if (subtypes.DEFAULT_OR_NAMED_ASSIGN in right.subtypes and\n      subtypes.TYPED_NAME not in left.subtypes):\n    # A named argument or default parameter shouldn't have spaces around it.\n    return style.Get('SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN')\n  if (subtypes.VARARGS_LIST in left.subtypes or\n      subtypes.VARARGS_LIST in right.subtypes):\n    return False\n  if (subtypes.VARARGS_STAR in left.subtypes or\n      subtypes.KWARGS_STAR_STAR in left.subtypes):\n    # Don't add a space after a vararg's star or a keyword's star-star.\n    return False\n  if lval == '@' and subtypes.DECORATOR in left.subtypes:\n    # Decorators shouldn't be separated from the 'at' sign.\n    return False\n  if left.is_keyword and rval == '.':\n    # Add space between keywords and dots.\n    return lval not in {'None', 'print'}\n  if lval == '.' and right.is_keyword:\n    # Add space between keywords and dots.\n    return rval not in {'None', 'print'}\n  if lval == '.' or rval == '.':\n    # Don't place spaces between dots.\n    return False\n  if ((lval == '(' and rval == ')') or (lval == '[' and rval == ']') or\n      (lval == '{' and rval == '}')):\n    # Empty objects shouldn't be separated by spaces.\n    return False\n  if not is_line_disabled and (left.OpensScope() or right.ClosesScope()):\n    if (style.GetOrDefault('SPACES_AROUND_DICT_DELIMITERS', False) and (\n        (lval == '{' and _IsDictListTupleDelimiterTok(left, is_opening=True)) or\n        (rval == '}' and\n         _IsDictListTupleDelimiterTok(right, is_opening=False)))):\n      return True\n    if (style.GetOrDefault('SPACES_AROUND_LIST_DELIMITERS', False) and (\n        (lval == '[' and _IsDictListTupleDelimiterTok(left, is_opening=True)) or\n        (rval == ']' and\n         _IsDictListTupleDelimiterTok(right, is_opening=False)))):\n      return True\n    if (style.GetOrDefault('SPACES_AROUND_TUPLE_DELIMITERS', False) and (\n        (lval == '(' and _IsDictListTupleDelimiterTok(left, is_opening=True)) or\n        (rval == ')' and\n         _IsDictListTupleDelimiterTok(right, is_opening=False)))):\n      return True\n  if left.OpensScope() and right.OpensScope():\n    # Nested objects' opening brackets shouldn't be separated, unless enabled\n    # by SPACE_INSIDE_BRACKETS.\n    return style.Get('SPACE_INSIDE_BRACKETS')\n  if left.ClosesScope() and right.ClosesScope():\n    # Nested objects' closing brackets shouldn't be separated, unless enabled\n    # by SPACE_INSIDE_BRACKETS.\n    return style.Get('SPACE_INSIDE_BRACKETS')\n  if left.ClosesScope() and rval in '([':\n    # A call, set, dictionary, or subscript that has a call or subscript after\n    # it shouldn't have a space between them.\n    return False\n  if left.OpensScope() and _IsIdNumberStringToken(right):\n    # Don't separate the opening bracket from the first item, unless enabled\n    # by SPACE_INSIDE_BRACKETS.\n    return style.Get('SPACE_INSIDE_BRACKETS')\n  if left.is_name and rval in '([':\n    # Don't separate a call or array access from the name.\n    return False\n  if right.ClosesScope():\n    # Don't separate the closing bracket from the last item, unless enabled\n    # by SPACE_INSIDE_BRACKETS.\n    # FIXME(morbo): This might be too permissive.\n    return style.Get('SPACE_INSIDE_BRACKETS')\n  if lval == 'print' and rval == '(':\n    # Special support for the 'print' function.\n    return False\n  if left.OpensScope() and _IsUnaryOperator(right):\n    # Don't separate a unary operator from the opening bracket, unless enabled\n    # by SPACE_INSIDE_BRACKETS.\n    return style.Get('SPACE_INSIDE_BRACKETS')\n  if (left.OpensScope() and (subtypes.VARARGS_STAR in right.subtypes or\n                             subtypes.KWARGS_STAR_STAR in right.subtypes)):\n    # Don't separate a '*' or '**' from the opening bracket, unless enabled\n    # by SPACE_INSIDE_BRACKETS.\n    return style.Get('SPACE_INSIDE_BRACKETS')\n  if rval == ';':\n    # Avoid spaces before a semicolon. (Why is there a semicolon?!)\n    return False\n  if lval == '(' and rval == 'await':\n    # Special support for the 'await' keyword. Don't separate the 'await'\n    # keyword from an opening paren, unless enabled by SPACE_INSIDE_BRACKETS.\n    return style.Get('SPACE_INSIDE_BRACKETS')\n  return True\n\n\ndef _MustBreakBefore(prev_token, cur_token):\n  \"\"\"Return True if a line break is required before the current token.\"\"\"\n  if prev_token.is_comment or (prev_token.previous_token and\n                               prev_token.is_pseudo and\n                               prev_token.previous_token.is_comment):\n    # Must break if the previous token was a comment.\n    return True\n  if (cur_token.is_string and prev_token.is_string and\n      IsSurroundedByBrackets(cur_token)):\n    # We want consecutive strings to be on separate lines. This is a\n    # reasonable assumption, because otherwise they should have written them\n    # all on the same line, or with a '+'.\n    return True\n  return cur_token.must_break_before\n\n\ndef _CanBreakBefore(prev_token, cur_token):\n  \"\"\"Return True if a line break may occur before the current token.\"\"\"\n  pval = prev_token.value\n  cval = cur_token.value\n  if pval == 'yield' and cval == 'from':\n    # Don't break before a yield argument.\n    return False\n  if pval in {'async', 'await'} and cval in {'def', 'with', 'for'}:\n    # Don't break after sync keywords.\n    return False\n  if cur_token.split_penalty >= split_penalty.UNBREAKABLE:\n    return False\n  if pval == '@':\n    # Don't break right after the beginning of a decorator.\n    return False\n  if cval == ':':\n    # Don't break before the start of a block of code.\n    return False\n  if cval == ',':\n    # Don't break before a comma.\n    return False\n  if prev_token.is_name and cval == '(':\n    # Don't break in the middle of a function definition or call.\n    return False\n  if prev_token.is_name and cval == '[':\n    # Don't break in the middle of an array dereference.\n    return False\n  if cur_token.is_comment and prev_token.lineno == cur_token.lineno:\n    # Don't break a comment at the end of the line.\n    return False\n  if subtypes.UNARY_OPERATOR in prev_token.subtypes:\n    # Don't break after a unary token.\n    return False\n  if not style.Get('ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS'):\n    if (subtypes.DEFAULT_OR_NAMED_ASSIGN in cur_token.subtypes or\n        subtypes.DEFAULT_OR_NAMED_ASSIGN in prev_token.subtypes):\n      return False\n  return True\n\n\ndef IsSurroundedByBrackets(tok):\n  \"\"\"Return True if the token is surrounded by brackets.\"\"\"\n  paren_count = 0\n  brace_count = 0\n  sq_bracket_count = 0\n  previous_token = tok.previous_token\n  while previous_token:\n    if previous_token.value == ')':\n      paren_count -= 1\n    elif previous_token.value == '}':\n      brace_count -= 1\n    elif previous_token.value == ']':\n      sq_bracket_count -= 1\n\n    if previous_token.value == '(':\n      if paren_count == 0:\n        return previous_token\n      paren_count += 1\n    elif previous_token.value == '{':\n      if brace_count == 0:\n        return previous_token\n      brace_count += 1\n    elif previous_token.value == '[':\n      if sq_bracket_count == 0:\n        return previous_token\n      sq_bracket_count += 1\n\n    previous_token = previous_token.previous_token\n  return None\n\n\ndef _IsDictListTupleDelimiterTok(tok, is_opening):\n  assert tok\n\n  if tok.matching_bracket is None:\n    return False\n\n  if is_opening:\n    open_tok = tok\n    close_tok = tok.matching_bracket\n  else:\n    open_tok = tok.matching_bracket\n    close_tok = tok\n\n  # There must be something in between the tokens\n  if open_tok.next_token == close_tok:\n    return False\n\n  assert open_tok.next_token.node\n  assert open_tok.next_token.node.parent\n\n  return open_tok.next_token.node.parent.type in [\n      python_symbols.dictsetmaker,\n      python_symbols.listmaker,\n      python_symbols.testlist_gexp,\n  ]\n\n\n_LOGICAL_OPERATORS = frozenset({'and', 'or'})\n_BITWISE_OPERATORS = frozenset({'&', '|', '^'})\n_ARITHMETIC_OPERATORS = frozenset({'+', '-', '*', '/', '%', '//', '@'})\n\n\ndef _SplitPenalty(prev_token, cur_token):\n  \"\"\"Return the penalty for breaking the line before the current token.\"\"\"\n  pval = prev_token.value\n  cval = cur_token.value\n  if pval == 'not':\n    return split_penalty.UNBREAKABLE\n\n  if cur_token.node_split_penalty > 0:\n    return cur_token.node_split_penalty\n\n  if style.Get('SPLIT_BEFORE_LOGICAL_OPERATOR'):\n    # Prefer to split before 'and' and 'or'.\n    if pval in _LOGICAL_OPERATORS:\n      return style.Get('SPLIT_PENALTY_LOGICAL_OPERATOR')\n    if cval in _LOGICAL_OPERATORS:\n      return 0\n  else:\n    # Prefer to split after 'and' and 'or'.\n    if pval in _LOGICAL_OPERATORS:\n      return 0\n    if cval in _LOGICAL_OPERATORS:\n      return style.Get('SPLIT_PENALTY_LOGICAL_OPERATOR')\n\n  if style.Get('SPLIT_BEFORE_BITWISE_OPERATOR'):\n    # Prefer to split before '&', '|', and '^'.\n    if pval in _BITWISE_OPERATORS:\n      return style.Get('SPLIT_PENALTY_BITWISE_OPERATOR')\n    if cval in _BITWISE_OPERATORS:\n      return 0\n  else:\n    # Prefer to split after '&', '|', and '^'.\n    if pval in _BITWISE_OPERATORS:\n      return 0\n    if cval in _BITWISE_OPERATORS:\n      return style.Get('SPLIT_PENALTY_BITWISE_OPERATOR')\n\n  if (subtypes.COMP_FOR in cur_token.subtypes or\n      subtypes.COMP_IF in cur_token.subtypes):\n    # We don't mind breaking before the 'for' or 'if' of a list comprehension.\n    return 0\n  if subtypes.UNARY_OPERATOR in prev_token.subtypes:\n    # Try not to break after a unary operator.\n    return style.Get('SPLIT_PENALTY_AFTER_UNARY_OPERATOR')\n  if pval == ',':\n    # Breaking after a comma is fine, if need be.\n    return 0\n  if pval == '**' or cval == '**':\n    return split_penalty.STRONGLY_CONNECTED\n  if (subtypes.VARARGS_STAR in prev_token.subtypes or\n      subtypes.KWARGS_STAR_STAR in prev_token.subtypes):\n    # Don't split after a varargs * or kwargs **.\n    return split_penalty.UNBREAKABLE\n  if prev_token.OpensScope() and cval != '(':\n    # Slightly prefer\n    return style.Get('SPLIT_PENALTY_AFTER_OPENING_BRACKET')\n  if cval == ':':\n    # Don't split before a colon.\n    return split_penalty.UNBREAKABLE\n  if cval == '=':\n    # Don't split before an assignment.\n    return split_penalty.UNBREAKABLE\n  if (subtypes.DEFAULT_OR_NAMED_ASSIGN in prev_token.subtypes or\n      subtypes.DEFAULT_OR_NAMED_ASSIGN in cur_token.subtypes):\n    # Don't break before or after an default or named assignment.\n    return split_penalty.UNBREAKABLE\n  if cval == '==':\n    # We would rather not split before an equality operator.\n    return split_penalty.STRONGLY_CONNECTED\n  if cur_token.ClosesScope():\n    # Give a slight penalty for splitting before the closing scope.\n    return 100\n  return 0\n"
  },
  {
    "path": "yapf/yapflib/object_state.py",
    "content": "# Copyright 2017 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Represents the state of Python objects being formatted.\n\nObjects (e.g., list comprehensions, dictionaries, etc.) have specific\nrequirements on how they're formatted. These state objects keep track of these\nrequirements.\n\"\"\"\n\nfrom functools import lru_cache\n\nfrom yapf.yapflib import style\nfrom yapf.yapflib import subtypes\n\n\nclass ComprehensionState(object):\n  \"\"\"Maintains the state of list comprehension formatting decisions.\n\n  A stack of ComprehensionState objects are kept to ensure that list\n  comprehensions are wrapped with well-defined rules.\n\n  Attributes:\n    expr_token: The first token in the comprehension.\n    for_token: The first 'for' token of the comprehension.\n    opening_bracket: The opening bracket of the list comprehension.\n    closing_bracket: The closing bracket of the list comprehension.\n    has_split_at_for: Whether there is a newline immediately before the\n      for_token.\n    has_interior_split: Whether there is a newline within the comprehension.\n      That is, a split somewhere after expr_token or before closing_bracket.\n  \"\"\"\n\n  def __init__(self, expr_token):\n    self.expr_token = expr_token\n    self.for_token = None\n    self.has_split_at_for = False\n    self.has_interior_split = False\n\n  def HasTrivialExpr(self):\n    \"\"\"Returns whether the comp_expr is \"trivial\" i.e. is a single token.\"\"\"\n    return self.expr_token.next_token.value == 'for'\n\n  @property\n  def opening_bracket(self):\n    return self.expr_token.previous_token\n\n  @property\n  def closing_bracket(self):\n    return self.opening_bracket.matching_bracket\n\n  def Clone(self):\n    clone = ComprehensionState(self.expr_token)\n    clone.for_token = self.for_token\n    clone.has_split_at_for = self.has_split_at_for\n    clone.has_interior_split = self.has_interior_split\n    return clone\n\n  def __repr__(self):\n    return ('[opening_bracket::%s, for_token::%s, has_split_at_for::%s,'\n            ' has_interior_split::%s, has_trivial_expr::%s]' %\n            (self.opening_bracket, self.for_token, self.has_split_at_for,\n             self.has_interior_split, self.HasTrivialExpr()))\n\n  def __eq__(self, other):\n    return hash(self) == hash(other)\n\n  def __ne__(self, other):\n    return not self == other\n\n  def __hash__(self, *args, **kwargs):\n    return hash((self.expr_token, self.for_token, self.has_split_at_for,\n                 self.has_interior_split))\n\n\nclass ParameterListState(object):\n  \"\"\"Maintains the state of function parameter list formatting decisions.\n\n  Attributes:\n    opening_bracket: The opening bracket of the parameter list.\n    closing_bracket: The closing bracket of the parameter list.\n    has_typed_return: True if the function definition has a typed return.\n    ends_in_comma: True if the parameter list ends in a comma.\n    last_token: Returns the last token of the function declaration.\n    has_default_values: True if the parameters have default values.\n    has_split_before_first_param: Whether there is a newline before the first\n      parameter.\n    opening_column: The position of the opening parameter before a newline.\n    parameters: A list of parameter objects (Parameter).\n    split_before_closing_bracket: Split before the closing bracket. Sometimes\n      needed if the indentation would collide.\n  \"\"\"\n\n  def __init__(self, opening_bracket, newline, opening_column):\n    self.opening_bracket = opening_bracket\n    self.has_split_before_first_param = newline\n    self.opening_column = opening_column\n    self.parameters = opening_bracket.parameters\n    self.split_before_closing_bracket = False\n\n  @property\n  def closing_bracket(self):\n    return self.opening_bracket.matching_bracket\n\n  @property\n  def has_typed_return(self):\n    return self.closing_bracket.next_token.value == '->'\n\n  @property\n  @lru_cache()\n  def has_default_values(self):\n    return any(param.has_default_value for param in self.parameters)\n\n  @property\n  @lru_cache()\n  def ends_in_comma(self):\n    if not self.parameters:\n      return False\n    return self.parameters[-1].last_token.next_token.value == ','\n\n  @property\n  @lru_cache()\n  def last_token(self):\n    token = self.opening_bracket.matching_bracket\n    while not token.is_comment and token.next_token:\n      token = token.next_token\n    return token\n\n  @lru_cache()\n  def LastParamFitsOnLine(self, indent):\n    \"\"\"Return true if the last parameter fits on a single line.\"\"\"\n    if not self.has_typed_return:\n      return False\n    if not self.parameters:\n      return True\n    total_length = self.last_token.total_length\n    last_param = self.parameters[-1].first_token\n    total_length -= last_param.total_length - len(last_param.value)\n    return total_length + indent <= style.Get('COLUMN_LIMIT')\n\n  @lru_cache()\n  def SplitBeforeClosingBracket(self, indent):\n    \"\"\"Return true if there's a split before the closing bracket.\"\"\"\n    if style.Get('DEDENT_CLOSING_BRACKETS'):\n      return True\n    if self.ends_in_comma:\n      return True\n    if not self.parameters:\n      return False\n    total_length = self.last_token.total_length\n    last_param = self.parameters[-1].first_token\n    total_length -= last_param.total_length - len(last_param.value)\n    return total_length + indent > style.Get('COLUMN_LIMIT')\n\n  def Clone(self):\n    clone = ParameterListState(self.opening_bracket,\n                               self.has_split_before_first_param,\n                               self.opening_column)\n    clone.split_before_closing_bracket = self.split_before_closing_bracket\n    clone.parameters = [param.Clone() for param in self.parameters]\n    return clone\n\n  def __repr__(self):\n    return ('[opening_bracket::%s, has_split_before_first_param::%s, '\n            'opening_column::%d]' %\n            (self.opening_bracket, self.has_split_before_first_param,\n             self.opening_column))\n\n  def __eq__(self, other):\n    return hash(self) == hash(other)\n\n  def __ne__(self, other):\n    return not self == other\n\n  def __hash__(self, *args, **kwargs):\n    return hash(\n        (self.opening_bracket, self.has_split_before_first_param,\n         self.opening_column, (hash(param) for param in self.parameters)))\n\n\nclass Parameter(object):\n  \"\"\"A parameter in a parameter list.\n\n  Attributes:\n    first_token: (format_token.FormatToken) First token of parameter.\n    last_token: (format_token.FormatToken) Last token of parameter.\n    has_default_value: (boolean) True if the parameter has a default value\n  \"\"\"\n\n  def __init__(self, first_token, last_token):\n    self.first_token = first_token\n    self.last_token = last_token\n\n  @property\n  @lru_cache()\n  def has_default_value(self):\n    \"\"\"Returns true if the parameter has a default value.\"\"\"\n    tok = self.first_token\n    while tok != self.last_token:\n      if subtypes.DEFAULT_OR_NAMED_ASSIGN in tok.subtypes:\n        return True\n      tok = tok.matching_bracket if tok.OpensScope() else tok.next_token\n    return False\n\n  def Clone(self):\n    return Parameter(self.first_token, self.last_token)\n\n  def __repr__(self):\n    return '[first_token::%s, last_token:%s]' % (self.first_token,\n                                                 self.last_token)\n\n  def __eq__(self, other):\n    return hash(self) == hash(other)\n\n  def __ne__(self, other):\n    return not self == other\n\n  def __hash__(self, *args, **kwargs):\n    return hash((self.first_token, self.last_token))\n"
  },
  {
    "path": "yapf/yapflib/reformatter.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Decide what the format for the code should be.\n\nThe `logical_line.LogicalLine`s are now ready to be formatted. LogicalLInes that\ncan be merged together are. The best formatting is returned as a string.\n\n  Reformat(): the main function exported by this module.\n\"\"\"\n\nimport collections\nimport heapq\nimport re\n\nfrom yapf_third_party._ylib2to3 import pytree\nfrom yapf_third_party._ylib2to3.pgen2 import token\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.yapflib import format_decision_state\nfrom yapf.yapflib import format_token\nfrom yapf.yapflib import line_joiner\nfrom yapf.yapflib import style\n\n\ndef Reformat(llines, lines=None):\n  \"\"\"Reformat the logical lines.\n\n  Arguments:\n    llines: (list of logical_line.LogicalLine) Lines we want to format.\n    lines: (set of int) The lines which can be modified or None if there is no\n      line range restriction.\n\n  Returns:\n    A string representing the reformatted code.\n  \"\"\"\n  final_lines = []\n  prev_line = None  # The previous line.\n  indent_width = style.Get('INDENT_WIDTH')\n\n  for lline in _SingleOrMergedLines(llines):\n    first_token = lline.first\n    _FormatFirstToken(first_token, lline.depth, prev_line, final_lines)\n\n    indent_amt = indent_width * lline.depth\n    state = format_decision_state.FormatDecisionState(lline, indent_amt)\n    state.MoveStateToNextToken()\n\n    if not lline.disable:\n      if lline.first.is_comment:\n        lline.first.value = lline.first.value.rstrip()\n      elif lline.last.is_comment:\n        lline.last.value = lline.last.value.rstrip()\n      if prev_line and prev_line.disable:\n        # Keep the vertical spacing between a disabled and enabled formatting\n        # region.\n        _RetainRequiredVerticalSpacingBetweenTokens(lline.first, prev_line.last,\n                                                    lines)\n      if any(tok.is_comment for tok in lline.tokens):\n        _RetainVerticalSpacingBeforeComments(lline)\n\n    if lline.disable or _LineHasContinuationMarkers(lline):\n      _RetainHorizontalSpacing(lline)\n      _RetainRequiredVerticalSpacing(lline, prev_line, lines)\n      _EmitLineUnformatted(state)\n\n    elif (_LineContainsPylintDisableLineTooLong(lline) or\n          _LineContainsI18n(lline)):\n      # Don't modify vertical spacing, but fix any horizontal spacing issues.\n      _RetainRequiredVerticalSpacing(lline, prev_line, lines)\n      _EmitLineUnformatted(state)\n\n    elif _CanPlaceOnSingleLine(lline) and not any(tok.must_break_before\n                                                  for tok in lline.tokens):\n      # The logical line fits on one line.\n      while state.next_token:\n        state.AddTokenToState(newline=False, dry_run=False)\n\n    elif not _AnalyzeSolutionSpace(state):\n      # Failsafe mode. If there isn't a solution to the line, then just emit\n      # it as is.\n      state = format_decision_state.FormatDecisionState(lline, indent_amt)\n      state.MoveStateToNextToken()\n      _RetainHorizontalSpacing(lline)\n      _RetainRequiredVerticalSpacing(lline, prev_line, None)\n      _EmitLineUnformatted(state)\n\n    final_lines.append(lline)\n    prev_line = lline\n\n  _AlignTrailingComments(final_lines)\n  return _FormatFinalLines(final_lines)\n\n\ndef _RetainHorizontalSpacing(line):\n  \"\"\"Retain all horizontal spacing between tokens.\"\"\"\n  for tok in line.tokens:\n    tok.RetainHorizontalSpacing(line.first.column, line.depth)\n\n\ndef _RetainRequiredVerticalSpacing(cur_line, prev_line, lines):\n  \"\"\"Retain all vertical spacing between lines.\"\"\"\n  prev_tok = None\n  if prev_line is not None:\n    prev_tok = prev_line.last\n\n  if cur_line.disable:\n    # After the first token we are acting on a single line. So if it is\n    # disabled we must not reformat.\n    lines = set()\n\n  for cur_tok in cur_line.tokens:\n    _RetainRequiredVerticalSpacingBetweenTokens(cur_tok, prev_tok, lines)\n    prev_tok = cur_tok\n\n\ndef _RetainRequiredVerticalSpacingBetweenTokens(cur_tok, prev_tok, lines):\n  \"\"\"Retain vertical spacing between two tokens if not in editable range.\"\"\"\n  if prev_tok is None:\n    return\n\n  if prev_tok.is_string:\n    prev_lineno = prev_tok.lineno + prev_tok.value.count('\\n')\n  elif prev_tok.is_pseudo:\n    if not prev_tok.previous_token.is_multiline_string:\n      prev_lineno = prev_tok.previous_token.lineno\n    else:\n      prev_lineno = prev_tok.lineno\n  else:\n    prev_lineno = prev_tok.lineno\n\n  if cur_tok.is_comment:\n    cur_lineno = cur_tok.lineno - cur_tok.value.count('\\n')\n  else:\n    cur_lineno = cur_tok.lineno\n\n  if not prev_tok.is_comment and prev_tok.value.endswith('\\\\'):\n    prev_lineno += prev_tok.value.count('\\n')\n\n  required_newlines = cur_lineno - prev_lineno\n  if cur_tok.is_comment and not prev_tok.is_comment:\n    # Don't adjust between a comment and non-comment.\n    pass\n  elif lines and lines.intersection(range(prev_lineno, cur_lineno + 1)):\n    desired_newlines = cur_tok.whitespace_prefix.count('\\n')\n    whitespace_lines = range(prev_lineno + 1, cur_lineno)\n    deletable_lines = len(lines.intersection(whitespace_lines))\n    required_newlines = max(required_newlines - deletable_lines,\n                            desired_newlines)\n\n  cur_tok.AdjustNewlinesBefore(required_newlines)\n\n\ndef _RetainVerticalSpacingBeforeComments(line):\n  \"\"\"Retain vertical spacing before comments.\"\"\"\n  prev_token = None\n  for tok in line.tokens:\n    if tok.is_comment and prev_token:\n      if tok.lineno - tok.value.count('\\n') - prev_token.lineno > 1:\n        tok.AdjustNewlinesBefore(ONE_BLANK_LINE)\n\n    prev_token = tok\n\n\ndef _EmitLineUnformatted(state):\n  \"\"\"Emit the line without formatting.\n\n  The line contains code that if reformatted would break a non-syntactic\n  convention. E.g., i18n comments and function calls are tightly bound by\n  convention. Instead, we calculate when / if a newline should occur and honor\n  that. But otherwise the code emitted will be the same as the original code.\n\n  Arguments:\n    state: (format_decision_state.FormatDecisionState) The format decision\n      state.\n  \"\"\"\n  while state.next_token:\n    previous_token = state.next_token.previous_token\n    previous_lineno = previous_token.lineno\n\n    if previous_token.is_multiline_string or previous_token.is_string:\n      previous_lineno += previous_token.value.count('\\n')\n\n    if previous_token.is_continuation:\n      newline = False\n    else:\n      newline = state.next_token.lineno > previous_lineno\n\n    state.AddTokenToState(newline=newline, dry_run=False)\n\n\ndef _LineContainsI18n(line):\n  \"\"\"Return true if there are i18n comments or function calls in the line.\n\n  I18n comments and pseudo-function calls are closely related. They cannot\n  be moved apart without breaking i18n.\n\n  Arguments:\n    line: (logical_line.LogicalLine) The line currently being formatted.\n\n  Returns:\n    True if the line contains i18n comments or function calls. False otherwise.\n  \"\"\"\n  if style.Get('I18N_COMMENT'):\n    for tok in line.tokens:\n      if tok.is_comment and re.match(style.Get('I18N_COMMENT'), tok.value):\n        # Contains an i18n comment.\n        return True\n\n  if style.Get('I18N_FUNCTION_CALL'):\n    length = len(line.tokens)\n    for index in range(length - 1):\n      if (line.tokens[index + 1].value == '(' and\n          line.tokens[index].value in style.Get('I18N_FUNCTION_CALL')):\n        return True\n  return False\n\n\ndef _LineContainsPylintDisableLineTooLong(line):\n  \"\"\"Return true if there is a \"pylint: disable=line-too-long\" comment.\"\"\"\n  return re.search(r'\\bpylint:\\s+disable=line-too-long\\b', line.last.value)\n\n\ndef _LineHasContinuationMarkers(line):\n  \"\"\"Return true if the line has continuation markers in it.\"\"\"\n  return any(tok.is_continuation for tok in line.tokens)\n\n\ndef _CanPlaceOnSingleLine(line):\n  \"\"\"Determine if the logical line can go on a single line.\n\n  Arguments:\n    line: (logical_line.LogicalLine) The line currently being formatted.\n\n  Returns:\n    True if the line can or should be added to a single line. False otherwise.\n  \"\"\"\n  token_types = [x.type for x in line.tokens]\n  if (style.Get('SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED') and\n      any(token_types[token_index - 1] == token.COMMA\n          for token_index, token_type in enumerate(token_types[1:], start=1)\n          if token_type == token.RPAR)):\n    return False\n  if (style.Get('FORCE_MULTILINE_DICT') and token.LBRACE in token_types):\n    return False\n  indent_amt = style.Get('INDENT_WIDTH') * line.depth\n  last = line.last\n  last_index = -1\n  if (last.is_pylint_comment or last.is_pytype_comment or\n      last.is_copybara_comment):\n    last = last.previous_token\n    last_index = -2\n  if last is None:\n    return True\n  return (last.total_length + indent_amt <= style.Get('COLUMN_LIMIT') and\n          not any(tok.is_comment for tok in line.tokens[:last_index]))\n\n\ndef _AlignTrailingComments(final_lines):\n  \"\"\"Align trailing comments to the same column.\"\"\"\n  final_lines_index = 0\n  while final_lines_index < len(final_lines):\n    line = final_lines[final_lines_index]\n    assert line.tokens\n\n    processed_content = False\n\n    for tok in line.tokens:\n      if (tok.is_comment and isinstance(tok.spaces_required_before, list) and\n          tok.value.startswith('#')):\n        # All trailing comments and comments that appear on a line by themselves\n        # in this block should be indented at the same level. The block is\n        # terminated by an empty line or EOF. Enumerate through each line in\n        # the block and calculate the max line length. Once complete, use the\n        # first col value greater than that value and create the necessary for\n        # each line accordingly.\n        all_pc_line_lengths = []  # All pre-comment line lengths\n        max_line_length = 0\n\n        while True:\n          # EOF\n          if final_lines_index + len(all_pc_line_lengths) == len(final_lines):\n            break\n\n          this_line = final_lines[final_lines_index + len(all_pc_line_lengths)]\n\n          # Blank line - note that content is preformatted so we don't need to\n          # worry about spaces/tabs; a blank line will always be '\\n\\n'.\n          assert this_line.tokens\n          if (all_pc_line_lengths and\n              this_line.tokens[0].formatted_whitespace_prefix.startswith('\\n\\n')\n             ):\n            break\n\n          if this_line.disable:\n            all_pc_line_lengths.append([])\n            continue\n\n          # Calculate the length of each line in this logical line.\n          line_content = ''\n          pc_line_lengths = []\n\n          for line_tok in this_line.tokens:\n            whitespace_prefix = line_tok.formatted_whitespace_prefix\n\n            newline_index = whitespace_prefix.rfind('\\n')\n            if newline_index != -1:\n              max_line_length = max(max_line_length, len(line_content))\n              line_content = ''\n\n              whitespace_prefix = whitespace_prefix[newline_index + 1:]\n\n            if line_tok.is_comment:\n              pc_line_lengths.append(len(line_content))\n            else:\n              line_content += '{}{}'.format(whitespace_prefix, line_tok.value)\n\n          if pc_line_lengths:\n            max_line_length = max(max_line_length, max(pc_line_lengths))\n\n          all_pc_line_lengths.append(pc_line_lengths)\n\n        # Calculate the aligned column value\n        max_line_length += 2\n\n        aligned_col = None\n        for potential_col in tok.spaces_required_before:\n          if potential_col > max_line_length:\n            aligned_col = potential_col\n            break\n\n        if aligned_col is None:\n          aligned_col = max_line_length\n\n        # Update the comment token values based on the aligned values\n        for all_pc_line_lengths_index, pc_line_lengths in enumerate(\n            all_pc_line_lengths):\n          if not pc_line_lengths:\n            continue\n\n          this_line = final_lines[final_lines_index + all_pc_line_lengths_index]\n\n          pc_line_length_index = 0\n          for line_tok in this_line.tokens:\n            if line_tok.is_comment:\n              assert pc_line_length_index < len(pc_line_lengths)\n              assert pc_line_lengths[pc_line_length_index] < aligned_col\n\n              # Note that there may be newlines embedded in the comments, so\n              # we need to apply a whitespace prefix to each line.\n              whitespace = ' ' * (\n                  aligned_col - pc_line_lengths[pc_line_length_index] - 1)\n              pc_line_length_index += 1\n\n              line_content = []\n\n              for comment_line_index, comment_line in enumerate(\n                  line_tok.value.split('\\n')):\n                line_content.append('{}{}'.format(whitespace,\n                                                  comment_line.strip()))\n\n                if comment_line_index == 0:\n                  whitespace = ' ' * (aligned_col - 1)\n\n              line_content = '\\n'.join(line_content)\n\n              # Account for initial whitespace already slated for the\n              # beginning of the line.\n              existing_whitespace_prefix = \\\n                line_tok.formatted_whitespace_prefix.lstrip('\\n')\n\n              if line_content.startswith(existing_whitespace_prefix):\n                line_content = line_content[len(existing_whitespace_prefix):]\n\n              line_tok.value = line_content\n\n          assert pc_line_length_index == len(pc_line_lengths)\n\n        final_lines_index += len(all_pc_line_lengths)\n\n        processed_content = True\n        break\n\n    if not processed_content:\n      final_lines_index += 1\n\n\ndef _FormatFinalLines(final_lines):\n  \"\"\"Compose the final output from the finalized lines.\"\"\"\n  formatted_code = []\n  for line in final_lines:\n    formatted_line = []\n    for tok in line.tokens:\n      if not tok.is_pseudo:\n        formatted_line.append(tok.formatted_whitespace_prefix)\n        formatted_line.append(tok.value)\n      elif (not tok.next_token.whitespace_prefix.startswith('\\n') and\n            not tok.next_token.whitespace_prefix.startswith(' ')):\n        if (tok.previous_token.value == ':' or\n            tok.next_token.value not in ',}])'):\n          formatted_line.append(' ')\n\n    formatted_code.append(''.join(formatted_line))\n\n  return ''.join(formatted_code) + '\\n'\n\n\nclass _StateNode(object):\n  \"\"\"An edge in the solution space from 'previous.state' to 'state'.\n\n  Attributes:\n    state: (format_decision_state.FormatDecisionState) The format decision state\n      for this node.\n    newline: If True, then on the edge from 'previous.state' to 'state' a\n      newline is inserted.\n    previous: (_StateNode) The previous state node in the graph.\n  \"\"\"\n\n  # TODO(morbo): Add a '__cmp__' method.\n\n  def __init__(self, state, newline, previous):\n    self.state = state.Clone()\n    self.newline = newline\n    self.previous = previous\n\n  def __repr__(self):  # pragma: no cover\n    return 'StateNode(state=[\\n{0}\\n], newline={1})'.format(\n        self.state, self.newline)\n\n\n# A tuple of (penalty, count) that is used to prioritize the BFS. In case of\n# equal penalties, we prefer states that were inserted first. During state\n# generation, we make sure that we insert states first that break the line as\n# late as possible.\n_OrderedPenalty = collections.namedtuple('OrderedPenalty', ['penalty', 'count'])\n\n# An item in the prioritized BFS search queue. The 'StateNode's 'state' has\n# the given '_OrderedPenalty'.\n_QueueItem = collections.namedtuple('QueueItem',\n                                    ['ordered_penalty', 'state_node'])\n\n\ndef _AnalyzeSolutionSpace(initial_state):\n  \"\"\"Analyze the entire solution space starting from initial_state.\n\n  This implements a variant of Dijkstra's algorithm on the graph that spans\n  the solution space (LineStates are the nodes). The algorithm tries to find\n  the shortest path (the one with the lowest penalty) from 'initial_state' to\n  the state where all tokens are placed.\n\n  Arguments:\n    initial_state: (format_decision_state.FormatDecisionState) The initial state\n      to start the search from.\n\n  Returns:\n    True if a formatting solution was found. False otherwise.\n  \"\"\"\n  count = 0\n  seen = set()\n  p_queue = []\n\n  # Insert start element.\n  node = _StateNode(initial_state, False, None)\n  heapq.heappush(p_queue, _QueueItem(_OrderedPenalty(0, count), node))\n\n  count += 1\n  while p_queue:\n    item = p_queue[0]\n    penalty = item.ordered_penalty.penalty\n    node = item.state_node\n    if not node.state.next_token:\n      break\n    heapq.heappop(p_queue)\n\n    if count > 10000:\n      node.state.ignore_stack_for_comparison = True\n\n    # Unconditionally add the state and check if it was present to avoid having\n    # to hash it twice in the common case (state hashing is expensive).\n    before_seen_count = len(seen)\n    seen.add(node.state)\n    # If seen didn't change size, the state was already present.\n    if before_seen_count == len(seen):\n      continue\n\n    # FIXME(morbo): Add a 'decision' element?\n\n    count = _AddNextStateToQueue(penalty, node, False, count, p_queue)\n    count = _AddNextStateToQueue(penalty, node, True, count, p_queue)\n\n  if not p_queue:\n    # We weren't able to find a solution. Do nothing.\n    return False\n\n  _ReconstructPath(initial_state, heapq.heappop(p_queue).state_node)\n  return True\n\n\ndef _AddNextStateToQueue(penalty, previous_node, newline, count, p_queue):\n  \"\"\"Add the following state to the analysis queue.\n\n  Assume the current state is 'previous_node' and has been reached with a\n  penalty of 'penalty'. Insert a line break if 'newline' is True.\n\n  Arguments:\n    penalty: (int) The penalty associated with the path up to this point.\n    previous_node: (_StateNode) The last _StateNode inserted into the priority\n      queue.\n    newline: (bool) Add a newline if True.\n    count: (int) The number of elements in the queue.\n    p_queue: (heapq) The priority queue representing the solution space.\n\n  Returns:\n    The updated number of elements in the queue.\n  \"\"\"\n  must_split = previous_node.state.MustSplit()\n  if newline and not previous_node.state.CanSplit(must_split):\n    # Don't add a newline if the token cannot be split.\n    return count\n  if not newline and must_split:\n    # Don't add a token we must split but where we aren't splitting.\n    return count\n\n  node = _StateNode(previous_node.state, newline, previous_node)\n  penalty += node.state.AddTokenToState(\n      newline=newline, dry_run=True, must_split=must_split)\n  heapq.heappush(p_queue, _QueueItem(_OrderedPenalty(penalty, count), node))\n  return count + 1\n\n\ndef _ReconstructPath(initial_state, current):\n  \"\"\"Reconstruct the path through the queue with lowest penalty.\n\n  Arguments:\n    initial_state: (format_decision_state.FormatDecisionState) The initial state\n      to start the search from.\n    current: (_StateNode) The node in the decision graph that is the end point\n      of the path with the least penalty.\n  \"\"\"\n  path = collections.deque()\n\n  while current.previous:\n    path.appendleft(current)\n    current = current.previous\n\n  for node in path:\n    initial_state.AddTokenToState(newline=node.newline, dry_run=False)\n\n\nNESTED_DEPTH = []\n\n\ndef _FormatFirstToken(first_token, indent_depth, prev_line, final_lines):\n  \"\"\"Format the first token in the logical line.\n\n  Add a newline and the required indent before the first token of the logical\n  line.\n\n  Arguments:\n    first_token: (format_token.FormatToken) The first token in the logical line.\n    indent_depth: (int) The line's indentation depth.\n    prev_line: (list of logical_line.LogicalLine) The logical line previous to\n      this line.\n    final_lines: (list of logical_line.LogicalLine) The logical lines that have\n      already been processed.\n  \"\"\"\n  global NESTED_DEPTH\n  while NESTED_DEPTH and NESTED_DEPTH[-1] > indent_depth:\n    NESTED_DEPTH.pop()\n\n  first_nested = False\n  if _IsClassOrDef(first_token):\n    if not NESTED_DEPTH:\n      NESTED_DEPTH = [indent_depth]\n    elif NESTED_DEPTH[-1] < indent_depth:\n      first_nested = True\n      NESTED_DEPTH.append(indent_depth)\n\n  first_token.AddWhitespacePrefix(\n      _CalculateNumberOfNewlines(first_token, indent_depth, prev_line,\n                                 final_lines, first_nested),\n      indent_level=indent_depth)\n\n\nNO_BLANK_LINES = 1\nONE_BLANK_LINE = 2\nTWO_BLANK_LINES = 3\n\n\ndef _IsClassOrDef(tok):\n  if tok.value in {'class', 'def', '@'}:\n    return True\n  return (tok.next_token and tok.value == 'async' and\n          tok.next_token.value == 'def')\n\n\ndef _CalculateNumberOfNewlines(first_token, indent_depth, prev_line,\n                               final_lines, first_nested):\n  \"\"\"Calculate the number of newlines we need to add.\n\n  Arguments:\n    first_token: (format_token.FormatToken) The first token in the logical\n      line.\n    indent_depth: (int) The line's indentation depth.\n    prev_line: (list of logical_line.LogicalLine) The logical line previous to\n      this line.\n    final_lines: (list of logical_line.LogicalLine) The logical lines that have\n      already been processed.\n    first_nested: (boolean) Whether this is the first nested class or function.\n\n  Returns:\n    The number of newlines needed before the first token.\n  \"\"\"\n  # TODO(morbo): Special handling for imports.\n  # TODO(morbo): Create a knob that can tune these.\n  if prev_line is None:\n    # The first line in the file. Don't add blank lines.\n    # FIXME(morbo): Is this correct?\n    if first_token.newlines is not None:\n      first_token.newlines = None\n    return 0\n\n  if first_token.is_docstring:\n    if (prev_line.first.value == 'class' and\n        style.Get('BLANK_LINE_BEFORE_CLASS_DOCSTRING')):\n      # Enforce a blank line before a class's docstring.\n      return ONE_BLANK_LINE\n    elif (prev_line.first.value.startswith('#') and\n          style.Get('BLANK_LINE_BEFORE_MODULE_DOCSTRING')):\n      # Enforce a blank line before a module's docstring.\n      return ONE_BLANK_LINE\n    # The docstring shouldn't have a newline before it.\n    return NO_BLANK_LINES\n\n  if first_token.is_name and not indent_depth:\n    if prev_line.first.value in {'from', 'import'}:\n      # Support custom number of blank lines between top-level imports and\n      # variable definitions.\n      return 1 + style.Get(\n          'BLANK_LINES_BETWEEN_TOP_LEVEL_IMPORTS_AND_VARIABLES')\n\n  prev_last_token = prev_line.last\n  if prev_last_token.is_docstring:\n    if (not indent_depth and first_token.value in {'class', 'def', 'async'}):\n      # Separate a class or function from the module-level docstring with\n      # appropriate number of blank lines.\n      return 1 + style.Get('BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION')\n    if (first_nested and\n        not style.Get('BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF') and\n        _IsClassOrDef(first_token)):\n      first_token.newlines = None\n      return NO_BLANK_LINES\n    if _NoBlankLinesBeforeCurrentToken(prev_last_token.value, first_token,\n                                       prev_last_token):\n      return NO_BLANK_LINES\n    else:\n      return ONE_BLANK_LINE\n\n  if _IsClassOrDef(first_token):\n    # TODO(morbo): This can go once the blank line calculator is more\n    # sophisticated.\n    if not indent_depth:\n      # This is a top-level class or function.\n      is_inline_comment = prev_last_token.whitespace_prefix.count('\\n') == 0\n      if (not prev_line.disable and prev_last_token.is_comment and\n          not is_inline_comment):\n        # This token follows a non-inline comment.\n        if _NoBlankLinesBeforeCurrentToken(prev_last_token.value, first_token,\n                                           prev_last_token):\n          # Assume that the comment is \"attached\" to the current line.\n          # Therefore, we want two blank lines before the comment.\n          index = len(final_lines) - 1\n          while index > 0:\n            if not final_lines[index - 1].is_comment:\n              break\n            index -= 1\n          if final_lines[index - 1].first.value == '@':\n            final_lines[index].first.AdjustNewlinesBefore(NO_BLANK_LINES)\n          else:\n            prev_last_token.AdjustNewlinesBefore(\n                1 + style.Get('BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION'))\n          if first_token.newlines is not None:\n            first_token.newlines = None\n          return NO_BLANK_LINES\n    elif _IsClassOrDef(prev_line.first):\n      if first_nested and not style.Get(\n          'BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF'):\n        first_token.newlines = None\n        return NO_BLANK_LINES\n\n  # Calculate how many newlines were between the original lines. We want to\n  # retain that formatting if it doesn't violate one of the style guide rules.\n  if first_token.is_comment:\n    first_token_lineno = first_token.lineno - first_token.value.count('\\n')\n  else:\n    first_token_lineno = first_token.lineno\n\n  prev_last_token_lineno = prev_last_token.lineno\n  if prev_last_token.is_multiline_string:\n    prev_last_token_lineno += prev_last_token.value.count('\\n')\n\n  if first_token_lineno - prev_last_token_lineno > 1:\n    return ONE_BLANK_LINE\n\n  return NO_BLANK_LINES\n\n\ndef _SingleOrMergedLines(lines):\n  \"\"\"Generate the lines we want to format.\n\n  Arguments:\n    lines: (list of logical_line.LogicalLine) Lines we want to format.\n\n  Yields:\n    Either a single line, if the current line cannot be merged with the\n    succeeding line, or the next two lines merged into one line.\n  \"\"\"\n  index = 0\n  last_was_merged = False\n  while index < len(lines):\n    if lines[index].disable:\n      line = lines[index]\n      index += 1\n      while index < len(lines):\n        column = line.last.column + 2\n        if lines[index].lineno != line.lineno:\n          break\n        if line.last.value != ':':\n          leaf = pytree.Leaf(\n              type=token.SEMI, value=';', context=('', (line.lineno, column)))\n          line.AppendToken(\n              format_token.FormatToken(leaf, pytree_utils.NodeName(leaf)))\n        for tok in lines[index].tokens:\n          line.AppendToken(tok)\n        index += 1\n      yield line\n    elif line_joiner.CanMergeMultipleLines(lines[index:], last_was_merged):\n      # TODO(morbo): This splice is potentially very slow. Come up with a more\n      # performance-friendly way of determining if two lines can be merged.\n      next_line = lines[index + 1]\n      for tok in next_line.tokens:\n        lines[index].AppendToken(tok)\n      if (len(next_line.tokens) == 1 and next_line.first.is_multiline_string):\n        # This may be a multiline shebang. In that case, we want to retain the\n        # formatting. Otherwise, it could mess up the shell script's syntax.\n        lines[index].disable = True\n      yield lines[index]\n      index += 2\n      last_was_merged = True\n    else:\n      yield lines[index]\n      index += 1\n      last_was_merged = False\n\n\ndef _NoBlankLinesBeforeCurrentToken(text, cur_token, prev_token):\n  \"\"\"Determine if there are no blank lines before the current token.\n\n  The previous token is a docstring or comment. The prev_token_lineno is the\n  start of the text of that token. Counting the number of newlines in its text\n  gives us the extent and thus where the line number of the end of the\n  docstring or comment. After that, we just compare it to the current token's\n  line number to see if there are blank lines between them.\n\n  Arguments:\n    text: (unicode) The text of the docstring or comment before the current\n      token.\n    cur_token: (format_token.FormatToken) The current token in the logical line.\n    prev_token: (format_token.FormatToken) The previous token in the logical\n      line.\n\n  Returns:\n    True if there is no blank line before the current token.\n  \"\"\"\n  cur_token_lineno = cur_token.lineno\n  if cur_token.is_comment:\n    cur_token_lineno -= cur_token.value.count('\\n')\n  num_newlines = text.count('\\n') if not prev_token.is_comment else 0\n  return prev_token.lineno + num_newlines == cur_token_lineno - 1\n"
  },
  {
    "path": "yapf/yapflib/split_penalty.py",
    "content": "# Copyright 2022 Bill Wendling, All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom yapf.yapflib import style\n\n# Generic split penalties\nUNBREAKABLE = 1000**5\nVERY_STRONGLY_CONNECTED = 5000\nSTRONGLY_CONNECTED = 2500\n\n#############################################################################\n# Grammar-specific penalties - should be <= 1000                            #\n#############################################################################\n\n# Lambdas shouldn't be split unless absolutely necessary or if\n# ALLOW_MULTILINE_LAMBDAS is True.\nLAMBDA = 1000\nMULTILINE_LAMBDA = 500\n\nANNOTATION = 100\nARGUMENT = 25\n\n# TODO: Assign real values.\nRETURN_TYPE = 1\nDOTTED_NAME = 40\nEXPR = 10\nDICT_KEY_EXPR = 20\nDICT_VALUE_EXPR = 11\n"
  },
  {
    "path": "yapf/yapflib/style.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Python formatting style settings.\"\"\"\n\nimport os\nimport re\nimport sys\nimport textwrap\nfrom configparser import ConfigParser\n\nfrom yapf.yapflib import errors\n\nif sys.version_info >= (3, 11):\n  import tomllib\nelse:\n  import tomli as tomllib\n\n\nclass StyleConfigError(errors.YapfError):\n  \"\"\"Raised when there's a problem reading the style configuration.\"\"\"\n  pass\n\n\ndef Get(setting_name):\n  \"\"\"Get a style setting.\"\"\"\n  return _style[setting_name]\n\n\ndef GetOrDefault(setting_name, default_value):\n  \"\"\"Get a style setting or default value if the setting does not exist.\"\"\"\n  return _style.get(setting_name, default_value)\n\n\ndef Help():\n  \"\"\"Return dict mapping style names to help strings.\"\"\"\n  return _STYLE_HELP\n\n\ndef SetGlobalStyle(style):\n  \"\"\"Set a style dict.\"\"\"\n  global _style\n  global _GLOBAL_STYLE_FACTORY\n  factory = _GetStyleFactory(style)\n  if factory:\n    _GLOBAL_STYLE_FACTORY = factory\n  _style = style\n\n\n_STYLE_HELP = dict(\n    # BASED_ON_STYLE='Which predefined style this style is based on',\n    ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT=textwrap.dedent(\"\"\"\\\n      Align closing bracket with visual indentation.\n    \"\"\"),\n    ALLOW_MULTILINE_DICTIONARY_KEYS=textwrap.dedent(\"\"\"\\\n      Allow dictionary keys to exist on multiple lines. For example:\n\n        x = {\n            ('this is the first element of a tuple',\n             'this is the second element of a tuple'):\n                 value,\n        }\n    \"\"\"),\n    ALLOW_MULTILINE_LAMBDAS=textwrap.dedent(\"\"\"\\\n      Allow lambdas to be formatted on more than one line.\n    \"\"\"),\n    ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS=textwrap.dedent(\"\"\"\\\n      Allow splitting before a default / named assignment in an argument list.\n    \"\"\"),\n    ALLOW_SPLIT_BEFORE_DICT_VALUE=textwrap.dedent(\"\"\"\\\n      Allow splits before the dictionary value.\n    \"\"\"),\n    ARITHMETIC_PRECEDENCE_INDICATION=textwrap.dedent(\"\"\"\\\n      Let spacing indicate operator precedence. For example:\n\n        a = 1 * 2 + 3 / 4\n        b = 1 / 2 - 3 * 4\n        c = (1 + 2) * (3 - 4)\n        d = (1 - 2) / (3 + 4)\n        e = 1 * 2 - 3\n        f = 1 + 2 + 3 + 4\n\n    will be formatted as follows to indicate precedence:\n\n        a = 1*2 + 3/4\n        b = 1/2 - 3*4\n        c = (1+2) * (3-4)\n        d = (1-2) / (3+4)\n        e = 1*2 - 3\n        f = 1 + 2 + 3 + 4\n\n    \"\"\"),\n    BLANK_LINE_BEFORE_CLASS_DOCSTRING=textwrap.dedent(\"\"\"\\\n      Insert a blank line before a class-level docstring.\n    \"\"\"),\n    BLANK_LINE_BEFORE_MODULE_DOCSTRING=textwrap.dedent(\"\"\"\\\n      Insert a blank line before a module docstring.\n    \"\"\"),\n    BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=textwrap.dedent(\"\"\"\\\n      Insert a blank line before a 'def' or 'class' immediately nested\n      within another 'def' or 'class'. For example:\n\n        class Foo:\n                           # <------ this blank line\n          def method():\n            pass\n    \"\"\"),\n    BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION=textwrap.dedent(\"\"\"\\\n      Number of blank lines surrounding top-level function and class\n      definitions.\n    \"\"\"),\n    BLANK_LINES_BETWEEN_TOP_LEVEL_IMPORTS_AND_VARIABLES=textwrap.dedent(\"\"\"\\\n      Number of blank lines between top-level imports and variable\n      definitions.\n    \"\"\"),\n    COALESCE_BRACKETS=textwrap.dedent(\"\"\"\\\n      Do not split consecutive brackets. Only relevant when\n      dedent_closing_brackets is set. For example:\n\n         call_func_that_takes_a_dict(\n             {\n                 'key1': 'value1',\n                 'key2': 'value2',\n             }\n         )\n\n      would reformat to:\n\n         call_func_that_takes_a_dict({\n             'key1': 'value1',\n             'key2': 'value2',\n         })\n    \"\"\"),\n    COLUMN_LIMIT=textwrap.dedent(\"\"\"\\\n      The column limit.\n    \"\"\"),\n    CONTINUATION_ALIGN_STYLE=textwrap.dedent(\"\"\"\\\n      The style for continuation alignment. Possible values are:\n\n      - SPACE: Use spaces for continuation alignment. This is default behavior.\n      - FIXED: Use fixed number (CONTINUATION_INDENT_WIDTH) of columns\n        (ie: CONTINUATION_INDENT_WIDTH/INDENT_WIDTH tabs or\n        CONTINUATION_INDENT_WIDTH spaces) for continuation alignment.\n      - VALIGN-RIGHT: Vertically align continuation lines to multiple of\n        INDENT_WIDTH columns. Slightly right (one tab or a few spaces) if\n        cannot vertically align continuation lines with indent characters.\n    \"\"\"),\n    CONTINUATION_INDENT_WIDTH=textwrap.dedent(\"\"\"\\\n      Indent width used for line continuations.\n    \"\"\"),\n    DEDENT_CLOSING_BRACKETS=textwrap.dedent(\"\"\"\\\n      Put closing brackets on a separate line, dedented, if the bracketed\n      expression can't fit in a single line. Applies to all kinds of brackets,\n      including function definitions and calls. For example:\n\n        config = {\n            'key1': 'value1',\n            'key2': 'value2',\n        }        # <--- this bracket is dedented and on a separate line\n\n        time_series = self.remote_client.query_entity_counters(\n            entity='dev3246.region1',\n            key='dns.query_latency_tcp',\n            transform=Transformation.AVERAGE(window=timedelta(seconds=60)),\n            start_ts=now()-timedelta(days=3),\n            end_ts=now(),\n        )        # <--- this bracket is dedented and on a separate line\n    \"\"\"),\n    DISABLE_ENDING_COMMA_HEURISTIC=textwrap.dedent(\"\"\"\\\n      Disable the heuristic which places each list element on a separate line\n      if the list is comma-terminated.\n\n      Note: The behavior of this flag changed in v0.40.3.  Before, if this flag\n      was true, we would split lists that contained a trailing comma or a\n      comment.  Now, we have a separate flag, `DISABLE_SPLIT_LIT_WITH_COMMENT`,\n      that controls splitting when a list contains a comment.  To get the old\n      behavior, set both flags to true.  More information in CHANGELOG.md.\n    \"\"\"),\n    DISABLE_SPLIT_LIST_WITH_COMMENT=textwrap.dedent(\"\"\"\n      Don't put every element on a new line within a list that contains\n      interstitial comments.\n    \"\"\"),\n    EACH_DICT_ENTRY_ON_SEPARATE_LINE=textwrap.dedent(\"\"\"\\\n      Place each dictionary entry onto its own line.\n    \"\"\"),\n    FORCE_MULTILINE_DICT=textwrap.dedent(\"\"\"\\\n      Require multiline dictionary even if it would normally fit on one line.\n      For example:\n\n        config = {\n            'key1': 'value1'\n        }\n    \"\"\"),\n    I18N_COMMENT=textwrap.dedent(\"\"\"\\\n      The regex for an i18n comment. The presence of this comment stops\n      reformatting of that line, because the comments are required to be\n      next to the string they translate.\n    \"\"\"),\n    I18N_FUNCTION_CALL=textwrap.dedent(\"\"\"\\\n      The i18n function call names. The presence of this function stops\n      reformattting on that line, because the string it has cannot be moved\n      away from the i18n comment.\n    \"\"\"),\n    INDENT_CLOSING_BRACKETS=textwrap.dedent(\"\"\"\\\n      Put closing brackets on a separate line, indented, if the bracketed\n      expression can't fit in a single line. Applies to all kinds of brackets,\n      including function definitions and calls. For example:\n\n        config = {\n            'key1': 'value1',\n            'key2': 'value2',\n            }        # <--- this bracket is indented and on a separate line\n\n        time_series = self.remote_client.query_entity_counters(\n            entity='dev3246.region1',\n            key='dns.query_latency_tcp',\n            transform=Transformation.AVERAGE(window=timedelta(seconds=60)),\n            start_ts=now()-timedelta(days=3),\n            end_ts=now(),\n            )        # <--- this bracket is indented and on a separate line\n    \"\"\"),\n    INDENT_DICTIONARY_VALUE=textwrap.dedent(\"\"\"\\\n      Indent the dictionary value if it cannot fit on the same line as the\n      dictionary key. For example:\n\n        config = {\n            'key1':\n                'value1',\n            'key2': value1 +\n                    value2,\n        }\n    \"\"\"),\n    INDENT_BLANK_LINES=textwrap.dedent(\"\"\"\\\n      Indent blank lines.\n    \"\"\"),\n    INDENT_WIDTH=textwrap.dedent(\"\"\"\\\n      The number of columns to use for indentation.\n    \"\"\"),\n    JOIN_MULTIPLE_LINES=textwrap.dedent(\"\"\"\\\n      Join short lines into one line. E.g., single line 'if' statements.\n    \"\"\"),\n    NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS=textwrap.dedent(\"\"\"\\\n      Do not include spaces around selected binary operators. For example:\n\n        1 + 2 * 3 - 4 / 5\n\n      will be formatted as follows when configured with \"*,/\":\n\n        1 + 2*3 - 4/5\n    \"\"\"),\n    SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET=textwrap.dedent(\"\"\"\\\n      Insert a space between the ending comma and closing bracket of a list,\n      etc.\n    \"\"\"),\n    SPACE_INSIDE_BRACKETS=textwrap.dedent(\"\"\"\\\n      Use spaces inside brackets, braces, and parentheses.  For example:\n\n        method_call( 1 )\n        my_dict[ 3 ][ 1 ][ get_index( *args, **kwargs ) ]\n        my_set = { 1, 2, 3 }\n    \"\"\"),\n    SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN=textwrap.dedent(\"\"\"\\\n      Use spaces around default or named assigns.\n    \"\"\"),\n    SPACES_AROUND_DICT_DELIMITERS=textwrap.dedent(\"\"\"\\\n      Adds a space after the opening '{' and before the ending '}' dict\n      delimiters.\n\n        {1: 2}\n\n      will be formatted as:\n\n        { 1: 2 }\n    \"\"\"),\n    SPACES_AROUND_LIST_DELIMITERS=textwrap.dedent(\"\"\"\\\n      Adds a space after the opening '[' and before the ending ']' list\n      delimiters.\n\n        [1, 2]\n\n      will be formatted as:\n\n        [ 1, 2 ]\n    \"\"\"),\n    SPACES_AROUND_POWER_OPERATOR=textwrap.dedent(\"\"\"\\\n      Use spaces around the power operator.\n    \"\"\"),\n    SPACES_AROUND_SUBSCRIPT_COLON=textwrap.dedent(\"\"\"\\\n      Use spaces around the subscript / slice operator.  For example:\n\n        my_list[1 : 10 : 2]\n    \"\"\"),\n    SPACES_AROUND_TUPLE_DELIMITERS=textwrap.dedent(\"\"\"\\\n      Adds a space after the opening '(' and before the ending ')' tuple\n      delimiters.\n\n        (1, 2, 3)\n\n      will be formatted as:\n\n        ( 1, 2, 3 )\n    \"\"\"),\n    SPACES_BEFORE_COMMENT=textwrap.dedent(\"\"\"\\\n      The number of spaces required before a trailing comment.\n      This can be a single value (representing the number of spaces\n      before each trailing comment) or list of values (representing\n      alignment column values; trailing comments within a block will\n      be aligned to the first column value that is greater than the maximum\n      line length within the block). For example:\n\n      With spaces_before_comment=5:\n\n        1 + 1 # Adding values\n\n      will be formatted as:\n\n        1 + 1     # Adding values <-- 5 spaces between the end of the\n                  # statement and comment\n\n      With spaces_before_comment=15, 20:\n\n        1 + 1 # Adding values\n        two + two # More adding\n\n        longer_statement # This is a longer statement\n        short # This is a shorter statement\n\n        a_very_long_statement_that_extends_beyond_the_final_column # Comment\n        short # This is a shorter statement\n\n      will be formatted as:\n\n        1 + 1          # Adding values <-- end of line comments in block\n                       # aligned to col 15\n        two + two      # More adding\n\n        longer_statement    # This is a longer statement <-- end of line\n                            # comments in block aligned to col 20\n        short               # This is a shorter statement\n\n        a_very_long_statement_that_extends_beyond_the_final_column  # Comment <-- the end of line comments are aligned based on the line length\n        short                                                       # This is a shorter statement\n\n    \"\"\"),  # noqa\n    SPLIT_ALL_COMMA_SEPARATED_VALUES=textwrap.dedent(\"\"\"\\\n      Split before arguments.\n    \"\"\"),\n    SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES=textwrap.dedent(\"\"\"\\\n      Split before arguments, but do not split all subexpressions recursively\n      (unless needed).\n    \"\"\"),\n    SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED=textwrap.dedent(\"\"\"\\\n      Split before arguments if the argument list is terminated by a\n      comma.\n    \"\"\"),\n    SPLIT_BEFORE_ARITHMETIC_OPERATOR=textwrap.dedent(\"\"\"\\\n      Set to True to prefer splitting before '+', '-', '*', '/', '//', or '@'\n      rather than after.\n    \"\"\"),\n    SPLIT_BEFORE_BITWISE_OPERATOR=textwrap.dedent(\"\"\"\\\n      Set to True to prefer splitting before '&', '|' or '^' rather than\n      after.\n    \"\"\"),\n    SPLIT_BEFORE_CLOSING_BRACKET=textwrap.dedent(\"\"\"\\\n      Split before the closing bracket if a list or dict literal doesn't fit on\n      a single line.\n    \"\"\"),\n    SPLIT_BEFORE_DICT_SET_GENERATOR=textwrap.dedent(\"\"\"\\\n      Split before a dictionary or set generator (comp_for). For example, note\n      the split before the 'for':\n\n        foo = {\n            variable: 'Hello world, have a nice day!'\n            for variable in bar if variable != 42\n        }\n    \"\"\"),\n    SPLIT_BEFORE_DOT=textwrap.dedent(\"\"\"\\\n      Split before the '.' if we need to split a longer expression:\n\n        foo = ('This is a really long string: {}, {}, {}, {}'.format(a, b, c, d))\n\n      would reformat to something like:\n\n        foo = ('This is a really long string: {}, {}, {}, {}'\n               .format(a, b, c, d))\n    \"\"\"),  # noqa\n    SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN=textwrap.dedent(\"\"\"\\\n      Split after the opening paren which surrounds an expression if it doesn't\n      fit on a single line.\n    \"\"\"),\n    SPLIT_BEFORE_FIRST_ARGUMENT=textwrap.dedent(\"\"\"\\\n      If an argument / parameter list is going to be split, then split before\n      the first argument.\n    \"\"\"),\n    SPLIT_BEFORE_LOGICAL_OPERATOR=textwrap.dedent(\"\"\"\\\n      Set to True to prefer splitting before 'and' or 'or' rather than\n      after.\n    \"\"\"),\n    SPLIT_BEFORE_NAMED_ASSIGNS=textwrap.dedent(\"\"\"\\\n      Split named assignments onto individual lines.\n    \"\"\"),\n    SPLIT_COMPLEX_COMPREHENSION=textwrap.dedent(\"\"\"\\\n      Set to True to split list comprehensions and generators that have\n      non-trivial expressions and multiple clauses before each of these\n      clauses. For example:\n\n        result = [\n            a_long_var + 100 for a_long_var in xrange(1000)\n            if a_long_var % 10]\n\n      would reformat to something like:\n\n        result = [\n            a_long_var + 100\n            for a_long_var in xrange(1000)\n            if a_long_var % 10]\n    \"\"\"),\n    SPLIT_PENALTY_AFTER_OPENING_BRACKET=textwrap.dedent(\"\"\"\\\n      The penalty for splitting right after the opening bracket.\n    \"\"\"),\n    SPLIT_PENALTY_AFTER_UNARY_OPERATOR=textwrap.dedent(\"\"\"\\\n      The penalty for splitting the line after a unary operator.\n    \"\"\"),\n    SPLIT_PENALTY_ARITHMETIC_OPERATOR=textwrap.dedent(\"\"\"\\\n      The penalty of splitting the line around the '+', '-', '*', '/', '//',\n      `%`, and '@' operators.\n    \"\"\"),\n    SPLIT_PENALTY_BEFORE_IF_EXPR=textwrap.dedent(\"\"\"\\\n      The penalty for splitting right before an if expression.\n    \"\"\"),\n    SPLIT_PENALTY_BITWISE_OPERATOR=textwrap.dedent(\"\"\"\\\n      The penalty of splitting the line around the '&', '|', and '^' operators.\n    \"\"\"),\n    SPLIT_PENALTY_COMPREHENSION=textwrap.dedent(\"\"\"\\\n      The penalty for splitting a list comprehension or generator\n      expression.\n    \"\"\"),\n    SPLIT_PENALTY_EXCESS_CHARACTER=textwrap.dedent(\"\"\"\\\n      The penalty for characters over the column limit.\n    \"\"\"),\n    SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT=textwrap.dedent(\"\"\"\\\n      The penalty incurred by adding a line split to the logical line. The\n      more line splits added the higher the penalty.\n    \"\"\"),\n    SPLIT_PENALTY_IMPORT_NAMES=textwrap.dedent(\"\"\"\\\n      The penalty of splitting a list of \"import as\" names. For example:\n\n        from a_very_long_or_indented_module_name_yada_yad import (long_argument_1,\n                                                                  long_argument_2,\n                                                                  long_argument_3)\n\n      would reformat to something like:\n\n        from a_very_long_or_indented_module_name_yada_yad import (\n            long_argument_1, long_argument_2, long_argument_3)\n    \"\"\"),  # noqa\n    SPLIT_PENALTY_LOGICAL_OPERATOR=textwrap.dedent(\"\"\"\\\n      The penalty of splitting the line around the 'and' and 'or' operators.\n    \"\"\"),\n    USE_TABS=textwrap.dedent(\"\"\"\\\n      Use the Tab character for indentation.\n    \"\"\"),\n)\n\n\ndef CreatePEP8Style():\n  \"\"\"Create the PEP8 formatting style.\"\"\"\n  return dict(\n      ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT=True,\n      ALLOW_MULTILINE_DICTIONARY_KEYS=False,\n      ALLOW_MULTILINE_LAMBDAS=False,\n      ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS=True,\n      ALLOW_SPLIT_BEFORE_DICT_VALUE=True,\n      ARITHMETIC_PRECEDENCE_INDICATION=False,\n      BLANK_LINE_BEFORE_CLASS_DOCSTRING=False,\n      BLANK_LINE_BEFORE_MODULE_DOCSTRING=False,\n      BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=True,\n      BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION=2,\n      BLANK_LINES_BETWEEN_TOP_LEVEL_IMPORTS_AND_VARIABLES=1,\n      COALESCE_BRACKETS=False,\n      COLUMN_LIMIT=79,\n      CONTINUATION_ALIGN_STYLE='SPACE',\n      CONTINUATION_INDENT_WIDTH=4,\n      DEDENT_CLOSING_BRACKETS=False,\n      DISABLE_ENDING_COMMA_HEURISTIC=False,\n      DISABLE_SPLIT_LIST_WITH_COMMENT=False,\n      EACH_DICT_ENTRY_ON_SEPARATE_LINE=True,\n      FORCE_MULTILINE_DICT=False,\n      I18N_COMMENT='',\n      I18N_FUNCTION_CALL='',\n      INDENT_CLOSING_BRACKETS=False,\n      INDENT_DICTIONARY_VALUE=False,\n      INDENT_WIDTH=4,\n      INDENT_BLANK_LINES=False,\n      JOIN_MULTIPLE_LINES=True,\n      NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS=set(),\n      SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET=True,\n      SPACE_INSIDE_BRACKETS=False,\n      SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN=False,\n      SPACES_AROUND_DICT_DELIMITERS=False,\n      SPACES_AROUND_LIST_DELIMITERS=False,\n      SPACES_AROUND_POWER_OPERATOR=False,\n      SPACES_AROUND_SUBSCRIPT_COLON=False,\n      SPACES_AROUND_TUPLE_DELIMITERS=False,\n      SPACES_BEFORE_COMMENT=2,\n      SPLIT_ALL_COMMA_SEPARATED_VALUES=False,\n      SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES=False,\n      SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED=False,\n      SPLIT_BEFORE_ARITHMETIC_OPERATOR=False,\n      SPLIT_BEFORE_BITWISE_OPERATOR=True,\n      SPLIT_BEFORE_CLOSING_BRACKET=True,\n      SPLIT_BEFORE_DICT_SET_GENERATOR=True,\n      SPLIT_BEFORE_DOT=False,\n      SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN=False,\n      SPLIT_BEFORE_FIRST_ARGUMENT=False,\n      SPLIT_BEFORE_LOGICAL_OPERATOR=True,\n      SPLIT_BEFORE_NAMED_ASSIGNS=True,\n      SPLIT_COMPLEX_COMPREHENSION=False,\n      SPLIT_PENALTY_AFTER_OPENING_BRACKET=300,\n      SPLIT_PENALTY_AFTER_UNARY_OPERATOR=10000,\n      SPLIT_PENALTY_ARITHMETIC_OPERATOR=300,\n      SPLIT_PENALTY_BEFORE_IF_EXPR=0,\n      SPLIT_PENALTY_BITWISE_OPERATOR=300,\n      SPLIT_PENALTY_COMPREHENSION=80,\n      SPLIT_PENALTY_EXCESS_CHARACTER=7000,\n      SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT=30,\n      SPLIT_PENALTY_IMPORT_NAMES=0,\n      SPLIT_PENALTY_LOGICAL_OPERATOR=300,\n      USE_TABS=False,\n  )\n\n\ndef CreateGoogleStyle():\n  \"\"\"Create the Google formatting style.\"\"\"\n  style = CreatePEP8Style()\n  style['ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT'] = False\n  style['COLUMN_LIMIT'] = 80\n  style['INDENT_DICTIONARY_VALUE'] = True\n  style['INDENT_WIDTH'] = 4\n  style['I18N_COMMENT'] = r'#\\..*'\n  style['I18N_FUNCTION_CALL'] = ['N_', '_']\n  style['JOIN_MULTIPLE_LINES'] = False\n  style['SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET'] = False\n  style['SPLIT_BEFORE_BITWISE_OPERATOR'] = False\n  style['SPLIT_BEFORE_DICT_SET_GENERATOR'] = False\n  style['SPLIT_BEFORE_LOGICAL_OPERATOR'] = False\n  style['SPLIT_COMPLEX_COMPREHENSION'] = True\n  style['SPLIT_PENALTY_COMPREHENSION'] = 2100\n  return style\n\n\ndef CreateYapfStyle():\n  \"\"\"Create the YAPF formatting style.\"\"\"\n  style = CreateGoogleStyle()\n  style['ALLOW_MULTILINE_DICTIONARY_KEYS'] = True\n  style['ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS'] = False\n  style['INDENT_WIDTH'] = 2\n  style['SPLIT_BEFORE_BITWISE_OPERATOR'] = True\n  style['SPLIT_BEFORE_DOT'] = True\n  style['SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN'] = True\n  return style\n\n\ndef CreateFacebookStyle():\n  \"\"\"Create the Facebook formatting style.\"\"\"\n  style = CreatePEP8Style()\n  style['ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT'] = False\n  style['BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF'] = False\n  style['COLUMN_LIMIT'] = 80\n  style['DEDENT_CLOSING_BRACKETS'] = True\n  style['INDENT_CLOSING_BRACKETS'] = False\n  style['INDENT_DICTIONARY_VALUE'] = True\n  style['JOIN_MULTIPLE_LINES'] = False\n  style['SPACES_BEFORE_COMMENT'] = 2\n  style['SPLIT_PENALTY_AFTER_OPENING_BRACKET'] = 0\n  style['SPLIT_PENALTY_BEFORE_IF_EXPR'] = 30\n  style['SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT'] = 30\n  style['SPLIT_BEFORE_BITWISE_OPERATOR'] = False\n  style['SPLIT_BEFORE_LOGICAL_OPERATOR'] = False\n  return style\n\n\n_STYLE_NAME_TO_FACTORY = dict(\n    facebook=CreateFacebookStyle,\n    google=CreateGoogleStyle,\n    pep8=CreatePEP8Style,\n    yapf=CreateYapfStyle,\n)\n\n_DEFAULT_STYLE_TO_FACTORY = [\n    (CreateFacebookStyle(), CreateFacebookStyle),\n    (CreateGoogleStyle(), CreateGoogleStyle),\n    (CreatePEP8Style(), CreatePEP8Style),\n    (CreateYapfStyle(), CreateYapfStyle),\n]\n\n\ndef _GetStyleFactory(style):\n  for def_style, factory in _DEFAULT_STYLE_TO_FACTORY:\n    if style == def_style:\n      return factory\n  return None\n\n\ndef _ContinuationAlignStyleStringConverter(s):\n  \"\"\"Option value converter for a continuation align style string.\"\"\"\n  accepted_styles = ('SPACE', 'FIXED', 'VALIGN-RIGHT')\n  if s:\n    r = s.strip('\"\\'').replace('_', '-').upper()\n    if r not in accepted_styles:\n      raise ValueError('unknown continuation align style: %r' % (s,))\n  else:\n    r = accepted_styles[0]\n  return r\n\n\ndef _StringListConverter(s):\n  \"\"\"Option value converter for a comma-separated list of strings.\"\"\"\n  return [part.strip() for part in s.split(',')]\n\n\ndef _StringSetConverter(s):\n  \"\"\"Option value converter for a comma-separated set of strings.\"\"\"\n  if len(s) > 2 and s[0] in '\"\\'':\n    s = s[1:-1]\n  return {part.strip() for part in s.split(',')}\n\n\ndef _BoolConverter(s):\n  \"\"\"Option value converter for a boolean.\"\"\"\n  return ConfigParser.BOOLEAN_STATES[s.lower()]\n\n\ndef _IntListConverter(s):\n  \"\"\"Option value converter for a comma-separated list of integers.\"\"\"\n  s = s.strip()\n  if s.startswith('[') and s.endswith(']'):\n    s = s[1:-1]\n\n  return [int(part.strip()) for part in s.split(',') if part.strip()]\n\n\ndef _IntOrIntListConverter(s):\n  \"\"\"Option value converter for an integer or list of integers.\"\"\"\n  if len(s) > 2 and s[0] in '\"\\'':\n    s = s[1:-1]\n  return _IntListConverter(s) if ',' in s else int(s)\n\n\n# Different style options need to have their values interpreted differently when\n# read from the config file. This dict maps an option name to a \"converter\"\n# function that accepts the string read for the option's value from the file and\n# returns it wrapper in actual Python type that's going to be meaningful to\n# yapf.\n#\n# Note: this dict has to map all the supported style options.\n_STYLE_OPTION_VALUE_CONVERTER = dict(\n    ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT=_BoolConverter,\n    ALLOW_MULTILINE_DICTIONARY_KEYS=_BoolConverter,\n    ALLOW_MULTILINE_LAMBDAS=_BoolConverter,\n    ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS=_BoolConverter,\n    ALLOW_SPLIT_BEFORE_DICT_VALUE=_BoolConverter,\n    ARITHMETIC_PRECEDENCE_INDICATION=_BoolConverter,\n    BLANK_LINE_BEFORE_CLASS_DOCSTRING=_BoolConverter,\n    BLANK_LINE_BEFORE_MODULE_DOCSTRING=_BoolConverter,\n    BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=_BoolConverter,\n    BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION=int,\n    BLANK_LINES_BETWEEN_TOP_LEVEL_IMPORTS_AND_VARIABLES=int,\n    COALESCE_BRACKETS=_BoolConverter,\n    COLUMN_LIMIT=int,\n    CONTINUATION_ALIGN_STYLE=_ContinuationAlignStyleStringConverter,\n    CONTINUATION_INDENT_WIDTH=int,\n    DEDENT_CLOSING_BRACKETS=_BoolConverter,\n    DISABLE_ENDING_COMMA_HEURISTIC=_BoolConverter,\n    DISABLE_SPLIT_LIST_WITH_COMMENT=_BoolConverter,\n    EACH_DICT_ENTRY_ON_SEPARATE_LINE=_BoolConverter,\n    FORCE_MULTILINE_DICT=_BoolConverter,\n    I18N_COMMENT=str,\n    I18N_FUNCTION_CALL=_StringListConverter,\n    INDENT_BLANK_LINES=_BoolConverter,\n    INDENT_CLOSING_BRACKETS=_BoolConverter,\n    INDENT_DICTIONARY_VALUE=_BoolConverter,\n    INDENT_WIDTH=int,\n    JOIN_MULTIPLE_LINES=_BoolConverter,\n    NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS=_StringSetConverter,\n    SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET=_BoolConverter,\n    SPACE_INSIDE_BRACKETS=_BoolConverter,\n    SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN=_BoolConverter,\n    SPACES_AROUND_DICT_DELIMITERS=_BoolConverter,\n    SPACES_AROUND_LIST_DELIMITERS=_BoolConverter,\n    SPACES_AROUND_POWER_OPERATOR=_BoolConverter,\n    SPACES_AROUND_SUBSCRIPT_COLON=_BoolConverter,\n    SPACES_AROUND_TUPLE_DELIMITERS=_BoolConverter,\n    SPACES_BEFORE_COMMENT=_IntOrIntListConverter,\n    SPLIT_ALL_COMMA_SEPARATED_VALUES=_BoolConverter,\n    SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES=_BoolConverter,\n    SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED=_BoolConverter,\n    SPLIT_BEFORE_ARITHMETIC_OPERATOR=_BoolConverter,\n    SPLIT_BEFORE_BITWISE_OPERATOR=_BoolConverter,\n    SPLIT_BEFORE_CLOSING_BRACKET=_BoolConverter,\n    SPLIT_BEFORE_DICT_SET_GENERATOR=_BoolConverter,\n    SPLIT_BEFORE_DOT=_BoolConverter,\n    SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN=_BoolConverter,\n    SPLIT_BEFORE_FIRST_ARGUMENT=_BoolConverter,\n    SPLIT_BEFORE_LOGICAL_OPERATOR=_BoolConverter,\n    SPLIT_BEFORE_NAMED_ASSIGNS=_BoolConverter,\n    SPLIT_COMPLEX_COMPREHENSION=_BoolConverter,\n    SPLIT_PENALTY_AFTER_OPENING_BRACKET=int,\n    SPLIT_PENALTY_AFTER_UNARY_OPERATOR=int,\n    SPLIT_PENALTY_ARITHMETIC_OPERATOR=int,\n    SPLIT_PENALTY_BEFORE_IF_EXPR=int,\n    SPLIT_PENALTY_BITWISE_OPERATOR=int,\n    SPLIT_PENALTY_COMPREHENSION=int,\n    SPLIT_PENALTY_EXCESS_CHARACTER=int,\n    SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT=int,\n    SPLIT_PENALTY_IMPORT_NAMES=int,\n    SPLIT_PENALTY_LOGICAL_OPERATOR=int,\n    USE_TABS=_BoolConverter,\n)\n\n\ndef CreateStyleFromConfig(style_config):\n  \"\"\"Create a style dict from the given config.\n\n  Arguments:\n    style_config: either a style name or a file name. The file is expected to\n      contain settings. It can have a special BASED_ON_STYLE setting naming the\n      style which it derives from. If no such setting is found, it derives from\n      the default style. When style_config is None, the _GLOBAL_STYLE_FACTORY\n      config is created.\n\n  Returns:\n    A style dict.\n\n  Raises:\n    StyleConfigError: if an unknown style option was encountered.\n  \"\"\"\n\n  def GlobalStyles():\n    for style, _ in _DEFAULT_STYLE_TO_FACTORY:\n      yield style\n\n  def_style = False\n  if style_config is None:\n    for style in GlobalStyles():\n      if _style == style:\n        def_style = True\n        break\n    if not def_style:\n      return _style\n    return _GLOBAL_STYLE_FACTORY()\n\n  if isinstance(style_config, dict):\n    config = _CreateConfigParserFromConfigDict(style_config)\n  elif isinstance(style_config, str):\n    style_factory = _STYLE_NAME_TO_FACTORY.get(style_config.lower())\n    if style_factory is not None:\n      return style_factory()\n    if style_config.startswith('{'):\n      # Most likely a style specification from the command line.\n      config = _CreateConfigParserFromConfigString(style_config)\n    else:\n      # Unknown config name: assume it's a file name then.\n      config = _CreateConfigParserFromConfigFile(style_config)\n  return _CreateStyleFromConfigParser(config)\n\n\ndef _CreateConfigParserFromConfigDict(config_dict):\n  config = ConfigParser()\n  config.add_section('style')\n  for key, value in config_dict.items():\n    config.set('style', key, str(value))\n  return config\n\n\ndef _CreateConfigParserFromConfigString(config_string):\n  \"\"\"Given a config string from the command line, return a config parser.\"\"\"\n  if config_string[0] != '{' or config_string[-1] != '}':\n    raise StyleConfigError(\n        \"Invalid style dict syntax: '{}'.\".format(config_string))\n  config = ConfigParser()\n  config.add_section('style')\n  for key, value, _ in re.findall(\n      r'([a-zA-Z0-9_]+)\\s*[:=]\\s*'\n      r'(?:'\n      r'((?P<quote>[\\'\"]).*?(?P=quote)|'\n      r'[a-zA-Z0-9_]+)'\n      r')', config_string):  # yapf: disable\n    config.set('style', key, value)\n  return config\n\n\ndef _CreateConfigParserFromConfigFile(config_filename):\n  \"\"\"Read the file and return a ConfigParser object.\"\"\"\n  if not os.path.exists(config_filename):\n    # Provide a more meaningful error here.\n    raise StyleConfigError(\n        '\"{0}\" is not a valid style or file path'.format(config_filename))\n  config = ConfigParser()\n\n  if config_filename.endswith(PYPROJECT_TOML):\n    with open(config_filename, 'rb') as style_file:\n      pyproject_toml = tomllib.load(style_file)\n      style_dict = pyproject_toml.get('tool', {}).get('yapf', None)\n      if style_dict is None:\n        raise StyleConfigError(\n            'Unable to find section [tool.yapf] in {0}'.format(config_filename))\n      config.add_section('style')\n      for k, v in style_dict.items():\n        config.set('style', k, str(v))\n      return config\n\n  with open(config_filename) as style_file:\n    config.read_file(style_file)\n\n    if config_filename.endswith(SETUP_CONFIG):\n      if not config.has_section('yapf'):\n        raise StyleConfigError(\n            'Unable to find section [yapf] in {0}'.format(config_filename))\n      return config\n\n    if config_filename.endswith(LOCAL_STYLE):\n      if not config.has_section('style'):\n        raise StyleConfigError(\n            'Unable to find section [style] in {0}'.format(config_filename))\n      return config\n\n    if not config.has_section('style'):\n      raise StyleConfigError(\n          'Unable to find section [style] in {0}'.format(config_filename))\n    return config\n\n\ndef _CreateStyleFromConfigParser(config):\n  \"\"\"Create a style dict from a configuration file.\n\n  Arguments:\n    config: a ConfigParser object.\n\n  Returns:\n    A style dict.\n\n  Raises:\n    StyleConfigError: if an unknown style option was encountered.\n  \"\"\"\n  # Initialize the base style.\n  section = 'yapf' if config.has_section('yapf') else 'style'\n  if config.has_option('style', 'based_on_style'):\n    based_on = config.get('style', 'based_on_style').lower()\n    base_style = _STYLE_NAME_TO_FACTORY[based_on]()\n  elif config.has_option('yapf', 'based_on_style'):\n    based_on = config.get('yapf', 'based_on_style').lower()\n    base_style = _STYLE_NAME_TO_FACTORY[based_on]()\n  else:\n    base_style = _GLOBAL_STYLE_FACTORY()\n\n  # Read all options specified in the file and update the style.\n  for option, value in config.items(section):\n    if option.lower() == 'based_on_style':\n      # Now skip this one - we've already handled it and it's not one of the\n      # recognized style options.\n      continue\n    option = option.upper()\n    if option not in _STYLE_OPTION_VALUE_CONVERTER:\n      raise StyleConfigError('Unknown style option \"{0}\"'.format(option))\n    try:\n      base_style[option] = _STYLE_OPTION_VALUE_CONVERTER[option](value)\n    except ValueError:\n      raise StyleConfigError(\"'{}' is not a valid setting for {}.\".format(\n          value, option))\n  return base_style\n\n\n# The default style - used if yapf is not invoked without specifically\n# requesting a formatting style.\nDEFAULT_STYLE = 'pep8'\nDEFAULT_STYLE_FACTORY = CreatePEP8Style\n_GLOBAL_STYLE_FACTORY = CreatePEP8Style\n\n# The name of the file to use for global style definition.\nGLOBAL_STYLE = (\n    os.path.join(\n        os.getenv('XDG_CONFIG_HOME') or os.path.expanduser('~/.config'), 'yapf',\n        'style'))\n\n# The name of the file to use for directory-local style definition.\nLOCAL_STYLE = '.style.yapf'\n\n# Alternative place for directory-local style definition. Style should be\n# specified in the '[yapf]' section.\nSETUP_CONFIG = 'setup.cfg'\n\n# Style definition by local pyproject.toml file. Style should be specified\n# in the '[tool.yapf]' section.\nPYPROJECT_TOML = 'pyproject.toml'\n\n# TODO(eliben): For now we're preserving the global presence of a style dict.\n# Refactor this so that the style is passed around through yapf rather than\n# being global.\n_style = None\nSetGlobalStyle(_GLOBAL_STYLE_FACTORY())\n"
  },
  {
    "path": "yapf/yapflib/subtypes.py",
    "content": "# Copyright 2021 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Token subtypes used to improve formatting.\"\"\"\n\nNONE = 0\nUNARY_OPERATOR = 1\nBINARY_OPERATOR = 2\nSUBSCRIPT_COLON = 3\nSUBSCRIPT_BRACKET = 4\nDEFAULT_OR_NAMED_ASSIGN = 5\nDEFAULT_OR_NAMED_ASSIGN_ARG_LIST = 6\nVARARGS_LIST = 7\nVARARGS_STAR = 8\nKWARGS_STAR_STAR = 9\nASSIGN_OPERATOR = 10\nDICTIONARY_KEY = 11\nDICTIONARY_KEY_PART = 12\nDICTIONARY_VALUE = 13\nDICT_SET_GENERATOR = 14\nCOMP_EXPR = 15\nCOMP_FOR = 16\nCOMP_IF = 17\nFUNC_DEF = 18\nDECORATOR = 19\nTYPED_NAME = 20\nTYPED_NAME_ARG_LIST = 21\nSIMPLE_EXPRESSION = 22\nPARAMETER_START = 23\nPARAMETER_STOP = 24\nLAMBDEF = 25\n"
  },
  {
    "path": "yapf/yapflib/yapf_api.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Entry points for YAPF.\n\nThe main APIs that YAPF exposes to drive the reformatting.\n\n  FormatFile(): reformat a file.\n  FormatCode(): reformat a string of code.\n\nThese APIs have some common arguments:\n\n  style_config: (string) Either a style name or a path to a file that contains\n    formatting style settings. If None is specified, use the default style\n    as set in style.DEFAULT_STYLE_FACTORY\n  lines: (list of tuples of integers) A list of tuples of lines, [start, end],\n    that we want to format. The lines are 1-based indexed. It can be used by\n    third-party code (e.g., IDEs) when reformatting a snippet of code rather\n    than a whole file.\n  print_diff: (bool) Instead of returning the reformatted source, return a\n    diff that turns the formatted source into reformatter source.\n\"\"\"\n\nimport codecs\nimport difflib\nimport re\n\nfrom yapf.pyparser import pyparser\nfrom yapf.pytree import blank_line_calculator\nfrom yapf.pytree import comment_splicer\nfrom yapf.pytree import continuation_splicer\nfrom yapf.pytree import pytree_unwrapper\nfrom yapf.pytree import pytree_utils\nfrom yapf.pytree import split_penalty\nfrom yapf.pytree import subtype_assigner\nfrom yapf.yapflib import errors\nfrom yapf.yapflib import file_resources\nfrom yapf.yapflib import identify_container\nfrom yapf.yapflib import reformatter\nfrom yapf.yapflib import style\n\n\ndef FormatFile(filename,\n               style_config=None,\n               lines=None,\n               print_diff=False,\n               in_place=False,\n               logger=None):\n  \"\"\"Format a single Python file and return the formatted code.\n\n  Arguments:\n    filename: (unicode) The file to reformat.\n    style_config: (string) Either a style name or a path to a file that contains\n      formatting style settings. If None is specified, use the default style\n      as set in style.DEFAULT_STYLE_FACTORY\n    lines: (list of tuples of integers) A list of tuples of lines, [start, end],\n      that we want to format. The lines are 1-based indexed. It can be used by\n      third-party code (e.g., IDEs) when reformatting a snippet of code rather\n      than a whole file.\n    print_diff: (bool) Instead of returning the reformatted source, return a\n      diff that turns the formatted source into reformatter source.\n    in_place: (bool) If True, write the reformatted code back to the file.\n    logger: (io streamer) A stream to output logging.\n\n  Returns:\n    Tuple of (reformatted_code, encoding, changed). reformatted_code is None if\n    the file is successfully written to (having used in_place). reformatted_code\n    is a diff if print_diff is True.\n\n  Raises:\n    IOError: raised if there was an error reading the file.\n    ValueError: raised if in_place and print_diff are both specified.\n  \"\"\"\n  if in_place and print_diff:\n    raise ValueError('Cannot pass both in_place and print_diff.')\n\n  original_source, newline, encoding = ReadFile(filename, logger)\n  reformatted_source, changed = FormatCode(\n      original_source,\n      style_config=style_config,\n      filename=filename,\n      lines=lines,\n      print_diff=print_diff)\n  if newline != '\\n':\n    reformatted_source = reformatted_source.replace('\\n', newline)\n  if in_place:\n    if changed:\n      file_resources.WriteReformattedCode(filename, reformatted_source,\n                                          encoding, in_place)\n    return None, encoding, changed\n\n  return reformatted_source, encoding, changed\n\n\ndef FormatTree(tree, style_config=None, lines=None):\n  \"\"\"Format a parsed lib2to3 pytree.\n\n  This provides an alternative entry point to YAPF.\n\n  Arguments:\n    tree: (pytree.Node) The root of the pytree to format.\n    style_config: (string) Either a style name or a path to a file that contains\n      formatting style settings. If None is specified, use the default style\n      as set in style.DEFAULT_STYLE_FACTORY\n    lines: (list of tuples of integers) A list of tuples of lines, [start, end],\n      that we want to format. The lines are 1-based indexed. It can be used by\n      third-party code (e.g., IDEs) when reformatting a snippet of code rather\n      than a whole file.\n\n  Returns:\n    The source formatted according to the given formatting style.\n  \"\"\"\n  style.SetGlobalStyle(style.CreateStyleFromConfig(style_config))\n\n  # Run passes on the tree, modifying it in place.\n  comment_splicer.SpliceComments(tree)\n  continuation_splicer.SpliceContinuations(tree)\n  subtype_assigner.AssignSubtypes(tree)\n  identify_container.IdentifyContainers(tree)\n  split_penalty.ComputeSplitPenalties(tree)\n  blank_line_calculator.CalculateBlankLines(tree)\n\n  llines = pytree_unwrapper.UnwrapPyTree(tree)\n  for lline in llines:\n    lline.CalculateFormattingInformation()\n\n  lines = _LineRangesToSet(lines)\n  _MarkLinesToFormat(llines, lines)\n  return reformatter.Reformat(_SplitSemicolons(llines), lines)\n\n\ndef FormatAST(ast, style_config=None, lines=None):\n  \"\"\"Format a parsed lib2to3 pytree.\n\n  This provides an alternative entry point to YAPF.\n\n  Arguments:\n    unformatted_source: (unicode) The code to format.\n    style_config: (string) Either a style name or a path to a file that contains\n      formatting style settings. If None is specified, use the default style\n      as set in style.DEFAULT_STYLE_FACTORY\n    lines: (list of tuples of integers) A list of tuples of lines, [start, end],\n      that we want to format. The lines are 1-based indexed. It can be used by\n      third-party code (e.g., IDEs) when reformatting a snippet of code rather\n      than a whole file.\n\n  Returns:\n    The source formatted according to the given formatting style.\n  \"\"\"\n  style.SetGlobalStyle(style.CreateStyleFromConfig(style_config))\n\n  llines = pyparser.ParseCode(ast)\n  for lline in llines:\n    lline.CalculateFormattingInformation()\n\n  lines = _LineRangesToSet(lines)\n  _MarkLinesToFormat(llines, lines)\n  return reformatter.Reformat(_SplitSemicolons(llines), lines)\n\n\ndef FormatCode(unformatted_source,\n               filename='<unknown>',\n               style_config=None,\n               lines=None,\n               print_diff=False):\n  \"\"\"Format a string of Python code.\n\n  This provides an alternative entry point to YAPF.\n\n  Arguments:\n    unformatted_source: (unicode) The code to format.\n    filename: (unicode) The name of the file being reformatted.\n    style_config: (string) Either a style name or a path to a file that contains\n      formatting style settings. If None is specified, use the default style\n      as set in style.DEFAULT_STYLE_FACTORY\n    lines: (list of tuples of integers) A list of tuples of lines, [start, end],\n      that we want to format. The lines are 1-based indexed. It can be used by\n      third-party code (e.g., IDEs) when reformatting a snippet of code rather\n      than a whole file.\n    print_diff: (bool) Instead of returning the reformatted source, return a\n      diff that turns the formatted source into reformatter source.\n\n  Returns:\n    Tuple of (reformatted_source, changed). reformatted_source conforms to the\n    desired formatting style. changed is True if the source changed.\n  \"\"\"\n  try:\n    tree = pytree_utils.ParseCodeToTree(unformatted_source)\n  except Exception as e:\n    e.filename = filename\n    raise errors.YapfError(errors.FormatErrorMsg(e))\n\n  reformatted_source = FormatTree(tree, style_config=style_config, lines=lines)\n\n  if unformatted_source == reformatted_source:\n    return '' if print_diff else reformatted_source, False\n\n  if print_diff:\n    code_diff = _GetUnifiedDiff(\n        unformatted_source, reformatted_source, filename=filename)\n    return code_diff, code_diff.strip() != ''  # pylint: disable=g-explicit-bool-comparison # noqa\n\n  return reformatted_source, True\n\n\ndef ReadFile(filename, logger=None):\n  \"\"\"Read the contents of the file.\n\n  An optional logger can be specified to emit messages to your favorite logging\n  stream. If specified, then no exception is raised. This is external so that it\n  can be used by third-party applications.\n\n  Arguments:\n    filename: (unicode) The name of the file.\n    logger: (function) A function or lambda that takes a string and emits it.\n\n  Returns:\n    The contents of filename.\n\n  Raises:\n    IOError: raised if there was an error reading the file.\n  \"\"\"\n  try:\n    encoding = file_resources.FileEncoding(filename)\n\n    # Preserves line endings.\n    with codecs.open(filename, mode='r', encoding=encoding) as fd:\n      lines = fd.readlines()\n\n    line_ending = file_resources.LineEnding(lines)\n    source = '\\n'.join(line.rstrip('\\r\\n') for line in lines) + '\\n'\n    return source, line_ending, encoding\n  except IOError as e:  # pragma: no cover\n    if logger:\n      logger(e)\n    e.args = (e.args[0], (filename, e.args[1][1], e.args[1][2], e.args[1][3]))\n    raise\n  except UnicodeDecodeError as e:  # pragma: no cover\n    if logger:\n      logger('Could not parse %s! Consider excluding this file with --exclude.',\n             filename)\n      logger(e)\n    e.args = (e.args[0], (filename, e.args[1][1], e.args[1][2], e.args[1][3]))\n    raise\n\n\ndef _SplitSemicolons(lines):\n  res = []\n  for line in lines:\n    res.extend(line.Split())\n  return res\n\n\nDISABLE_PATTERN = r'^#.*\\b(?:yapf:\\s*disable|fmt: ?off)\\b'\nENABLE_PATTERN = r'^#.*\\b(?:yapf:\\s*enable|fmt: ?on)\\b'\n\n\ndef _LineRangesToSet(line_ranges):\n  \"\"\"Return a set of lines in the range.\"\"\"\n\n  if line_ranges is None:\n    return None\n\n  line_set = set()\n  for low, high in sorted(line_ranges):\n    line_set.update(range(low, high + 1))\n\n  return line_set\n\n\ndef _MarkLinesToFormat(llines, lines):\n  \"\"\"Skip sections of code that we shouldn't reformat.\"\"\"\n  if lines:\n    for uwline in llines:\n      uwline.disable = not lines.intersection(\n          range(uwline.lineno, uwline.last.lineno + 1))\n\n  # Now go through the lines and disable any lines explicitly marked as\n  # disabled.\n  index = 0\n  while index < len(llines):\n    uwline = llines[index]\n    if uwline.is_comment:\n      if _DisableYAPF(uwline.first.value.strip()):\n        index += 1\n        while index < len(llines):\n          uwline = llines[index]\n          line = uwline.first.value.strip()\n          if uwline.is_comment and _EnableYAPF(line):\n            if not _DisableYAPF(line):\n              break\n          uwline.disable = True\n          index += 1\n    elif re.search(DISABLE_PATTERN, uwline.last.value.strip(), re.IGNORECASE):\n      uwline.disable = True\n    index += 1\n\n\ndef _DisableYAPF(line):\n  return (re.search(DISABLE_PATTERN,\n                    line.split('\\n')[0].strip(), re.IGNORECASE) or\n          re.search(DISABLE_PATTERN,\n                    line.split('\\n')[-1].strip(), re.IGNORECASE))\n\n\ndef _EnableYAPF(line):\n  return (re.search(ENABLE_PATTERN,\n                    line.split('\\n')[0].strip(), re.IGNORECASE) or\n          re.search(ENABLE_PATTERN,\n                    line.split('\\n')[-1].strip(), re.IGNORECASE))\n\n\ndef _GetUnifiedDiff(before, after, filename='code'):\n  \"\"\"Get a unified diff of the changes.\n\n  Arguments:\n    before: (unicode) The original source code.\n    after: (unicode) The reformatted source code.\n    filename: (unicode) The code's filename.\n\n  Returns:\n    The unified diff text.\n  \"\"\"\n  before = before.splitlines()\n  after = after.splitlines()\n  return '\\n'.join(\n      difflib.unified_diff(\n          before,\n          after,\n          filename,\n          filename,\n          '(original)',\n          '(reformatted)',\n          lineterm='')) + '\\n'\n"
  },
  {
    "path": "yapftests/__init__.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n"
  },
  {
    "path": "yapftests/blank_line_calculator_test.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.blank_line_calculator.\"\"\"\n\nimport textwrap\nimport unittest\n\nfrom yapf.yapflib import reformatter\nfrom yapf.yapflib import style\nfrom yapf.yapflib import yapf_api\n\nfrom yapftests import yapf_test_helper\n\n\nclass BasicBlankLineCalculatorTest(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):\n    style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testDecorators(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        @bork()\n\n        def foo():\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        @bork()\n        def foo():\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testComplexDecorators(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        import sys\n        @bork()\n\n        def foo():\n          pass\n        @fork()\n\n        class moo(object):\n          @bar()\n          @baz()\n\n          def method(self):\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        import sys\n\n\n        @bork()\n        def foo():\n          pass\n\n\n        @fork()\n        class moo(object):\n\n          @bar()\n          @baz()\n          def method(self):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testCodeAfterFunctionsAndClasses(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          pass\n        top_level_code = True\n        class moo(object):\n          def method_1(self):\n            pass\n          ivar_a = 42\n          ivar_b = 13\n          def method_2(self):\n            pass\n        try:\n          raise Error\n        except Error as error:\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          pass\n\n\n        top_level_code = True\n\n\n        class moo(object):\n\n          def method_1(self):\n            pass\n\n          ivar_a = 42\n          ivar_b = 13\n\n          def method_2(self):\n            pass\n\n\n        try:\n          raise Error\n        except Error as error:\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testCommentSpacing(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        # This is the first comment\n        # And it's multiline\n\n        # This is the second comment\n\n        def foo():\n          pass\n\n        # multiline before a\n        # class definition\n\n        # This is the second comment\n\n        class qux(object):\n          pass\n\n\n        # An attached comment.\n        class bar(object):\n          '''class docstring'''\n          # Comment attached to\n          # function\n          def foo(self):\n            '''Another docstring.'''\n            # Another multiline\n            # comment\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        # This is the first comment\n        # And it's multiline\n\n        # This is the second comment\n\n\n        def foo():\n          pass\n\n\n        # multiline before a\n        # class definition\n\n        # This is the second comment\n\n\n        class qux(object):\n          pass\n\n\n        # An attached comment.\n        class bar(object):\n          '''class docstring'''\n\n          # Comment attached to\n          # function\n          def foo(self):\n            '''Another docstring.'''\n            # Another multiline\n            # comment\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testCommentBeforeMethod(self):\n    code = textwrap.dedent(\"\"\"\\\n        class foo(object):\n\n          # pylint: disable=invalid-name\n          def f(self):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testCommentsBeforeClassDefs(self):\n    code = textwrap.dedent('''\\\n        \"\"\"Test.\"\"\"\n\n        # Comment\n\n\n        class Foo(object):\n          pass\n    ''')\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testCommentsBeforeDecorator(self):\n    code = textwrap.dedent(\"\"\"\\\n        # The @foo operator adds bork to a().\n        @foo()\n        def a():\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n    code = textwrap.dedent(\"\"\"\\\n        # Hello world\n\n\n        @foo()\n        def a():\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testCommentsAfterDecorator(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def _():\n            pass\n\n          @pytest.mark.xfail(reason=\"#709 and #710\")\n          # also\n          #@pytest.mark.xfail(setuptools.tests.is_ascii,\n          #    reason=\"https://github.com/pypa/setuptools/issues/706\")\n          def test_unicode_filename_in_sdist(self, sdist_unicode, tmpdir, monkeypatch):\n            pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testInnerClasses(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n      class DeployAPIClient(object):\n          class Error(Exception): pass\n\n          class TaskValidationError(Error): pass\n\n          class DeployAPIHTTPError(Error): pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n      class DeployAPIClient(object):\n\n        class Error(Exception):\n          pass\n\n        class TaskValidationError(Error):\n          pass\n\n        class DeployAPIHTTPError(Error):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testLinesOnRangeBoundary(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def A():\n          pass\n\n        def B():  # 4\n          pass  # 5\n\n        def C():\n          pass\n        def D():  # 9\n          pass  # 10\n        def E():\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def A():\n          pass\n\n\n        def B():  # 4\n          pass  # 5\n\n        def C():\n          pass\n\n\n        def D():  # 9\n          pass  # 10\n        def E():\n          pass\n    \"\"\")\n    code, changed = yapf_api.FormatCode(\n        unformatted_code, lines=[(4, 5), (9, 10)])\n    self.assertCodeEqual(expected_formatted_code, code)\n    self.assertTrue(changed)\n\n  def testLinesRangeBoundaryNotOutside(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def A():\n          pass\n\n\n\n        def B():  # 6\n          pass  # 7\n\n\n\n        def C():\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def A():\n          pass\n\n\n\n        def B():  # 6\n          pass  # 7\n\n\n\n        def C():\n          pass\n    \"\"\")\n    code, changed = yapf_api.FormatCode(unformatted_code, lines=[(6, 7)])\n    self.assertCodeEqual(expected_formatted_code, code)\n    self.assertFalse(changed)\n\n  def testLinesRangeRemove(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def A():\n          pass\n\n\n\n        def B():  # 6\n          pass  # 7\n\n\n\n\n        def C():\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def A():\n          pass\n\n\n        def B():  # 6\n          pass  # 7\n\n\n\n\n        def C():\n          pass\n    \"\"\")\n    code, changed = yapf_api.FormatCode(unformatted_code, lines=[(5, 9)])\n    self.assertCodeEqual(expected_formatted_code, code)\n    self.assertTrue(changed)\n\n  def testLinesRangeRemoveSome(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def A():\n          pass\n\n\n\n\n        def B():  # 7\n          pass  # 8\n\n\n\n\n        def C():\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def A():\n          pass\n\n\n\n        def B():  # 7\n          pass  # 8\n\n\n\n\n        def C():\n          pass\n    \"\"\")\n    code, changed = yapf_api.FormatCode(unformatted_code, lines=[(6, 9)])\n    self.assertCodeEqual(expected_formatted_code, code)\n    self.assertTrue(changed)\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/comment_splicer_test.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.comment_splicer.\"\"\"\n\nimport textwrap\nimport unittest\n\nfrom yapf.pytree import comment_splicer\nfrom yapf.pytree import pytree_utils\n\nfrom yapftests import yapf_test_helper\n\n\nclass CommentSplicerTest(yapf_test_helper.YAPFTest):\n\n  def _AssertNodeType(self, expected_type, node):\n    self.assertEqual(expected_type, pytree_utils.NodeName(node))\n\n  def _AssertNodeIsComment(self, node, text_in_comment=None):\n    if pytree_utils.NodeName(node) == 'simple_stmt':\n      self._AssertNodeType('COMMENT', node.children[0])\n      node_value = node.children[0].value\n    else:\n      self._AssertNodeType('COMMENT', node)\n      node_value = node.value\n    if text_in_comment is not None:\n      self.assertIn(text_in_comment, node_value)\n\n  def _FindNthChildNamed(self, node, name, n=1):\n    for i, child in enumerate(\n        [c for c in node.pre_order() if pytree_utils.NodeName(c) == name]):\n      if i == n - 1:\n        return child\n    raise RuntimeError('No Nth child for n={0}'.format(n))\n\n  def testSimpleInline(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo = 1 # and a comment\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    expr = tree.children[0].children[0]\n    # Check that the expected node is still expr_stmt, but now it has 4 children\n    # (before comment splicing it had 3), the last child being the comment.\n    self._AssertNodeType('expr_stmt', expr)\n    self.assertEqual(4, len(expr.children))\n    comment_node = expr.children[3]\n    self._AssertNodeIsComment(comment_node, '# and a comment')\n\n  def testSimpleSeparateLine(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo = 1\n        # first comment\n        bar = 2\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    # The comment should've been added to the root's children (now 4, including\n    # the ENDMARKER in the end.\n    self.assertEqual(4, len(tree.children))\n    comment_node = tree.children[1]\n    self._AssertNodeIsComment(comment_node)\n\n  def testTwoLineComment(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo = 1\n        # first comment\n        # second comment\n        bar = 2\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    # This is similar to the single-line standalone comment.\n    self.assertEqual(4, len(tree.children))\n    self._AssertNodeIsComment(tree.children[1])\n\n  def testCommentIsFirstChildInCompound(self):\n    code = textwrap.dedent(\"\"\"\n        if x:\n          # a comment\n          foo = 1\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    # Look into the suite node under the 'if'. We don't care about the NEWLINE\n    # leaf but the new COMMENT must be a child of the suite and before the\n    # actual code leaf.\n    if_suite = tree.children[0].children[3]\n    self._AssertNodeType('NEWLINE', if_suite.children[0])\n    self._AssertNodeIsComment(if_suite.children[1])\n\n  def testCommentIsLastChildInCompound(self):\n    code = textwrap.dedent(\"\"\"\\\n        if x:\n          foo = 1\n          # a comment\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    # Look into the suite node under the 'if'. We don't care about the DEDENT\n    # leaf but the new COMMENT must be a child of the suite and after the\n    # actual code leaf.\n    if_suite = tree.children[0].children[3]\n    self._AssertNodeType('DEDENT', if_suite.children[-1])\n    self._AssertNodeIsComment(if_suite.children[-2])\n\n  def testInlineAfterSeparateLine(self):\n    code = textwrap.dedent(\"\"\"\\\n        bar = 1\n        # line comment\n        foo = 1 # inline comment\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    # The separate line comment should become a child of the root, while\n    # the inline comment remains within its simple_node.\n    sep_comment_node = tree.children[1]\n    self._AssertNodeIsComment(sep_comment_node, '# line comment')\n\n    expr = tree.children[2].children[0]\n    inline_comment_node = expr.children[-1]\n    self._AssertNodeIsComment(inline_comment_node, '# inline comment')\n\n  def testSeparateLineAfterInline(self):\n    code = textwrap.dedent(\"\"\"\\\n        bar = 1\n        foo = 1 # inline comment\n        # line comment\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    # The separate line comment should become a child of the root, while\n    # the inline comment remains within its simple_node.\n    sep_comment_node = tree.children[-2]\n    self._AssertNodeIsComment(sep_comment_node, '# line comment')\n\n    expr = tree.children[1].children[0]\n    inline_comment_node = expr.children[-1]\n    self._AssertNodeIsComment(inline_comment_node, '# inline comment')\n\n  def testCommentBeforeDedent(self):\n    code = textwrap.dedent(\"\"\"\\\n        if bar:\n          z = 1\n        # a comment\n        j = 2\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    # The comment should go under the tree root, not under the 'if'.\n    self._AssertNodeIsComment(tree.children[1])\n    if_suite = tree.children[0].children[3]\n    self._AssertNodeType('DEDENT', if_suite.children[-1])\n\n  def testCommentBeforeDedentTwoLevel(self):\n    code = textwrap.dedent(\"\"\"\\\n        if foo:\n          if bar:\n            z = 1\n          # a comment\n        y = 1\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    if_suite = tree.children[0].children[3]\n    # The comment is in the first if_suite, not the nested if under it. It's\n    # right before the DEDENT\n    self._AssertNodeIsComment(if_suite.children[-2])\n    self._AssertNodeType('DEDENT', if_suite.children[-1])\n\n  def testCommentBeforeDedentTwoLevelImproperlyIndented(self):\n    code = textwrap.dedent(\"\"\"\\\n        if foo:\n          if bar:\n            z = 1\n           # comment 2\n        y = 1\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    # The comment here is indented by 3 spaces, which is unlike any of the\n    # surrounding statement indentation levels. The splicer attaches it to the\n    # \"closest\" parent with smaller indentation.\n    if_suite = tree.children[0].children[3]\n    # The comment is in the first if_suite, not the nested if under it. It's\n    # right before the DEDENT\n    self._AssertNodeIsComment(if_suite.children[-2])\n    self._AssertNodeType('DEDENT', if_suite.children[-1])\n\n  def testCommentBeforeDedentThreeLevel(self):\n    code = textwrap.dedent(\"\"\"\\\n        if foo:\n          if bar:\n            z = 1\n            # comment 2\n          # comment 1\n        # comment 0\n        j = 2\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    # comment 0 should go under the tree root\n    self._AssertNodeIsComment(tree.children[1], '# comment 0')\n\n    # comment 1 is in the first if_suite, right before the DEDENT\n    if_suite_1 = self._FindNthChildNamed(tree, 'suite', n=1)\n    self._AssertNodeIsComment(if_suite_1.children[-2], '# comment 1')\n    self._AssertNodeType('DEDENT', if_suite_1.children[-1])\n\n    # comment 2 is in if_suite nested under the first if suite,\n    # right before the DEDENT\n    if_suite_2 = self._FindNthChildNamed(tree, 'suite', n=2)\n    self._AssertNodeIsComment(if_suite_2.children[-2], '# comment 2')\n    self._AssertNodeType('DEDENT', if_suite_2.children[-1])\n\n  def testCommentsInClass(self):\n    code = textwrap.dedent(\"\"\"\\\n        class Foo:\n          '''docstring abc...'''\n          # top-level comment\n          def foo(): pass\n          # another comment\n    \"\"\")\n\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    class_suite = tree.children[0].children[3]\n    another_comment = class_suite.children[-2]\n    self._AssertNodeIsComment(another_comment, '# another')\n\n    # It's OK for the comment to be a child of funcdef, as long as it's\n    # the first child and thus comes before the 'def'.\n    funcdef = class_suite.children[3]\n    toplevel_comment = funcdef.children[0]\n    self._AssertNodeIsComment(toplevel_comment, '# top-level')\n\n  def testMultipleBlockComments(self):\n    code = textwrap.dedent(\"\"\"\\\n        # Block comment number 1\n\n        # Block comment number 2\n        def f():\n          pass\n    \"\"\")\n\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    funcdef = tree.children[0]\n    block_comment_1 = funcdef.children[0]\n    self._AssertNodeIsComment(block_comment_1, '# Block comment number 1')\n\n    block_comment_2 = funcdef.children[1]\n    self._AssertNodeIsComment(block_comment_2, '# Block comment number 2')\n\n  def testCommentsOnDedents(self):\n    code = textwrap.dedent(\"\"\"\\\n        class Foo(object):\n          # A comment for qux.\n          def qux(self):\n            pass\n\n          # Interim comment.\n\n          def mux(self):\n            pass\n    \"\"\")\n\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    classdef = tree.children[0]\n    class_suite = classdef.children[6]\n    qux_comment = class_suite.children[1]\n    self._AssertNodeIsComment(qux_comment, '# A comment for qux.')\n\n    interim_comment = class_suite.children[4]\n    self._AssertNodeIsComment(interim_comment, '# Interim comment.')\n\n  def testExprComments(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo( # Request fractions of an hour.\n          948.0/3600, 20)\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    trailer = self._FindNthChildNamed(tree, 'trailer', 1)\n    comment = trailer.children[1]\n    self._AssertNodeIsComment(comment, '# Request fractions of an hour.')\n\n  def testMultipleCommentsInOneExpr(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo( # com 1\n          948.0/3600, # com 2\n          20 + 12 # com 3\n          )\n    \"\"\")\n    tree = pytree_utils.ParseCodeToTree(code)\n    comment_splicer.SpliceComments(tree)\n\n    trailer = self._FindNthChildNamed(tree, 'trailer', 1)\n    self._AssertNodeIsComment(trailer.children[1], '# com 1')\n\n    arglist = self._FindNthChildNamed(tree, 'arglist', 1)\n    self._AssertNodeIsComment(arglist.children[2], '# com 2')\n\n    arith_expr = self._FindNthChildNamed(tree, 'arith_expr', 1)\n    self._AssertNodeIsComment(arith_expr.children[-1], '# com 3')\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/file_resources_test.py",
    "content": "# -*- coding: utf-8 -*-\n# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.file_resources.\"\"\"\n\nimport codecs\nimport contextlib\nimport os\nimport shutil\nimport tempfile\nimport unittest\nfrom io import BytesIO\n\nfrom yapf.yapflib import errors\nfrom yapf.yapflib import file_resources\n\nfrom yapftests import utils\nfrom yapftests import yapf_test_helper\n\n\n@contextlib.contextmanager\ndef _restore_working_dir():\n  curdir = os.getcwd()\n  try:\n    yield\n  finally:\n    os.chdir(curdir)\n\n\n@contextlib.contextmanager\ndef _exists_mocked_in_module(module, mock_implementation):\n  unmocked_exists = getattr(module, 'exists')\n  setattr(module, 'exists', mock_implementation)\n  try:\n    yield\n  finally:\n    setattr(module, 'exists', unmocked_exists)\n\n\nclass GetExcludePatternsForDir(yapf_test_helper.YAPFTest):\n\n  def setUp(self):  # pylint: disable=g-missing-super-call\n    self.test_tmpdir = tempfile.mkdtemp()\n\n  def tearDown(self):  # pylint: disable=g-missing-super-call\n    shutil.rmtree(self.test_tmpdir)\n\n  def test_get_exclude_file_patterns_from_yapfignore(self):\n    local_ignore_file = os.path.join(self.test_tmpdir, '.yapfignore')\n    ignore_patterns = ['temp/**/*.py', 'temp2/*.py']\n    with open(local_ignore_file, 'w') as f:\n      f.writelines('\\n'.join(ignore_patterns))\n\n    self.assertEqual(\n        sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),\n        sorted(ignore_patterns))\n\n  def test_get_exclude_file_patterns_from_yapfignore_with_wrong_syntax(self):\n    local_ignore_file = os.path.join(self.test_tmpdir, '.yapfignore')\n    ignore_patterns = ['temp/**/*.py', './wrong/syntax/*.py']\n    with open(local_ignore_file, 'w') as f:\n      f.writelines('\\n'.join(ignore_patterns))\n\n    with self.assertRaises(errors.YapfError):\n      file_resources.GetExcludePatternsForDir(self.test_tmpdir)\n\n  def test_get_exclude_file_patterns_from_pyproject(self):\n    local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')\n    ignore_patterns = ['temp/**/*.py', 'temp2/*.py']\n    with open(local_ignore_file, 'w') as f:\n      f.write('[tool.yapfignore]\\n')\n      f.write('ignore_patterns=[')\n      f.writelines('\\n,'.join(['\"{}\"'.format(p) for p in ignore_patterns]))\n      f.write(']')\n\n    self.assertEqual(\n        sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),\n        sorted(ignore_patterns))\n\n  def test_get_exclude_file_patterns_from_pyproject_no_ignore_section(self):\n    local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')\n    ignore_patterns = []\n    open(local_ignore_file, 'w').close()\n\n    self.assertEqual(\n        sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),\n        sorted(ignore_patterns))\n\n  def test_get_exclude_file_patterns_from_pyproject_ignore_section_empty(self):\n    local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')\n    ignore_patterns = []\n    with open(local_ignore_file, 'w') as f:\n      f.write('[tool.yapfignore]\\n')\n\n    self.assertEqual(\n        sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),\n        sorted(ignore_patterns))\n\n  def test_get_exclude_file_patterns_with_no_config_files(self):\n    ignore_patterns = []\n\n    self.assertEqual(\n        sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),\n        sorted(ignore_patterns))\n\n\nclass GetDefaultStyleForDirTest(yapf_test_helper.YAPFTest):\n\n  def setUp(self):  # pylint: disable=g-missing-super-call\n    self.test_tmpdir = tempfile.mkdtemp()\n\n  def tearDown(self):  # pylint: disable=g-missing-super-call\n    shutil.rmtree(self.test_tmpdir)\n\n  def test_no_local_style(self):\n    test_file = os.path.join(self.test_tmpdir, 'file.py')\n    style_name = file_resources.GetDefaultStyleForDir(test_file)\n    self.assertEqual(style_name, 'pep8')\n\n  def test_no_local_style_custom_default(self):\n    test_file = os.path.join(self.test_tmpdir, 'file.py')\n    style_name = file_resources.GetDefaultStyleForDir(\n        test_file, default_style='custom-default')\n    self.assertEqual(style_name, 'custom-default')\n\n  def test_with_local_style(self):\n    # Create an empty .style.yapf file in test_tmpdir\n    style_file = os.path.join(self.test_tmpdir, '.style.yapf')\n    open(style_file, 'w').close()\n\n    test_filename = os.path.join(self.test_tmpdir, 'file.py')\n    self.assertEqual(style_file,\n                     file_resources.GetDefaultStyleForDir(test_filename))\n\n    test_filename = os.path.join(self.test_tmpdir, 'dir1', 'file.py')\n    self.assertEqual(style_file,\n                     file_resources.GetDefaultStyleForDir(test_filename))\n\n  def test_setup_config(self):\n    # An empty setup.cfg file should not be used\n    setup_config = os.path.join(self.test_tmpdir, 'setup.cfg')\n    open(setup_config, 'w').close()\n\n    test_dir = os.path.join(self.test_tmpdir, 'dir1')\n    style_name = file_resources.GetDefaultStyleForDir(test_dir)\n    self.assertEqual(style_name, 'pep8')\n\n    # One with a '[yapf]' section should be used\n    with open(setup_config, 'w') as f:\n      f.write('[yapf]\\n')\n    self.assertEqual(setup_config,\n                     file_resources.GetDefaultStyleForDir(test_dir))\n\n  def test_pyproject_toml(self):\n    pyproject_toml = os.path.join(self.test_tmpdir, 'pyproject.toml')\n    open(pyproject_toml, 'w').close()\n\n    test_dir = os.path.join(self.test_tmpdir, 'dir1')\n    style_name = file_resources.GetDefaultStyleForDir(test_dir)\n    self.assertEqual(style_name, 'pep8')\n\n    # One with a '[tool.yapf]' section should be used\n    with open(pyproject_toml, 'w') as f:\n      f.write('[tool.yapf]\\n')\n    self.assertEqual(pyproject_toml,\n                     file_resources.GetDefaultStyleForDir(test_dir))\n\n  def test_local_style_at_root(self):\n    # Test behavior of files located on the root, and under root.\n    rootdir = os.path.abspath(os.path.sep)\n    test_dir_at_root = os.path.join(rootdir, 'dir1')\n    test_dir_under_root = os.path.join(rootdir, 'dir1', 'dir2')\n\n    # Fake placing only a style file at the root by mocking `os.path.exists`.\n    style_file = os.path.join(rootdir, '.style.yapf')\n\n    def mock_exists_implementation(path):\n      return path == style_file\n\n    with _exists_mocked_in_module(file_resources.os.path,\n                                  mock_exists_implementation):\n      # Both files should find the style file at the root.\n      default_style_at_root = file_resources.GetDefaultStyleForDir(\n          test_dir_at_root)\n      self.assertEqual(style_file, default_style_at_root)\n      default_style_under_root = file_resources.GetDefaultStyleForDir(\n          test_dir_under_root)\n      self.assertEqual(style_file, default_style_under_root)\n\n\ndef _touch_files(filenames):\n  for name in filenames:\n    open(name, 'a').close()\n\n\nclass GetCommandLineFilesTest(yapf_test_helper.YAPFTest):\n\n  def setUp(self):  # pylint: disable=g-missing-super-call\n    self.test_tmpdir = tempfile.mkdtemp()\n    self.old_dir = os.getcwd()\n\n  def tearDown(self):  # pylint: disable=g-missing-super-call\n    os.chdir(self.old_dir)\n    shutil.rmtree(self.test_tmpdir)\n\n  def _make_test_dir(self, name):\n    fullpath = os.path.normpath(os.path.join(self.test_tmpdir, name))\n    os.makedirs(fullpath)\n    return fullpath\n\n  def test_find_files_not_dirs(self):\n    tdir1 = self._make_test_dir('test1')\n    tdir2 = self._make_test_dir('test2')\n    file1 = os.path.join(tdir1, 'testfile1.py')\n    file2 = os.path.join(tdir2, 'testfile2.py')\n    _touch_files([file1, file2])\n\n    self.assertEqual(\n        file_resources.GetCommandLineFiles([file1, file2],\n                                           recursive=False,\n                                           exclude=None), [file1, file2])\n    self.assertEqual(\n        file_resources.GetCommandLineFiles([file1, file2],\n                                           recursive=True,\n                                           exclude=None), [file1, file2])\n\n  def test_nonrecursive_find_in_dir(self):\n    tdir1 = self._make_test_dir('test1')\n    tdir2 = self._make_test_dir('test1/foo')\n    file1 = os.path.join(tdir1, 'testfile1.py')\n    file2 = os.path.join(tdir2, 'testfile2.py')\n    _touch_files([file1, file2])\n\n    self.assertRaises(\n        errors.YapfError,\n        file_resources.GetCommandLineFiles,\n        command_line_file_list=[tdir1],\n        recursive=False,\n        exclude=None)\n\n  def test_recursive_find_in_dir(self):\n    tdir1 = self._make_test_dir('test1')\n    tdir2 = self._make_test_dir('test2/testinner/')\n    tdir3 = self._make_test_dir('test3/foo/bar/bas/xxx')\n    files = [\n        os.path.join(tdir1, 'testfile1.py'),\n        os.path.join(tdir2, 'testfile2.py'),\n        os.path.join(tdir3, 'testfile3.py'),\n    ]\n    _touch_files(files)\n\n    self.assertEqual(\n        sorted(\n            file_resources.GetCommandLineFiles([self.test_tmpdir],\n                                               recursive=True,\n                                               exclude=None)), sorted(files))\n\n  def test_recursive_find_in_dir_with_exclude(self):\n    tdir1 = self._make_test_dir('test1')\n    tdir2 = self._make_test_dir('test2/testinner/')\n    tdir3 = self._make_test_dir('test3/foo/bar/bas/xxx')\n    files = [\n        os.path.join(tdir1, 'testfile1.py'),\n        os.path.join(tdir2, 'testfile2.py'),\n        os.path.join(tdir3, 'testfile3.py'),\n    ]\n    _touch_files(files)\n\n    self.assertEqual(\n        sorted(\n            file_resources.GetCommandLineFiles([self.test_tmpdir],\n                                               recursive=True,\n                                               exclude=['*test*3.py'])),\n        sorted([\n            os.path.join(tdir1, 'testfile1.py'),\n            os.path.join(tdir2, 'testfile2.py'),\n        ]))\n\n  def test_find_with_excluded_hidden_dirs(self):\n    tdir1 = self._make_test_dir('.test1')\n    tdir2 = self._make_test_dir('test_2')\n    tdir3 = self._make_test_dir('test.3')\n    files = [\n        os.path.join(tdir1, 'testfile1.py'),\n        os.path.join(tdir2, 'testfile2.py'),\n        os.path.join(tdir3, 'testfile3.py'),\n    ]\n    _touch_files(files)\n\n    actual = file_resources.GetCommandLineFiles([self.test_tmpdir],\n                                                recursive=True,\n                                                exclude=['*.test1*'])\n\n    self.assertEqual(\n        sorted(actual),\n        sorted([\n            os.path.join(tdir2, 'testfile2.py'),\n            os.path.join(tdir3, 'testfile3.py'),\n        ]))\n\n  def test_find_with_excluded_hidden_dirs_relative(self):\n    \"\"\"Test find with excluded hidden dirs.\n\n    A regression test against a specific case where a hidden directory (one\n    beginning with a period) is being excluded, but it is also an immediate\n    child of the current directory which has been specified in a relative\n    manner.\n\n    At its core, the bug has to do with overzealous stripping of \"./foo\" so that\n    it removes too much from \"./.foo\" .\n    \"\"\"\n    tdir1 = self._make_test_dir('.test1')\n    tdir2 = self._make_test_dir('test_2')\n    tdir3 = self._make_test_dir('test.3')\n    files = [\n        os.path.join(tdir1, 'testfile1.py'),\n        os.path.join(tdir2, 'testfile2.py'),\n        os.path.join(tdir3, 'testfile3.py'),\n    ]\n    _touch_files(files)\n\n    # We must temporarily change the current directory, so that we test against\n    # patterns like ./.test1/file instead of /tmp/foo/.test1/file\n    with _restore_working_dir():\n\n      os.chdir(self.test_tmpdir)\n      actual = file_resources.GetCommandLineFiles(\n          [os.path.relpath(self.test_tmpdir)],\n          recursive=True,\n          exclude=['*.test1*'])\n\n      self.assertEqual(\n          sorted(actual),\n          sorted([\n              os.path.join(\n                  os.path.relpath(self.test_tmpdir), os.path.basename(tdir2),\n                  'testfile2.py'),\n              os.path.join(\n                  os.path.relpath(self.test_tmpdir), os.path.basename(tdir3),\n                  'testfile3.py'),\n          ]))\n\n  def test_find_with_excluded_dirs(self):\n    tdir1 = self._make_test_dir('test1')\n    tdir2 = self._make_test_dir('test2/testinner/')\n    tdir3 = self._make_test_dir('test3/foo/bar/bas/xxx')\n    files = [\n        os.path.join(tdir1, 'testfile1.py'),\n        os.path.join(tdir2, 'testfile2.py'),\n        os.path.join(tdir3, 'testfile3.py'),\n    ]\n    _touch_files(files)\n\n    os.chdir(self.test_tmpdir)\n\n    found = sorted(\n        file_resources.GetCommandLineFiles(['test1', 'test2', 'test3'],\n                                           recursive=True,\n                                           exclude=[\n                                               'test1',\n                                               'test2/testinner/',\n                                           ]))\n\n    self.assertEqual(\n        found, ['test3/foo/bar/bas/xxx/testfile3.py'.replace('/', os.path.sep)])\n\n    found = sorted(\n        file_resources.GetCommandLineFiles(['.'],\n                                           recursive=True,\n                                           exclude=[\n                                               'test1',\n                                               'test3',\n                                           ]))\n\n    self.assertEqual(\n        found, ['./test2/testinner/testfile2.py'.replace('/', os.path.sep)])\n\n  def test_find_with_excluded_current_dir(self):\n    with self.assertRaises(errors.YapfError):\n      file_resources.GetCommandLineFiles([], False, exclude=['./z'])\n\n\nclass IsPythonFileTest(yapf_test_helper.YAPFTest):\n\n  def setUp(self):  # pylint: disable=g-missing-super-call\n    self.test_tmpdir = tempfile.mkdtemp()\n\n  def tearDown(self):  # pylint: disable=g-missing-super-call\n    shutil.rmtree(self.test_tmpdir)\n\n  def test_with_py_extension(self):\n    file1 = os.path.join(self.test_tmpdir, 'testfile1.py')\n    self.assertTrue(file_resources.IsPythonFile(file1))\n\n  def test_empty_without_py_extension(self):\n    file1 = os.path.join(self.test_tmpdir, 'testfile1')\n    self.assertFalse(file_resources.IsPythonFile(file1))\n    file2 = os.path.join(self.test_tmpdir, 'testfile1.rb')\n    self.assertFalse(file_resources.IsPythonFile(file2))\n\n  def test_python_shebang(self):\n    file1 = os.path.join(self.test_tmpdir, 'testfile1')\n    with open(file1, 'w') as f:\n      f.write('#!/usr/bin/python\\n')\n    self.assertTrue(file_resources.IsPythonFile(file1))\n\n    file2 = os.path.join(self.test_tmpdir, 'testfile2.run')\n    with open(file2, 'w') as f:\n      f.write('#! /bin/python2\\n')\n    self.assertTrue(file_resources.IsPythonFile(file1))\n\n  def test_with_latin_encoding(self):\n    file1 = os.path.join(self.test_tmpdir, 'testfile1')\n    with codecs.open(file1, mode='w', encoding='latin-1') as f:\n      f.write('#! /bin/python2\\n')\n    self.assertTrue(file_resources.IsPythonFile(file1))\n\n  def test_with_invalid_encoding(self):\n    file1 = os.path.join(self.test_tmpdir, 'testfile1')\n    with open(file1, 'w') as f:\n      f.write('#! /bin/python2\\n')\n      f.write('# -*- coding: iso-3-14159 -*-\\n')\n    self.assertFalse(file_resources.IsPythonFile(file1))\n\n\nclass IsIgnoredTest(yapf_test_helper.YAPFTest):\n\n  def test_root_path(self):\n    self.assertTrue(file_resources.IsIgnored('media', ['media']))\n    self.assertFalse(file_resources.IsIgnored('media', ['media/*']))\n\n  def test_sub_path(self):\n    self.assertTrue(file_resources.IsIgnored('media/a', ['*/a']))\n    self.assertTrue(file_resources.IsIgnored('media/b', ['media/*']))\n    self.assertTrue(file_resources.IsIgnored('media/b/c', ['*/*/c']))\n\n  def test_trailing_slash(self):\n    self.assertTrue(file_resources.IsIgnored('z', ['z']))\n    self.assertTrue(file_resources.IsIgnored('z', ['z' + os.path.sep]))\n\n\nclass BufferedByteStream(object):\n\n  def __init__(self):\n    self.stream = BytesIO()\n\n  def getvalue(self):  # pylint: disable=invalid-name\n    return self.stream.getvalue().decode('utf-8')\n\n  @property\n  def buffer(self):\n    return self.stream\n\n\nclass WriteReformattedCodeTest(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):  # pylint: disable=g-missing-super-call\n    cls.test_tmpdir = tempfile.mkdtemp()\n\n  @classmethod\n  def tearDownClass(cls):  # pylint: disable=g-missing-super-call\n    shutil.rmtree(cls.test_tmpdir)\n\n  def test_write_to_file(self):\n    s = 'foobar\\n'\n    with utils.NamedTempFile(dirname=self.test_tmpdir) as (f, fname):\n      file_resources.WriteReformattedCode(\n          fname, s, in_place=True, encoding='utf-8')\n      f.flush()\n\n      with open(fname) as f2:\n        self.assertEqual(f2.read(), s)\n\n  def test_write_to_stdout(self):\n    s = 'foobar'\n    stream = BufferedByteStream()\n    with utils.stdout_redirector(stream):\n      file_resources.WriteReformattedCode(\n          None, s, in_place=False, encoding='utf-8')\n    self.assertEqual(stream.getvalue(), s)\n\n  def test_write_encoded_to_stdout(self):\n    s = '\\ufeff# -*- coding: utf-8 -*-\\nresult = \"passed\"\\n'  # pylint: disable=anomalous-unicode-escape-in-string # noqa\n    stream = BufferedByteStream()\n    with utils.stdout_redirector(stream):\n      file_resources.WriteReformattedCode(\n          None, s, in_place=False, encoding='utf-8')\n    self.assertEqual(stream.getvalue(), s)\n\n\nclass LineEndingTest(yapf_test_helper.YAPFTest):\n\n  def test_line_ending_linefeed(self):\n    lines = ['spam\\n', 'spam\\n']\n    actual = file_resources.LineEnding(lines)\n    self.assertEqual(actual, '\\n')\n\n  def test_line_ending_carriage_return(self):\n    lines = ['spam\\r', 'spam\\r']\n    actual = file_resources.LineEnding(lines)\n    self.assertEqual(actual, '\\r')\n\n  def test_line_ending_combo(self):\n    lines = ['spam\\r\\n', 'spam\\r\\n']\n    actual = file_resources.LineEnding(lines)\n    self.assertEqual(actual, '\\r\\n')\n\n  def test_line_ending_weighted(self):\n    lines = [\n        'spam\\n',\n        'spam\\n',\n        'spam\\r',\n        'spam\\r\\n',\n    ]\n    actual = file_resources.LineEnding(lines)\n    self.assertEqual(actual, '\\n')\n\n  def test_line_ending_empty(self):\n    lines = []\n    actual = file_resources.LineEnding(lines)\n    self.assertEqual(actual, '\\n')\n\n  def test_line_ending_no_newline(self):\n    lines = ['spam']\n    actual = file_resources.LineEnding(lines)\n    self.assertEqual(actual, '\\n')\n\n  def test_line_ending_tie(self):\n    lines = [\n        'spam\\n',\n        'spam\\n',\n        'spam\\r\\n',\n        'spam\\r\\n',\n    ]\n    actual = file_resources.LineEnding(lines)\n    self.assertEqual(actual, '\\n')\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/format_decision_state_test.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.format_decision_state.\"\"\"\n\nimport textwrap\nimport unittest\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.yapflib import format_decision_state\nfrom yapf.yapflib import logical_line\nfrom yapf.yapflib import style\n\nfrom yapftests import yapf_test_helper\n\n\nclass FormatDecisionStateTest(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):\n    style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testSimpleFunctionDefWithNoSplitting(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f(a, b):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    lline = logical_line.LogicalLine(0, _FilterLine(llines[0]))\n    lline.CalculateFormattingInformation()\n\n    # Add: 'f'\n    state = format_decision_state.FormatDecisionState(lline, 0)\n    state.MoveStateToNextToken()\n    self.assertEqual('f', state.next_token.value)\n    self.assertFalse(state.CanSplit(False))\n\n    # Add: '('\n    state.AddTokenToState(False, True)\n    self.assertEqual('(', state.next_token.value)\n    self.assertFalse(state.CanSplit(False))\n    self.assertFalse(state.MustSplit())\n\n    # Add: 'a'\n    state.AddTokenToState(False, True)\n    self.assertEqual('a', state.next_token.value)\n    self.assertTrue(state.CanSplit(False))\n    self.assertFalse(state.MustSplit())\n\n    # Add: ','\n    state.AddTokenToState(False, True)\n    self.assertEqual(',', state.next_token.value)\n    self.assertFalse(state.CanSplit(False))\n    self.assertFalse(state.MustSplit())\n\n    # Add: 'b'\n    state.AddTokenToState(False, True)\n    self.assertEqual('b', state.next_token.value)\n    self.assertTrue(state.CanSplit(False))\n    self.assertFalse(state.MustSplit())\n\n    # Add: ')'\n    state.AddTokenToState(False, True)\n    self.assertEqual(')', state.next_token.value)\n    self.assertTrue(state.CanSplit(False))\n    self.assertFalse(state.MustSplit())\n\n    # Add: ':'\n    state.AddTokenToState(False, True)\n    self.assertEqual(':', state.next_token.value)\n    self.assertFalse(state.CanSplit(False))\n    self.assertFalse(state.MustSplit())\n\n    clone = state.Clone()\n    self.assertEqual(repr(state), repr(clone))\n\n  def testSimpleFunctionDefWithSplitting(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f(a, b):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    lline = logical_line.LogicalLine(0, _FilterLine(llines[0]))\n    lline.CalculateFormattingInformation()\n\n    # Add: 'f'\n    state = format_decision_state.FormatDecisionState(lline, 0)\n    state.MoveStateToNextToken()\n    self.assertEqual('f', state.next_token.value)\n    self.assertFalse(state.CanSplit(False))\n\n    # Add: '('\n    state.AddTokenToState(True, True)\n    self.assertEqual('(', state.next_token.value)\n    self.assertFalse(state.CanSplit(False))\n\n    # Add: 'a'\n    state.AddTokenToState(True, True)\n    self.assertEqual('a', state.next_token.value)\n    self.assertTrue(state.CanSplit(False))\n\n    # Add: ','\n    state.AddTokenToState(True, True)\n    self.assertEqual(',', state.next_token.value)\n    self.assertFalse(state.CanSplit(False))\n\n    # Add: 'b'\n    state.AddTokenToState(True, True)\n    self.assertEqual('b', state.next_token.value)\n    self.assertTrue(state.CanSplit(False))\n\n    # Add: ')'\n    state.AddTokenToState(True, True)\n    self.assertEqual(')', state.next_token.value)\n    self.assertTrue(state.CanSplit(False))\n\n    # Add: ':'\n    state.AddTokenToState(True, True)\n    self.assertEqual(':', state.next_token.value)\n    self.assertFalse(state.CanSplit(False))\n\n    clone = state.Clone()\n    self.assertEqual(repr(state), repr(clone))\n\n\ndef _FilterLine(lline):\n  \"\"\"Filter out nonsemantic tokens from the LogicalLines.\"\"\"\n  return [\n      ft for ft in lline.tokens\n      if ft.name not in pytree_utils.NONSEMANTIC_TOKENS\n  ]\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/format_token_test.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.format_token.\"\"\"\n\nimport unittest\n\nfrom yapf_third_party._ylib2to3 import pytree\nfrom yapf_third_party._ylib2to3.pgen2 import token\n\nfrom yapf.yapflib import format_token\n\nfrom yapftests import yapf_test_helper\n\n\nclass TabbedContinuationAlignPaddingTest(yapf_test_helper.YAPFTest):\n\n  def testSpace(self):\n    align_style = 'SPACE'\n\n    pad = format_token._TabbedContinuationAlignPadding(0, align_style, 2)\n    self.assertEqual(pad, '')\n\n    pad = format_token._TabbedContinuationAlignPadding(2, align_style, 2)\n    self.assertEqual(pad, ' ' * 2)\n\n    pad = format_token._TabbedContinuationAlignPadding(5, align_style, 2)\n    self.assertEqual(pad, ' ' * 5)\n\n  def testFixed(self):\n    align_style = 'FIXED'\n\n    pad = format_token._TabbedContinuationAlignPadding(0, align_style, 4)\n    self.assertEqual(pad, '')\n\n    pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4)\n    self.assertEqual(pad, '\\t')\n\n    pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4)\n    self.assertEqual(pad, '\\t' * 2)\n\n  def testVAlignRight(self):\n    align_style = 'VALIGN-RIGHT'\n\n    pad = format_token._TabbedContinuationAlignPadding(0, align_style, 4)\n    self.assertEqual(pad, '')\n\n    pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4)\n    self.assertEqual(pad, '\\t')\n\n    pad = format_token._TabbedContinuationAlignPadding(4, align_style, 4)\n    self.assertEqual(pad, '\\t')\n\n    pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4)\n    self.assertEqual(pad, '\\t' * 2)\n\n\nclass FormatTokenTest(yapf_test_helper.YAPFTest):\n\n  def testSimple(self):\n    tok = format_token.FormatToken(\n        pytree.Leaf(token.STRING, \"'hello world'\"), 'STRING')\n    self.assertEqual(\n        \"FormatToken(name=DOCSTRING, value='hello world', column=0, \"\n        'lineno=0, splitpenalty=0)', str(tok))\n    self.assertTrue(tok.is_string)\n\n    tok = format_token.FormatToken(\n        pytree.Leaf(token.COMMENT, '# A comment'), 'COMMENT')\n    self.assertEqual(\n        'FormatToken(name=COMMENT, value=# A comment, column=0, '\n        'lineno=0, splitpenalty=0)', str(tok))\n    self.assertTrue(tok.is_comment)\n\n  def testIsMultilineString(self):\n    tok = format_token.FormatToken(\n        pytree.Leaf(token.STRING, '\"\"\"hello\"\"\"'), 'STRING')\n    self.assertTrue(tok.is_multiline_string)\n\n    tok = format_token.FormatToken(\n        pytree.Leaf(token.STRING, 'r\"\"\"hello\"\"\"'), 'STRING')\n    self.assertTrue(tok.is_multiline_string)\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/line_joiner_test.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.line_joiner.\"\"\"\n\nimport textwrap\nimport unittest\n\nfrom yapf.yapflib import line_joiner\nfrom yapf.yapflib import style\n\nfrom yapftests import yapf_test_helper\n\n\nclass LineJoinerTest(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):\n    style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def _CheckLineJoining(self, code, join_lines):\n    \"\"\"Check that the given LogicalLines are joined as expected.\n\n    Arguments:\n      code: The code to check to see if we can join it.\n      join_lines: True if we expect the lines to be joined.\n    \"\"\"\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(line_joiner.CanMergeMultipleLines(llines), join_lines)\n\n  def testSimpleSingleLineStatement(self):\n    code = textwrap.dedent(\"\"\"\\\n        if isinstance(a, int): continue\n    \"\"\")\n    self._CheckLineJoining(code, join_lines=True)\n\n  def testSimpleMultipleLineStatement(self):\n    code = textwrap.dedent(\"\"\"\\\n        if isinstance(b, int):\n            continue\n    \"\"\")\n    self._CheckLineJoining(code, join_lines=False)\n\n  def testSimpleMultipleLineComplexStatement(self):\n    code = textwrap.dedent(\"\"\"\\\n        if isinstance(c, int):\n            while True:\n                continue\n    \"\"\")\n    self._CheckLineJoining(code, join_lines=False)\n\n  def testSimpleMultipleLineStatementWithComment(self):\n    code = textwrap.dedent(\"\"\"\\\n        if isinstance(d, int): continue  # We're pleased that d's an int.\n    \"\"\")\n    self._CheckLineJoining(code, join_lines=True)\n\n  def testSimpleMultipleLineStatementWithLargeIndent(self):\n    code = textwrap.dedent(\"\"\"\\\n        if isinstance(e, int):    continue\n    \"\"\")\n    self._CheckLineJoining(code, join_lines=True)\n\n  def testOverColumnLimit(self):\n    code = textwrap.dedent(\"\"\"\\\n        if instance(bbbbbbbbbbbbbbbbbbbbbbbbb, int): cccccccccccccccccccccccccc = ddddddddddddddddddddd\n    \"\"\")  # noqa\n    self._CheckLineJoining(code, join_lines=False)\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/logical_line_test.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.logical_line.\"\"\"\n\nimport textwrap\nimport unittest\n\nfrom yapf_third_party._ylib2to3 import pytree\nfrom yapf_third_party._ylib2to3.pgen2 import token\n\nfrom yapf.pytree import split_penalty\nfrom yapf.yapflib import format_token\nfrom yapf.yapflib import logical_line\n\nfrom yapftests import yapf_test_helper\n\n\nclass LogicalLineBasicTest(yapf_test_helper.YAPFTest):\n\n  def testConstruction(self):\n    toks = _MakeFormatTokenList([(token.DOT, '.', 'DOT'),\n                                 (token.VBAR, '|', 'VBAR')])\n    lline = logical_line.LogicalLine(20, toks)\n    self.assertEqual(20, lline.depth)\n    self.assertEqual(['DOT', 'VBAR'], [tok.name for tok in lline.tokens])\n\n  def testFirstLast(self):\n    toks = _MakeFormatTokenList([(token.DOT, '.', 'DOT'),\n                                 (token.LPAR, '(', 'LPAR'),\n                                 (token.VBAR, '|', 'VBAR')])\n    lline = logical_line.LogicalLine(20, toks)\n    self.assertEqual(20, lline.depth)\n    self.assertEqual('DOT', lline.first.name)\n    self.assertEqual('VBAR', lline.last.name)\n\n  def testAsCode(self):\n    toks = _MakeFormatTokenList([(token.DOT, '.', 'DOT'),\n                                 (token.LPAR, '(', 'LPAR'),\n                                 (token.VBAR, '|', 'VBAR')])\n    lline = logical_line.LogicalLine(2, toks)\n    self.assertEqual('    . ( |', lline.AsCode())\n\n  def testAppendToken(self):\n    lline = logical_line.LogicalLine(0)\n    lline.AppendToken(_MakeFormatTokenLeaf(token.LPAR, '(', 'LPAR'))\n    lline.AppendToken(_MakeFormatTokenLeaf(token.RPAR, ')', 'RPAR'))\n    self.assertEqual(['LPAR', 'RPAR'], [tok.name for tok in lline.tokens])\n\n\nclass LogicalLineFormattingInformationTest(yapf_test_helper.YAPFTest):\n\n  def testFuncDef(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f(a, b):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n\n    f = llines[0].tokens[1]\n    self.assertFalse(f.can_break_before)\n    self.assertFalse(f.must_break_before)\n    self.assertEqual(f.split_penalty, split_penalty.UNBREAKABLE)\n\n    lparen = llines[0].tokens[2]\n    self.assertFalse(lparen.can_break_before)\n    self.assertFalse(lparen.must_break_before)\n    self.assertEqual(lparen.split_penalty, split_penalty.UNBREAKABLE)\n\n\ndef _MakeFormatTokenLeaf(token_type, token_value, name):\n  return format_token.FormatToken(pytree.Leaf(token_type, token_value), name)\n\n\ndef _MakeFormatTokenList(token_type_values):\n  return [\n      _MakeFormatTokenLeaf(token_type, token_value, token_name)\n      for token_type, token_value, token_name in token_type_values\n  ]\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/main_test.py",
    "content": "# -*- coding: utf-8 -*-\n# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.__init__.main.\"\"\"\n\nimport sys\nimport unittest\nfrom contextlib import contextmanager\nfrom io import StringIO\n\nimport yapf\n\nfrom yapftests import yapf_test_helper\n\n\nclass IO(object):\n  \"\"\"IO is a thin wrapper around StringIO.\n\n  This is strictly to wrap the Python 3 StringIO object so that it can supply a\n  \"buffer\" attribute.\n  \"\"\"\n\n  class Buffer(object):\n\n    def __init__(self):\n      self.string_io = StringIO()\n\n    def write(self, s):\n      if isinstance(s, bytes):\n        s = str(s, 'utf-8')\n      self.string_io.write(s)\n\n    def getvalue(self):\n      return self.string_io.getvalue()\n\n  def __init__(self):\n    self.buffer = self.Buffer()\n\n  def write(self, s):\n    self.buffer.write(s)\n\n  def getvalue(self):\n    return self.buffer.getvalue()\n\n\n@contextmanager\ndef captured_output():\n  new_out, new_err = IO(), IO()\n  old_out, old_err = sys.stdout, sys.stderr\n  try:\n    sys.stdout, sys.stderr = new_out, new_err\n    yield sys.stdout, sys.stderr\n  finally:\n    sys.stdout, sys.stderr = old_out, old_err\n\n\n@contextmanager\ndef patched_input(code):\n  \"\"\"Monkey patch code as though it were coming from stdin.\"\"\"\n\n  def lines():\n    for line in code.splitlines():\n      yield line\n    raise EOFError()\n\n  def patch_raw_input(lines=lines()):\n    return next(lines)\n\n  try:\n    orig_raw_import = yapf._raw_input\n    yapf._raw_input = patch_raw_input\n    yield\n  finally:\n    yapf._raw_input = orig_raw_import\n\n\nclass RunMainTest(yapf_test_helper.YAPFTest):\n\n  def testShouldHandleYapfError(self):\n    \"\"\"run_main should handle YapfError and sys.exit(1).\"\"\"\n    expected_message = 'yapf: input filenames did not match any python files\\n'\n    sys.argv = ['yapf', 'foo.c']\n    with captured_output() as (out, err):\n      with self.assertRaises(SystemExit):\n        yapf.run_main()\n      self.assertEqual(out.getvalue(), '')\n      self.assertEqual(err.getvalue(), expected_message)\n\n\nclass MainTest(yapf_test_helper.YAPFTest):\n\n  def testNoPythonFilesMatched(self):\n    with self.assertRaisesRegex(yapf.errors.YapfError,\n                                'did not match any python files'):\n      yapf.main(['yapf', 'foo.c'])\n\n  def testEchoInput(self):\n    code = 'a = 1\\nb = 2\\n'\n    with patched_input(code):\n      with captured_output() as (out, _):\n        ret = yapf.main([])\n        self.assertEqual(ret, 0)\n        self.assertEqual(out.getvalue(), code)\n\n  def testEchoInputWithStyle(self):\n    code = 'def f(a = 1\\n\\n):\\n    return 2*a\\n'\n    yapf_code = 'def f(a=1):\\n  return 2 * a\\n'\n    with patched_input(code):\n      with captured_output() as (out, _):\n        ret = yapf.main(['-', '--style=yapf'])\n        self.assertEqual(ret, 0)\n        self.assertEqual(out.getvalue(), yapf_code)\n\n  def testEchoBadInput(self):\n    bad_syntax = '  a = 1\\n'\n    with patched_input(bad_syntax):\n      with captured_output() as (_, _):\n        with self.assertRaisesRegex(yapf.errors.YapfError, 'unexpected indent'):\n          yapf.main([])\n\n  def testHelp(self):\n    with captured_output() as (out, _):\n      ret = yapf.main(['-', '--style-help', '--style=pep8'])\n      self.assertEqual(ret, 0)\n      help_message = out.getvalue()\n      self.assertIn('indent_width=4', help_message)\n      self.assertIn('The number of spaces required before a trailing comment.',\n                    help_message)\n"
  },
  {
    "path": "yapftests/pytree_unwrapper_test.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.pytree_unwrapper.\"\"\"\n\nimport textwrap\nimport unittest\n\nfrom yapf.pytree import pytree_utils\n\nfrom yapftests import yapf_test_helper\n\n\nclass PytreeUnwrapperTest(yapf_test_helper.YAPFTest):\n\n  def _CheckLogicalLines(self, llines, list_of_expected):\n    \"\"\"Check that the given LogicalLines match expectations.\n\n    Args:\n      llines: list of LogicalLine\n      list_of_expected: list of (depth, values) pairs. Non-semantic tokens are\n        filtered out from the expected values.\n    \"\"\"\n    actual = []\n    for lline in llines:\n      filtered_values = [\n          ft.value\n          for ft in lline.tokens\n          if ft.name not in pytree_utils.NONSEMANTIC_TOKENS\n      ]\n      actual.append((lline.depth, filtered_values))\n\n    self.assertEqual(list_of_expected, actual)\n\n  def testSimpleFileScope(self):\n    code = textwrap.dedent(\"\"\"\\\n        x = 1\n        # a comment\n        y = 2\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['x', '=', '1']),\n        (0, ['# a comment']),\n        (0, ['y', '=', '2']),\n    ])\n\n  def testSimpleMultilineStatement(self):\n    code = textwrap.dedent(\"\"\"\\\n        y = (1 +\n             x)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['y', '=', '(', '1', '+', 'x', ')']),\n    ])\n\n  def testFileScopeWithInlineComment(self):\n    code = textwrap.dedent(\"\"\"\\\n        x = 1    # a comment\n        y = 2\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['x', '=', '1', '# a comment']),\n        (0, ['y', '=', '2']),\n    ])\n\n  def testSimpleIf(self):\n    code = textwrap.dedent(\"\"\"\\\n        if foo:\n            x = 1\n            y = 2\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['if', 'foo', ':']),\n        (1, ['x', '=', '1']),\n        (1, ['y', '=', '2']),\n    ])\n\n  def testSimpleIfWithComments(self):\n    code = textwrap.dedent(\"\"\"\\\n        # c1\n        if foo: # c2\n            x = 1\n            y = 2\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['# c1']),\n        (0, ['if', 'foo', ':', '# c2']),\n        (1, ['x', '=', '1']),\n        (1, ['y', '=', '2']),\n    ])\n\n  def testIfWithCommentsInside(self):\n    code = textwrap.dedent(\"\"\"\\\n        if foo:\n            # c1\n            x = 1 # c2\n            # c3\n            y = 2\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['if', 'foo', ':']),\n        (1, ['# c1']),\n        (1, ['x', '=', '1', '# c2']),\n        (1, ['# c3']),\n        (1, ['y', '=', '2']),\n    ])\n\n  def testIfElifElse(self):\n    code = textwrap.dedent(\"\"\"\\\n        if x:\n          x = 1 # c1\n        elif y: # c2\n          y = 1\n        else:\n          # c3\n          z = 1\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['if', 'x', ':']),\n        (1, ['x', '=', '1', '# c1']),\n        (0, ['elif', 'y', ':', '# c2']),\n        (1, ['y', '=', '1']),\n        (0, ['else', ':']),\n        (1, ['# c3']),\n        (1, ['z', '=', '1']),\n    ])\n\n  def testNestedCompoundTwoLevel(self):\n    code = textwrap.dedent(\"\"\"\\\n        if x:\n          x = 1 # c1\n          while t:\n            # c2\n            j = 1\n          k = 1\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['if', 'x', ':']),\n        (1, ['x', '=', '1', '# c1']),\n        (1, ['while', 't', ':']),\n        (2, ['# c2']),\n        (2, ['j', '=', '1']),\n        (1, ['k', '=', '1']),\n    ])\n\n  def testSimpleWhile(self):\n    code = textwrap.dedent(\"\"\"\\\n        while x > 1: # c1\n           # c2\n           x = 1\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['while', 'x', '>', '1', ':', '# c1']),\n        (1, ['# c2']),\n        (1, ['x', '=', '1']),\n    ])\n\n  def testSimpleTry(self):\n    code = textwrap.dedent(\"\"\"\\\n        try:\n          pass\n        except:\n          pass\n        except:\n          pass\n        else:\n          pass\n        finally:\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['try', ':']),\n        (1, ['pass']),\n        (0, ['except', ':']),\n        (1, ['pass']),\n        (0, ['except', ':']),\n        (1, ['pass']),\n        (0, ['else', ':']),\n        (1, ['pass']),\n        (0, ['finally', ':']),\n        (1, ['pass']),\n    ])\n\n  def testSimpleFuncdef(self):\n    code = textwrap.dedent(\"\"\"\\\n        def foo(x): # c1\n          # c2\n          return x\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['def', 'foo', '(', 'x', ')', ':', '# c1']),\n        (1, ['# c2']),\n        (1, ['return', 'x']),\n    ])\n\n  def testTwoFuncDefs(self):\n    code = textwrap.dedent(\"\"\"\\\n        def foo(x): # c1\n          # c2\n          return x\n\n        def bar(): # c3\n          # c4\n          return x\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['def', 'foo', '(', 'x', ')', ':', '# c1']),\n        (1, ['# c2']),\n        (1, ['return', 'x']),\n        (0, ['def', 'bar', '(', ')', ':', '# c3']),\n        (1, ['# c4']),\n        (1, ['return', 'x']),\n    ])\n\n  def testSimpleClassDef(self):\n    code = textwrap.dedent(\"\"\"\\\n        class Klass: # c1\n          # c2\n          p = 1\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['class', 'Klass', ':', '# c1']),\n        (1, ['# c2']),\n        (1, ['p', '=', '1']),\n    ])\n\n  def testSingleLineStmtInFunc(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f(): return 37\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['def', 'f', '(', ')', ':']),\n        (1, ['return', '37']),\n    ])\n\n  def testMultipleComments(self):\n    code = textwrap.dedent(\"\"\"\\\n        # Comment #1\n\n        # Comment #2\n        def f():\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [\n        (0, ['# Comment #1']),\n        (0, ['# Comment #2']),\n        (0, ['def', 'f', '(', ')', ':']),\n        (1, ['pass']),\n    ])\n\n  def testSplitListWithComment(self):\n    code = textwrap.dedent(\"\"\"\\\n        a = [\n            'a',\n            'b',\n            'c',  # hello world\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckLogicalLines(llines, [(0, [\n        'a', '=', '[', \"'a'\", ',', \"'b'\", ',', \"'c'\", ',', '# hello world', ']'\n    ])])\n\n\nclass MatchBracketsTest(yapf_test_helper.YAPFTest):\n\n  def _CheckMatchingBrackets(self, llines, list_of_expected):\n    \"\"\"Check that the tokens have the expected matching bracket.\n\n    Arguments:\n      llines: list of LogicalLine.\n      list_of_expected: list of (index, index) pairs. The matching brackets at\n        the indexes need to match. Non-semantic tokens are filtered out from the\n        expected values.\n    \"\"\"\n    actual = []\n    for lline in llines:\n      filtered_values = [(ft, ft.matching_bracket)\n                         for ft in lline.tokens\n                         if ft.name not in pytree_utils.NONSEMANTIC_TOKENS]\n      if filtered_values:\n        actual.append(filtered_values)\n\n    for index, bracket_list in enumerate(list_of_expected):\n      lline = actual[index]\n      if not bracket_list:\n        for value in lline:\n          self.assertIsNone(value[1])\n      else:\n        for open_bracket, close_bracket in bracket_list:\n          self.assertEqual(lline[open_bracket][0], lline[close_bracket][1])\n          self.assertEqual(lline[close_bracket][0], lline[open_bracket][1])\n\n  def testFunctionDef(self):\n    code = textwrap.dedent(\"\"\"\\\n        def foo(a, b=['w','d'], c=[42, 37]):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckMatchingBrackets(llines, [\n        [(2, 20), (7, 11), (15, 19)],\n        [],\n    ])\n\n  def testDecorator(self):\n    code = textwrap.dedent(\"\"\"\\\n        @bar()\n        def foo(a, b, c):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckMatchingBrackets(llines, [\n        [(2, 3)],\n        [(2, 8)],\n        [],\n    ])\n\n  def testClassDef(self):\n    code = textwrap.dedent(\"\"\"\\\n        class A(B, C, D):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckMatchingBrackets(llines, [\n        [(2, 8)],\n        [],\n    ])\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/pytree_utils_test.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.pytree_utils.\"\"\"\n\nimport unittest\n\nfrom yapf_third_party._ylib2to3 import pygram\nfrom yapf_third_party._ylib2to3 import pytree\nfrom yapf_third_party._ylib2to3.pgen2 import token\n\nfrom yapf.pytree import pytree_utils\n\nfrom yapftests import yapf_test_helper\n\n# More direct access to the symbol->number mapping living within the grammar\n# module.\n_GRAMMAR_SYMBOL2NUMBER = pygram.python_grammar.symbol2number\n\n_FOO = 'foo'\n_FOO1 = 'foo1'\n_FOO2 = 'foo2'\n_FOO3 = 'foo3'\n_FOO4 = 'foo4'\n_FOO5 = 'foo5'\n\n\nclass NodeNameTest(yapf_test_helper.YAPFTest):\n\n  def testNodeNameForLeaf(self):\n    leaf = pytree.Leaf(token.LPAR, '(')\n    self.assertEqual('LPAR', pytree_utils.NodeName(leaf))\n\n  def testNodeNameForNode(self):\n    leaf = pytree.Leaf(token.LPAR, '(')\n    node = pytree.Node(pygram.python_grammar.symbol2number['suite'], [leaf])\n    self.assertEqual('suite', pytree_utils.NodeName(node))\n\n\nclass ParseCodeToTreeTest(yapf_test_helper.YAPFTest):\n\n  def testParseCodeToTree(self):\n    # Since ParseCodeToTree is a thin wrapper around underlying lib2to3\n    # functionality, only a sanity test here...\n    tree = pytree_utils.ParseCodeToTree('foo = 2\\n')\n    self.assertEqual('file_input', pytree_utils.NodeName(tree))\n    self.assertEqual(2, len(tree.children))\n    self.assertEqual('simple_stmt', pytree_utils.NodeName(tree.children[0]))\n\n  def testPrintFunctionToTree(self):\n    tree = pytree_utils.ParseCodeToTree(\n        'print(\"hello world\", file=sys.stderr)\\n')\n    self.assertEqual('file_input', pytree_utils.NodeName(tree))\n    self.assertEqual(2, len(tree.children))\n    self.assertEqual('simple_stmt', pytree_utils.NodeName(tree.children[0]))\n\n  def testPrintStatementToTree(self):\n    with self.assertRaises(SyntaxError):\n      pytree_utils.ParseCodeToTree('print \"hello world\"\\n')\n\n  def testClassNotLocal(self):\n    with self.assertRaises(SyntaxError):\n      pytree_utils.ParseCodeToTree('class nonlocal: pass\\n')\n\n\nclass InsertNodesBeforeAfterTest(yapf_test_helper.YAPFTest):\n\n  def _BuildSimpleTree(self):\n    # Builds a simple tree we can play with in the tests.\n    # The tree looks like this:\n    #\n    #   suite:\n    #     LPAR\n    #     LPAR\n    #     simple_stmt:\n    #       NAME('foo')\n    #\n    lpar1 = pytree.Leaf(token.LPAR, '(')\n    lpar2 = pytree.Leaf(token.LPAR, '(')\n    simple_stmt = pytree.Node(_GRAMMAR_SYMBOL2NUMBER['simple_stmt'],\n                              [pytree.Leaf(token.NAME, 'foo')])\n    return pytree.Node(_GRAMMAR_SYMBOL2NUMBER['suite'],\n                       [lpar1, lpar2, simple_stmt])\n\n  def _MakeNewNodeRPAR(self):\n    return pytree.Leaf(token.RPAR, ')')\n\n  def setUp(self):\n    self._simple_tree = self._BuildSimpleTree()\n\n  def testInsertNodesBefore(self):\n    # Insert before simple_stmt and make sure it went to the right place\n    pytree_utils.InsertNodesBefore([self._MakeNewNodeRPAR()],\n                                   self._simple_tree.children[2])\n    self.assertEqual(4, len(self._simple_tree.children))\n    self.assertEqual('RPAR',\n                     pytree_utils.NodeName(self._simple_tree.children[2]))\n    self.assertEqual('simple_stmt',\n                     pytree_utils.NodeName(self._simple_tree.children[3]))\n\n  def testInsertNodesBeforeFirstChild(self):\n    # Insert before the first child of its parent\n    simple_stmt = self._simple_tree.children[2]\n    foo_child = simple_stmt.children[0]\n    pytree_utils.InsertNodesBefore([self._MakeNewNodeRPAR()], foo_child)\n    self.assertEqual(3, len(self._simple_tree.children))\n    self.assertEqual(2, len(simple_stmt.children))\n    self.assertEqual('RPAR', pytree_utils.NodeName(simple_stmt.children[0]))\n    self.assertEqual('NAME', pytree_utils.NodeName(simple_stmt.children[1]))\n\n  def testInsertNodesAfter(self):\n    # Insert after and make sure it went to the right place\n    pytree_utils.InsertNodesAfter([self._MakeNewNodeRPAR()],\n                                  self._simple_tree.children[2])\n    self.assertEqual(4, len(self._simple_tree.children))\n    self.assertEqual('simple_stmt',\n                     pytree_utils.NodeName(self._simple_tree.children[2]))\n    self.assertEqual('RPAR',\n                     pytree_utils.NodeName(self._simple_tree.children[3]))\n\n  def testInsertNodesAfterLastChild(self):\n    # Insert after the last child of its parent\n    simple_stmt = self._simple_tree.children[2]\n    foo_child = simple_stmt.children[0]\n    pytree_utils.InsertNodesAfter([self._MakeNewNodeRPAR()], foo_child)\n    self.assertEqual(3, len(self._simple_tree.children))\n    self.assertEqual(2, len(simple_stmt.children))\n    self.assertEqual('NAME', pytree_utils.NodeName(simple_stmt.children[0]))\n    self.assertEqual('RPAR', pytree_utils.NodeName(simple_stmt.children[1]))\n\n  def testInsertNodesWhichHasParent(self):\n    # Try to insert an existing tree node into another place and fail.\n    with self.assertRaises(RuntimeError):\n      pytree_utils.InsertNodesAfter([self._simple_tree.children[1]],\n                                    self._simple_tree.children[0])\n\n\nclass AnnotationsTest(yapf_test_helper.YAPFTest):\n\n  def setUp(self):\n    self._leaf = pytree.Leaf(token.LPAR, '(')\n    self._node = pytree.Node(_GRAMMAR_SYMBOL2NUMBER['simple_stmt'],\n                             [pytree.Leaf(token.NAME, 'foo')])\n\n  def testGetWhenNone(self):\n    self.assertIsNone(pytree_utils.GetNodeAnnotation(self._leaf, _FOO))\n\n  def testSetWhenNone(self):\n    pytree_utils.SetNodeAnnotation(self._leaf, _FOO, 20)\n    self.assertEqual(pytree_utils.GetNodeAnnotation(self._leaf, _FOO), 20)\n\n  def testSetAgain(self):\n    pytree_utils.SetNodeAnnotation(self._leaf, _FOO, 20)\n    self.assertEqual(pytree_utils.GetNodeAnnotation(self._leaf, _FOO), 20)\n    pytree_utils.SetNodeAnnotation(self._leaf, _FOO, 30)\n    self.assertEqual(pytree_utils.GetNodeAnnotation(self._leaf, _FOO), 30)\n\n  def testMultiple(self):\n    pytree_utils.SetNodeAnnotation(self._leaf, _FOO, 20)\n    pytree_utils.SetNodeAnnotation(self._leaf, _FOO1, 1)\n    pytree_utils.SetNodeAnnotation(self._leaf, _FOO2, 2)\n    pytree_utils.SetNodeAnnotation(self._leaf, _FOO3, 3)\n    pytree_utils.SetNodeAnnotation(self._leaf, _FOO4, 4)\n    pytree_utils.SetNodeAnnotation(self._leaf, _FOO5, 5)\n\n    self.assertEqual(pytree_utils.GetNodeAnnotation(self._leaf, _FOO), 20)\n    self.assertEqual(pytree_utils.GetNodeAnnotation(self._leaf, _FOO1), 1)\n    self.assertEqual(pytree_utils.GetNodeAnnotation(self._leaf, _FOO2), 2)\n    self.assertEqual(pytree_utils.GetNodeAnnotation(self._leaf, _FOO3), 3)\n    self.assertEqual(pytree_utils.GetNodeAnnotation(self._leaf, _FOO4), 4)\n    self.assertEqual(pytree_utils.GetNodeAnnotation(self._leaf, _FOO5), 5)\n\n  def testSubtype(self):\n    pytree_utils.AppendNodeAnnotation(self._leaf,\n                                      pytree_utils.Annotation.SUBTYPE, _FOO)\n\n    self.assertSetEqual(\n        pytree_utils.GetNodeAnnotation(self._leaf,\n                                       pytree_utils.Annotation.SUBTYPE), {_FOO})\n\n    pytree_utils.RemoveSubtypeAnnotation(self._leaf, _FOO)\n\n    self.assertSetEqual(\n        pytree_utils.GetNodeAnnotation(self._leaf,\n                                       pytree_utils.Annotation.SUBTYPE), set())\n\n  def testSetOnNode(self):\n    pytree_utils.SetNodeAnnotation(self._node, _FOO, 20)\n    self.assertEqual(pytree_utils.GetNodeAnnotation(self._node, _FOO), 20)\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/pytree_visitor_test.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.pytree_visitor.\"\"\"\n\nimport unittest\nfrom io import StringIO\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.pytree import pytree_visitor\n\nfrom yapftests import yapf_test_helper\n\n\nclass _NodeNameCollector(pytree_visitor.PyTreeVisitor):\n  \"\"\"A tree visitor that collects the names of all tree nodes into a list.\n\n  Attributes:\n    all_node_names: collected list of the names, available when the traversal\n      is over.\n    name_node_values: collects a list of NAME leaves (in addition to those going\n      into all_node_names).\n  \"\"\"\n\n  def __init__(self):\n    self.all_node_names = []\n    self.name_node_values = []\n\n  def DefaultNodeVisit(self, node):\n    self.all_node_names.append(pytree_utils.NodeName(node))\n    super(_NodeNameCollector, self).DefaultNodeVisit(node)\n\n  def DefaultLeafVisit(self, leaf):\n    self.all_node_names.append(pytree_utils.NodeName(leaf))\n\n  def Visit_NAME(self, leaf):\n    self.name_node_values.append(leaf.value)\n    self.DefaultLeafVisit(leaf)\n\n\n_VISITOR_TEST_SIMPLE_CODE = \"\"\"\\\nfoo = bar\nbaz = x\n\"\"\"\n\n_VISITOR_TEST_NESTED_CODE = \"\"\"\\\nif x:\n  if y:\n    return z\n\"\"\"\n\n\nclass PytreeVisitorTest(yapf_test_helper.YAPFTest):\n\n  def testCollectAllNodeNamesSimpleCode(self):\n    tree = pytree_utils.ParseCodeToTree(_VISITOR_TEST_SIMPLE_CODE)\n    collector = _NodeNameCollector()\n    collector.Visit(tree)\n    expected_names = [\n        'file_input',\n        'simple_stmt', 'expr_stmt', 'NAME', 'EQUAL', 'NAME', 'NEWLINE',\n        'simple_stmt', 'expr_stmt', 'NAME', 'EQUAL', 'NAME', 'NEWLINE',\n        'ENDMARKER',\n    ]  # yapf: disable\n    self.assertEqual(expected_names, collector.all_node_names)\n\n    expected_name_node_values = ['foo', 'bar', 'baz', 'x']\n    self.assertEqual(expected_name_node_values, collector.name_node_values)\n\n  def testCollectAllNodeNamesNestedCode(self):\n    tree = pytree_utils.ParseCodeToTree(_VISITOR_TEST_NESTED_CODE)\n    collector = _NodeNameCollector()\n    collector.Visit(tree)\n    expected_names = [\n        'file_input',\n        'if_stmt', 'NAME', 'NAME', 'COLON',\n        'suite', 'NEWLINE',\n        'INDENT', 'if_stmt', 'NAME', 'NAME', 'COLON', 'suite', 'NEWLINE',\n        'INDENT', 'simple_stmt', 'return_stmt', 'NAME', 'NAME', 'NEWLINE',\n        'DEDENT', 'DEDENT', 'ENDMARKER',\n    ]  # yapf: disable\n    self.assertEqual(expected_names, collector.all_node_names)\n\n    expected_name_node_values = ['if', 'x', 'if', 'y', 'return', 'z']\n    self.assertEqual(expected_name_node_values, collector.name_node_values)\n\n  def testDumper(self):\n    # PyTreeDumper is mainly a debugging utility, so only do basic sanity\n    # checking.\n    tree = pytree_utils.ParseCodeToTree(_VISITOR_TEST_SIMPLE_CODE)\n    stream = StringIO()\n    pytree_visitor.PyTreeDumper(target_stream=stream).Visit(tree)\n\n    dump_output = stream.getvalue()\n    self.assertIn('file_input [3 children]', dump_output)\n    self.assertIn(\"NAME(Leaf(NAME, 'foo'))\", dump_output)\n    self.assertIn(\"EQUAL(Leaf(EQUAL, '='))\", dump_output)\n\n  def testDumpPyTree(self):\n    # Similar sanity checking for the convenience wrapper DumpPyTree\n    tree = pytree_utils.ParseCodeToTree(_VISITOR_TEST_SIMPLE_CODE)\n    stream = StringIO()\n    pytree_visitor.DumpPyTree(tree, target_stream=stream)\n\n    dump_output = stream.getvalue()\n    self.assertIn('file_input [3 children]', dump_output)\n    self.assertIn(\"NAME(Leaf(NAME, 'foo'))\", dump_output)\n    self.assertIn(\"EQUAL(Leaf(EQUAL, '='))\", dump_output)\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/reformatter_basic_test.py",
    "content": "# Copyright 2016 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Basic tests for yapf.reformatter.\"\"\"\n\nimport sys\nimport textwrap\nimport unittest\n\nfrom yapf.yapflib import reformatter\nfrom yapf.yapflib import style\n\nfrom yapftests import yapf_test_helper\n\n\nclass BasicReformatterTest(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):\n    style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testSplittingAllArgs(self):\n    style.SetGlobalStyle(\n        style.CreateStyleFromConfig(\n            '{split_all_comma_separated_values: true, column_limit: 40}'))\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        responseDict = {\"timestamp\": timestamp, \"someValue\":   value, \"whatever\": 120}\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        responseDict = {\n            \"timestamp\": timestamp,\n            \"someValue\": value,\n            \"whatever\": 120\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        yes = { 'yes': 'no', 'no': 'yes', }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        yes = {\n            'yes': 'no',\n            'no': 'yes',\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo(long_arg, really_long_arg, really_really_long_arg, cant_keep_all_these_args):\n            pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo(long_arg,\n                really_long_arg,\n                really_really_long_arg,\n                cant_keep_all_these_args):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        foo_tuple = [long_arg, really_long_arg, really_really_long_arg, cant_keep_all_these_args]\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        foo_tuple = [\n            long_arg,\n            really_long_arg,\n            really_really_long_arg,\n            cant_keep_all_these_args\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        foo_tuple = [short, arg]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        foo_tuple = [short, arg]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        values = [ lambda arg1, arg2: arg1 + arg2 ]\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        values = [\n            lambda arg1, arg2: arg1 + arg2\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        values = [\n            (some_arg1, some_arg2) for some_arg1, some_arg2 in values\n        ]\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        values = [\n            (some_arg1,\n             some_arg2)\n            for some_arg1, some_arg2 in values\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    # There is a test for split_all_top_level_comma_separated_values, with\n    # different expected value\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        someLongFunction(this_is_a_very_long_parameter,\n            abc=(a, this_will_just_fit_xxxxxxx))\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        someLongFunction(\n            this_is_a_very_long_parameter,\n            abc=(a,\n                 this_will_just_fit_xxxxxxx))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplittingTopLevelAllArgs(self):\n    style_dict = style.CreateStyleFromConfig(\n        '{split_all_top_level_comma_separated_values: true, '\n        'column_limit: 40}')\n    style.SetGlobalStyle(style_dict)\n    # Works the same way as split_all_comma_separated_values\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        responseDict = {\"timestamp\": timestamp, \"someValue\":   value, \"whatever\": 120}\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        responseDict = {\n            \"timestamp\": timestamp,\n            \"someValue\": value,\n            \"whatever\": 120\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    # Works the same way as split_all_comma_separated_values\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo(long_arg, really_long_arg, really_really_long_arg, cant_keep_all_these_args):\n              pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo(long_arg,\n                really_long_arg,\n                really_really_long_arg,\n                cant_keep_all_these_args):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    # Works the same way as split_all_comma_separated_values\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        foo_tuple = [long_arg, really_long_arg, really_really_long_arg, cant_keep_all_these_args]\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        foo_tuple = [\n            long_arg,\n            really_long_arg,\n            really_really_long_arg,\n            cant_keep_all_these_args\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    # Works the same way as split_all_comma_separated_values\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        foo_tuple = [short, arg]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        foo_tuple = [short, arg]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    # Works the same way as split_all_comma_separated_values\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        values = [ lambda arg1, arg2: arg1 + arg2 ]\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        values = [\n            lambda arg1, arg2: arg1 + arg2\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    # There is a test for split_all_comma_separated_values, with different\n    # expected value\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        values = [\n            (some_arg1, some_arg2) for some_arg1, some_arg2 in values\n        ]\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        values = [\n            (some_arg1, some_arg2)\n            for some_arg1, some_arg2 in values\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n    # There is a test for split_all_comma_separated_values, with different\n    # expected value\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        someLongFunction(this_is_a_very_long_parameter,\n            abc=(a, this_will_just_fit_xxxxxxx))\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        someLongFunction(\n            this_is_a_very_long_parameter,\n            abc=(a, this_will_just_fit_xxxxxxx))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    actual_formatted_code = reformatter.Reformat(llines)\n    self.assertEqual(40, len(actual_formatted_code.splitlines()[-1]))\n    self.assertCodeEqual(expected_formatted_code, actual_formatted_code)\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        someLongFunction(this_is_a_very_long_parameter,\n            abc=(a, this_will_not_fit_xxxxxxxxx))\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        someLongFunction(\n            this_is_a_very_long_parameter,\n            abc=(a,\n                 this_will_not_fit_xxxxxxxxx))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    # This tests when there is an embedded dictionary that will fit in a line\n    original_multiline = style_dict['FORCE_MULTILINE_DICT']\n    style_dict['FORCE_MULTILINE_DICT'] = False\n    style.SetGlobalStyle(style_dict)\n    unformatted_code = textwrap.dedent(\"\"\"\\\n          someLongFunction(this_is_a_very_long_parameter,\n              abc={a: b, b: c})\n          \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n          someLongFunction(\n              this_is_a_very_long_parameter,\n              abc={\n                  a: b, b: c\n              })\n          \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    actual_formatted_code = reformatter.Reformat(llines)\n    self.assertCodeEqual(expected_formatted_code, actual_formatted_code)\n\n    # This tests when there is an embedded dictionary that will fit in a line,\n    #  but FORCE_MULTILINE_DICT is set\n    style_dict['FORCE_MULTILINE_DICT'] = True\n    style.SetGlobalStyle(style_dict)\n    unformatted_code = textwrap.dedent(\"\"\"\\\n          someLongFunction(this_is_a_very_long_parameter,\n              abc={a: b, b: c})\n          \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n          someLongFunction(\n              this_is_a_very_long_parameter,\n              abc={\n                  a: b,\n                  b: c\n              })\n          \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    actual_formatted_code = reformatter.Reformat(llines)\n    self.assertCodeEqual(expected_formatted_code, actual_formatted_code)\n\n    style_dict['FORCE_MULTILINE_DICT'] = original_multiline\n    style.SetGlobalStyle(style_dict)\n\n    # Exercise the case where there's no opening bracket (for a, b)\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a, b = f(\n            a_very_long_parameter, yet_another_one, and_another)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a, b = f(\n            a_very_long_parameter, yet_another_one, and_another)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    # Don't require splitting before comments.\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        KO = {\n            'ABC': Abc, # abc\n            'DEF': Def, # def\n            'LOL': Lol, # wtf\n            'GHI': Ghi,\n            'JKL': Jkl,\n        }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        KO = {\n            'ABC': Abc,  # abc\n            'DEF': Def,  # def\n            'LOL': Lol,  # wtf\n            'GHI': Ghi,\n            'JKL': Jkl,\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSimpleFunctionsWithTrailingComments(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def g():  # Trailing comment\n          if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and\n              xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n            pass\n\n        def f(  # Intermediate comment\n        ):\n          if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and\n              xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def g():  # Trailing comment\n          if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and\n              xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n            pass\n\n\n        def f(  # Intermediate comment\n        ):\n          if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and\n              xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testParamListWithTrailingComments(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def f(a,\n              b, #\n              c):\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def f(a, b,  #\n              c):\n          pass\n    \"\"\")\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: yapf,'\n              ' disable_split_list_with_comment: True}'))\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testBlankLinesBetweenTopLevelImportsAndVariables(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        import foo as bar\n        VAR = 'baz'\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        import foo as bar\n\n        VAR = 'baz'\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        import foo as bar\n\n        VAR = 'baz'\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        import foo as bar\n\n\n        VAR = 'baz'\n    \"\"\")\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: yapf, '\n              'blank_lines_between_top_level_imports_and_variables: 2}'))\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        import foo as bar\n        # Some comment\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        import foo as bar\n        # Some comment\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        import foo as bar\n        class Baz():\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        import foo as bar\n\n\n        class Baz():\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        import foo as bar\n        def foobar():\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        import foo as bar\n\n\n        def foobar():\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foobar():\n          from foo import Bar\n          Bar.baz()\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foobar():\n          from foo import Bar\n          Bar.baz()\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testBlankLinesAtEndOfFile(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foobar(): # foo\n         pass\n\n\n\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foobar():  # foo\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        x = {  'a':37,'b':42,\n\n        'c':927}\n\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        x = {'a': 37, 'b': 42, 'c': 927}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testIndentBlankLines(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class foo(object):\n\n          def foobar(self):\n\n            pass\n\n          def barfoo(self, x, y):  # bar\n\n            if x:\n\n              return y\n\n\n        def bar():\n\n          return 0\n    \"\"\")\n    expected_formatted_code = \"\"\"\\\nclass foo(object):\\n  \\n  def foobar(self):\\n    \\n    pass\\n  \\n  def barfoo(self, x, y):  # bar\\n    \\n    if x:\\n      \\n      return y\\n\\n\\ndef bar():\\n  \\n  return 0\n\"\"\"  # noqa\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: yapf, indent_blank_lines: true}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n    unformatted_code, expected_formatted_code = (expected_formatted_code,\n                                                 unformatted_code)\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testMultipleUgliness(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        x = {  'a':37,'b':42,\n\n        'c':927}\n\n        y = 'hello ''world'\n        z = 'hello '+'world'\n        a = 'hello {}'.format('world')\n        class foo  (     object  ):\n          def f    (self   ):\n            return       37*-+2\n          def g(self, x,y=42):\n              return y\n        def f  (   a ) :\n          return      37+-+a[42-x :  y**3]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        x = {'a': 37, 'b': 42, 'c': 927}\n\n        y = 'hello ' 'world'\n        z = 'hello ' + 'world'\n        a = 'hello {}'.format('world')\n\n\n        class foo(object):\n\n          def f(self):\n            return 37 * -+2\n\n          def g(self, x, y=42):\n            return y\n\n\n        def f(a):\n          return 37 + -+a[42 - x:y**3]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testComments(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class Foo(object):\n          pass\n\n        # Attached comment\n        class Bar(object):\n          pass\n\n        global_assignment = 42\n\n        # Comment attached to class with decorator.\n        # Comment attached to class with decorator.\n        @noop\n        @noop\n        class Baz(object):\n          pass\n\n        # Intermediate comment\n\n        class Qux(object):\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class Foo(object):\n          pass\n\n\n        # Attached comment\n        class Bar(object):\n          pass\n\n\n        global_assignment = 42\n\n\n        # Comment attached to class with decorator.\n        # Comment attached to class with decorator.\n        @noop\n        @noop\n        class Baz(object):\n          pass\n\n\n        # Intermediate comment\n\n\n        class Qux(object):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSingleComment(self):\n    code = textwrap.dedent(\"\"\"\\\n        # Thing 1\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testCommentsWithTrailingSpaces(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        # Thing 1    \\n# Thing 2    \\n\"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        # Thing 1\n        # Thing 2\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testCommentsInDataLiteral(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n          return collections.OrderedDict({\n              # First comment.\n              'fnord': 37,\n\n              # Second comment.\n              # Continuation of second comment.\n              'bork': 42,\n\n              # Ending comment.\n          })\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testEndingWhitespaceAfterSimpleStatement(self):\n    code = textwrap.dedent(\"\"\"\\\n        import foo as bar\n        # Thing 1\n        # Thing 2\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testDocstrings(self):\n    unformatted_code = textwrap.dedent('''\\\n        u\"\"\"Module-level docstring.\"\"\"\n        import os\n        class Foo(object):\n\n          \"\"\"Class-level docstring.\"\"\"\n          # A comment for qux.\n          def qux(self):\n\n\n            \"\"\"Function-level docstring.\n\n            A multiline function docstring.\n            \"\"\"\n            print('hello {}'.format('world'))\n            return 42\n    ''')\n    expected_formatted_code = textwrap.dedent('''\\\n        u\"\"\"Module-level docstring.\"\"\"\n        import os\n\n\n        class Foo(object):\n          \"\"\"Class-level docstring.\"\"\"\n\n          # A comment for qux.\n          def qux(self):\n            \"\"\"Function-level docstring.\n\n            A multiline function docstring.\n            \"\"\"\n            print('hello {}'.format('world'))\n            return 42\n    ''')\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDocstringAndMultilineComment(self):\n    unformatted_code = textwrap.dedent('''\\\n        \"\"\"Hello world\"\"\"\n        # A multiline\n        # comment\n        class bar(object):\n          \"\"\"class docstring\"\"\"\n          # class multiline\n          # comment\n          def foo(self):\n            \"\"\"Another docstring.\"\"\"\n            # Another multiline\n            # comment\n            pass\n    ''')\n    expected_formatted_code = textwrap.dedent('''\\\n        \"\"\"Hello world\"\"\"\n\n\n        # A multiline\n        # comment\n        class bar(object):\n          \"\"\"class docstring\"\"\"\n\n          # class multiline\n          # comment\n          def foo(self):\n            \"\"\"Another docstring.\"\"\"\n            # Another multiline\n            # comment\n            pass\n    ''')\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testMultilineDocstringAndMultilineComment(self):\n    unformatted_code = textwrap.dedent('''\\\n        \"\"\"Hello world\n\n        RIP Dennis Richie.\n        \"\"\"\n        # A multiline\n        # comment\n        class bar(object):\n          \"\"\"class docstring\n\n          A classy class.\n          \"\"\"\n          # class multiline\n          # comment\n          def foo(self):\n            \"\"\"Another docstring.\n\n            A functional function.\n            \"\"\"\n            # Another multiline\n            # comment\n            pass\n    ''')\n    expected_formatted_code = textwrap.dedent('''\\\n        \"\"\"Hello world\n\n        RIP Dennis Richie.\n        \"\"\"\n\n\n        # A multiline\n        # comment\n        class bar(object):\n          \"\"\"class docstring\n\n          A classy class.\n          \"\"\"\n\n          # class multiline\n          # comment\n          def foo(self):\n            \"\"\"Another docstring.\n\n            A functional function.\n            \"\"\"\n            # Another multiline\n            # comment\n            pass\n    ''')\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testTupleCommaBeforeLastParen(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a = ( 1, )\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = (1,)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testNoBreakOutsideOfBracket(self):\n    # FIXME(morbo): How this is formatted is not correct. But it's syntactically\n    # correct.\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          assert port >= minimum, \\\n'Unexpected port %d when minimum was %d.' % (port, minimum)\n        \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          assert port >= minimum, 'Unexpected port %d when minimum was %d.' % (port,\n                                                                               minimum)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testBlankLinesBeforeDecorators(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        @foo()\n        class A(object):\n          @bar()\n          @baz()\n          def x(self):\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        @foo()\n        class A(object):\n\n          @bar()\n          @baz()\n          def x(self):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testCommentBetweenDecorators(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        @foo()\n        # frob\n        @bar\n        def x  (self):\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        @foo()\n        # frob\n        @bar\n        def x(self):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testListComprehension(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def given(y):\n            [k for k in ()\n              if k in y]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def given(y):\n          [k for k in () if k in y]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testListComprehensionPreferOneLine(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def given(y):\n            long_variable_name = [\n                long_var_name + 1\n                for long_var_name in ()\n                if long_var_name == 2]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def given(y):\n          long_variable_name = [\n              long_var_name + 1 for long_var_name in () if long_var_name == 2\n          ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testListComprehensionPreferOneLineOverArithmeticSplit(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def given(used_identifiers):\n          return (sum(len(identifier)\n                      for identifier in used_identifiers) / len(used_identifiers))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def given(used_identifiers):\n          return (sum(len(identifier) for identifier in used_identifiers) /\n                  len(used_identifiers))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testListComprehensionPreferThreeLinesForLineWrap(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def given(y):\n            long_variable_name = [\n                long_var_name + 1\n                for long_var_name, number_two in ()\n                if long_var_name == 2 and number_two == 3]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def given(y):\n          long_variable_name = [\n              long_var_name + 1\n              for long_var_name, number_two in ()\n              if long_var_name == 2 and number_two == 3\n          ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testListComprehensionPreferNoBreakForTrivialExpression(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def given(y):\n            long_variable_name = [\n                long_var_name\n                for long_var_name, number_two in ()\n                if long_var_name == 2 and number_two == 3]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def given(y):\n          long_variable_name = [\n              long_var_name for long_var_name, number_two in ()\n              if long_var_name == 2 and number_two == 3\n          ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testOpeningAndClosingBrackets(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        foo( (1, ) )\n        foo( ( 1, 2, 3  ) )\n        foo( ( 1, 2, 3, ) )\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        foo((1,))\n        foo((1, 2, 3))\n        foo((\n            1,\n            2,\n            3,\n        ))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSingleLineFunctions(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo():  return 42\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          return 42\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testNoQueueSeletionInMiddleOfLine(self):\n    # If the queue isn't properly constructed, then a token in the middle of the\n    # line may be selected as the one with least penalty. The tokens after that\n    # one are then splatted at the end of the line with no formatting.\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        find_symbol(node.type) + \"< \" + \" \".join(find_pattern(n) for n in node.child) + \" >\"\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        find_symbol(node.type) + \"< \" + \" \".join(\n            find_pattern(n) for n in node.child) + \" >\"\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testNoSpacesBetweenSubscriptsAndCalls(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaa = bbbbbbbb.ccccccccc() [42] (a, 2)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaa = bbbbbbbb.ccccccccc()[42](a, 2)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testNoSpacesBetweenOpeningBracketAndStartingOperator(self):\n    # Unary operator.\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaa = bbbbbbbb.ccccccccc[ -1 ]( -42 )\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaa = bbbbbbbb.ccccccccc[-1](-42)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    # Varargs and kwargs.\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaa = bbbbbbbb.ccccccccc( *varargs )\n        aaaaaaaaaa = bbbbbbbb.ccccccccc( **kwargs )\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaa = bbbbbbbb.ccccccccc(*varargs)\n        aaaaaaaaaa = bbbbbbbb.ccccccccc(**kwargs)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testMultilineCommentReformatted(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            # This is a multiline\n            # comment.\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          # This is a multiline\n          # comment.\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDictionaryMakerFormatting(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        _PYTHON_STATEMENTS = frozenset({\n            lambda x, y: 'simple_stmt': 'small_stmt', 'expr_stmt': 'print_stmt', 'del_stmt':\n            'pass_stmt', lambda: 'break_stmt': 'continue_stmt', 'return_stmt': 'raise_stmt',\n            'yield_stmt': 'import_stmt', lambda: 'global_stmt': 'exec_stmt', 'assert_stmt':\n            'if_stmt', 'while_stmt': 'for_stmt',\n        })\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        _PYTHON_STATEMENTS = frozenset({\n            lambda x, y: 'simple_stmt': 'small_stmt',\n            'expr_stmt': 'print_stmt',\n            'del_stmt': 'pass_stmt',\n            lambda: 'break_stmt': 'continue_stmt',\n            'return_stmt': 'raise_stmt',\n            'yield_stmt': 'import_stmt',\n            lambda: 'global_stmt': 'exec_stmt',\n            'assert_stmt': 'if_stmt',\n            'while_stmt': 'for_stmt',\n        })\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSimpleMultilineCode(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          aaaaaaaaaaaaaa.bbbbbbbbbbbbbb.ccccccc(zzzzzzzzzzzz, \\\nxxxxxxxxxxx, yyyyyyyyyyyy, vvvvvvvvv)\n          aaaaaaaaaaaaaa.bbbbbbbbbbbbbb.ccccccc(zzzzzzzzzzzz, \\\nxxxxxxxxxxx, yyyyyyyyyyyy, vvvvvvvvv)\n        \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          aaaaaaaaaaaaaa.bbbbbbbbbbbbbb.ccccccc(zzzzzzzzzzzz, xxxxxxxxxxx, yyyyyyyyyyyy,\n                                                vvvvvvvvv)\n          aaaaaaaaaaaaaa.bbbbbbbbbbbbbb.ccccccc(zzzzzzzzzzzz, xxxxxxxxxxx, yyyyyyyyyyyy,\n                                                vvvvvvvvv)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testMultilineComment(self):\n    code = textwrap.dedent(\"\"\"\\\n        if Foo:\n          # Hello world\n          # Yo man.\n          # Yo man.\n          # Yo man.\n          # Yo man.\n          a = 42\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testSpaceBetweenStringAndParentheses(self):\n    code = textwrap.dedent(\"\"\"\\\n        b = '0' ('hello')\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testMultilineString(self):\n    code = textwrap.dedent(\"\"\"\\\n        code = textwrap.dedent('''\\\n            if Foo:\n              # Hello world\n              # Yo man.\n              # Yo man.\n              # Yo man.\n              # Yo man.\n              a = 42\n            ''')\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent('''\\\n        def f():\n            email_text += \"\"\"<html>This is a really long docstring that goes over the column limit and is multi-line.<br><br>\n        <b>Czar: </b>\"\"\"+despot[\"Nicholas\"]+\"\"\"<br>\n        <b>Minion: </b>\"\"\"+serf[\"Dmitri\"]+\"\"\"<br>\n        <b>Residence: </b>\"\"\"+palace[\"Winter\"]+\"\"\"<br>\n        </body>\n        </html>\"\"\"\n    ''')  # noqa\n    expected_formatted_code = textwrap.dedent('''\\\n        def f():\n          email_text += \"\"\"<html>This is a really long docstring that goes over the column limit and is multi-line.<br><br>\n        <b>Czar: </b>\"\"\" + despot[\"Nicholas\"] + \"\"\"<br>\n        <b>Minion: </b>\"\"\" + serf[\"Dmitri\"] + \"\"\"<br>\n        <b>Residence: </b>\"\"\" + palace[\"Winter\"] + \"\"\"<br>\n        </body>\n        </html>\"\"\"\n    ''')  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSimpleMultilineWithComments(self):\n    code = textwrap.dedent(\"\"\"\\\n        if (  # This is the first comment\n            a and  # This is the second comment\n            # This is the third comment\n            b):  # A trailing comment\n          # Whoa! A normal comment!!\n          pass  # Another trailing comment\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testMatchingParenSplittingMatching(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          raise RuntimeError('unable to find insertion point for target node',\n                             (target,))\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          raise RuntimeError('unable to find insertion point for target node',\n                             (target,))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testContinuationIndent(self):\n    unformatted_code = textwrap.dedent('''\\\n        class F:\n          def _ProcessArgLists(self, node):\n            \"\"\"Common method for processing argument lists.\"\"\"\n            for child in node.children:\n              if isinstance(child, pytree.Leaf):\n                self._SetTokenSubtype(\n                    child, subtype=_ARGLIST_TOKEN_TO_SUBTYPE.get(\n                        child.value, format_token.Subtype.NONE))\n    ''')\n    expected_formatted_code = textwrap.dedent('''\\\n        class F:\n\n          def _ProcessArgLists(self, node):\n            \"\"\"Common method for processing argument lists.\"\"\"\n            for child in node.children:\n              if isinstance(child, pytree.Leaf):\n                self._SetTokenSubtype(\n                    child,\n                    subtype=_ARGLIST_TOKEN_TO_SUBTYPE.get(child.value,\n                                                          format_token.Subtype.NONE))\n    ''')  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testTrailingCommaAndBracket(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a = { 42, }\n        b = ( 42, )\n        c = [ 42, ]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = {\n            42,\n        }\n        b = (42,)\n        c = [\n            42,\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testI18n(self):\n    code = textwrap.dedent(\"\"\"\\\n        N_('Some years ago - never mind how long precisely - having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world.')  # A comment is here.\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n    code = textwrap.dedent(\"\"\"\\\n        foo('Fake function call')  #. Some years ago - never mind how long precisely - having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world.\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testI18nCommentsInDataLiteral(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n          return collections.OrderedDict({\n              #. First i18n comment.\n              'bork': 'foo',\n\n              #. Second i18n comment.\n              'snork': 'bar#.*=\\\\\\\\0',\n          })\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testClosingBracketIndent(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n\n          def g():\n            while (xxxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz]) == 'aaaaaaaaaaa' and\n                   xxxxxxxxxxxxxxxxxxxxx(\n                       yyyyyyyyyyyyy[zzzzz].aaaaaaaa[0]) == 'bbbbbbb'):\n              pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testClosingBracketsInlinedInCall(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class Foo(object):\n\n          def bar(self):\n            self.aaaaaaaa = xxxxxxxxxxxxxxxxxxx.yyyyyyyyyyyyy(\n                self.cccccc.ddddddddd.eeeeeeee,\n                options={\n                    \"forkforkfork\": 1,\n                    \"borkborkbork\": 2,\n                    \"corkcorkcork\": 3,\n                    \"horkhorkhork\": 4,\n                    \"porkporkpork\": 5,\n                    })\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class Foo(object):\n\n          def bar(self):\n            self.aaaaaaaa = xxxxxxxxxxxxxxxxxxx.yyyyyyyyyyyyy(\n                self.cccccc.ddddddddd.eeeeeeee,\n                options={\n                    \"forkforkfork\": 1,\n                    \"borkborkbork\": 2,\n                    \"corkcorkcork\": 3,\n                    \"horkhorkhork\": 4,\n                    \"porkporkpork\": 5,\n                })\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testLineWrapInForExpression(self):\n    code = textwrap.dedent(\"\"\"\\\n        class A:\n\n          def x(self, node, name, n=1):\n            for i, child in enumerate(\n                itertools.ifilter(lambda c: pytree_utils.NodeName(c) == name,\n                                  node.pre_order())):\n              pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testFunctionCallContinuationLine(self):\n    code = textwrap.dedent(\"\"\"\\\n        class foo:\n\n          def bar(self, node, name, n=1):\n            if True:\n              if True:\n                return [(aaaaaaaaaa,\n                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n                             cccc, ddddddddddddddddddddddddddddddddddddd))]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testI18nNonFormatting(self):\n    code = textwrap.dedent(\"\"\"\\\n        class F(object):\n\n          def __init__(self, fieldname,\n                       #. Error message indicating an invalid e-mail address.\n                       message=N_('Please check your email address.'), **kwargs):\n            pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testNoSpaceBetweenUnaryOpAndOpeningParen(self):\n    code = textwrap.dedent(\"\"\"\\\n        if ~(a or b):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testCommentBeforeFuncDef(self):\n    code = textwrap.dedent(\"\"\"\\\n        class Foo(object):\n\n          a = 42\n\n          # This is a comment.\n          def __init__(self,\n                       xxxxxxx,\n                       yyyyy=0,\n                       zzzzzzz=None,\n                       aaaaaaaaaaaaaaaaaa=False,\n                       bbbbbbbbbbbbbbb=False):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testExcessLineCountWithDefaultKeywords(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class Fnord(object):\n          def Moo(self):\n            aaaaaaaaaaaaaaaa = self._bbbbbbbbbbbbbbbbbbbbbbb(\n                ccccccccccccc=ccccccccccccc, ddddddd=ddddddd, eeee=eeee,\n                fffff=fffff, ggggggg=ggggggg, hhhhhhhhhhhhh=hhhhhhhhhhhhh,\n                iiiiiii=iiiiiiiiiiiiii)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class Fnord(object):\n\n          def Moo(self):\n            aaaaaaaaaaaaaaaa = self._bbbbbbbbbbbbbbbbbbbbbbb(\n                ccccccccccccc=ccccccccccccc,\n                ddddddd=ddddddd,\n                eeee=eeee,\n                fffff=fffff,\n                ggggggg=ggggggg,\n                hhhhhhhhhhhhh=hhhhhhhhhhhhh,\n                iiiiiii=iiiiiiiiiiiiii)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSpaceAfterNotOperator(self):\n    code = textwrap.dedent(\"\"\"\\\n        if not (this and that):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testNoPenaltySplitting(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n          if True:\n            if True:\n              python_files.extend(\n                  os.path.join(filename, f)\n                  for f in os.listdir(filename)\n                  if IsPythonFile(os.path.join(filename, f)))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testExpressionPenalties(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n          if ((left.value == '(' and right.value == ')') or\n              (left.value == '[' and right.value == ']') or\n              (left.value == '{' and right.value == '}')):\n            return False\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testLineDepthOfSingleLineStatement(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        while True: continue\n        for x in range(3): continue\n        try: a = 42\n        except: b = 42\n        with open(a) as fd: a = fd.read()\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        while True:\n          continue\n        for x in range(3):\n          continue\n        try:\n          a = 42\n        except:\n          b = 42\n        with open(a) as fd:\n          a = fd.read()\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplitListWithTerminatingComma(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        FOO = ['bar', 'baz', 'mux', 'qux', 'quux', 'quuux', 'quuuux',\n          'quuuuux', 'quuuuuux', 'quuuuuuux', lambda a, b: 37,]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        FOO = [\n            'bar',\n            'baz',\n            'mux',\n            'qux',\n            'quux',\n            'quuux',\n            'quuuux',\n            'quuuuux',\n            'quuuuuux',\n            'quuuuuuux',\n            lambda a, b: 37,\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplitListWithInterspersedComments(self):\n    code = textwrap.dedent(\"\"\"\\\n        FOO = [\n            'bar',  # bar\n            'baz',  # baz\n            'mux',  # mux\n            'qux',  # qux\n            'quux',  # quux\n            'quuux',  # quuux\n            'quuuux',  # quuuux\n            'quuuuux',  # quuuuux\n            'quuuuuux',  # quuuuuux\n            'quuuuuuux',  # quuuuuuux\n            lambda a, b: 37  # lambda\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testRelativeImportStatements(self):\n    code = textwrap.dedent(\"\"\"\\\n        from ... import bork\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testSingleLineList(self):\n    # A list on a single line should prefer to remain contiguous.\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = aaaaaaaaaaa(\n            (\"...\", \".\"), \"..\",\n            \"..............................................\"\n        )\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = aaaaaaaaaaa(\n            (\"...\", \".\"), \"..\", \"..............................................\")\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testBlankLinesBeforeFunctionsNotInColumnZero(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        import signal\n\n\n        try:\n          signal.SIGALRM\n          # ..................................................................\n          # ...............................................................\n\n\n          def timeout(seconds=1):\n            pass\n        except:\n          pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        import signal\n\n        try:\n          signal.SIGALRM\n\n          # ..................................................................\n          # ...............................................................\n\n\n          def timeout(seconds=1):\n            pass\n        except:\n          pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testNoKeywordArgumentBreakage(self):\n    code = textwrap.dedent(\"\"\"\\\n        class A(object):\n\n          def b(self):\n            if self.aaaaaaaaaaaaaaaaaaaa not in self.bbbbbbbbbb(\n                cccccccccccccccccccc=True):\n              pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testTrailerOnSingleLine(self):\n    code = textwrap.dedent(\"\"\"\\\n        urlpatterns = patterns('', url(r'^$', 'homepage_view'),\n                               url(r'^/login/$', 'login_view'),\n                               url(r'^/login/$', 'logout_view'),\n                               url(r'^/user/(?P<username>\\\\w+)/$', 'profile_view'))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testIfConditionalParens(self):\n    code = textwrap.dedent(\"\"\"\\\n        class Foo:\n\n          def bar():\n            if True:\n              if (child.type == grammar_token.NAME and\n                  child.value in substatement_names):\n                pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testContinuationMarkers(self):\n    code = textwrap.dedent(\"\"\"\\\n        text = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. \"\\\\\n               \"Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur \"\\\\\n               \"ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis \"\\\\\n               \"sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. \"\\\\\n               \"Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet\"\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n    code = textwrap.dedent(\"\"\"\\\n        from __future__ import nested_scopes, generators, division, absolute_import, with_statement, \\\\\n            print_function, unicode_literals\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n    code = textwrap.dedent(\"\"\"\\\n        if aaaaaaaaa == 42 and bbbbbbbbbbbbbb == 42 and \\\\\n           cccccccc == 42:\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testCommentsWithContinuationMarkers(self):\n    code = textwrap.dedent(\"\"\"\\\n        def fn(arg):\n          v = fn2(key1=True,\n                  #c1\n                  key2=arg)\\\\\n                        .fn3()\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testMultipleContinuationMarkers(self):\n    code = textwrap.dedent(\"\"\"\\\n        xyz = \\\\\n            \\\\\n            some_thing()\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testContinuationMarkerAfterStringWithContinuation(self):\n    code = textwrap.dedent(\"\"\"\\\n        s = 'foo \\\\\n            bar' \\\\\n            .format()\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testEmptyContainers(self):\n    code = textwrap.dedent(\"\"\"\\\n        flags.DEFINE_list(\n            'output_dirs', [],\n            'Lorem ipsum dolor sit amet, consetetur adipiscing elit. Donec a diam lectus. '\n            'Sed sit amet ipsum mauris. Maecenas congue.')\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testSplitStringsIfSurroundedByParens(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a = foo.bar({'xxxxxxxxxxxxxxxxxxxxxxx' 'yyyyyyyyyyyyyyyyyyyyyyyyyy': baz[42]} + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' 'bbbbbbbbbbbbbbbbbbbbbbbbbb' 'cccccccccccccccccccccccccccccccc' 'ddddddddddddddddddddddddddddd')\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = foo.bar({'xxxxxxxxxxxxxxxxxxxxxxx'\n                     'yyyyyyyyyyyyyyyyyyyyyyyyyy': baz[42]} +\n                    'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n                    'bbbbbbbbbbbbbbbbbbbbbbbbbb'\n                    'cccccccccccccccccccccccccccccccc'\n                    'ddddddddddddddddddddddddddddd')\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    code = textwrap.dedent(\"\"\"\\\n        a = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' \\\n'bbbbbbbbbbbbbbbbbbbbbbbbbb' 'cccccccccccccccccccccccccccccccc' \\\n'ddddddddddddddddddddddddddddd'\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testMultilineShebang(self):\n    code = textwrap.dedent(\"\"\"\\\n        #!/bin/sh\n        if \"true\" : '''\\'\n        then\n\n        export FOO=123\n        exec /usr/bin/env python \"$0\" \"$@\"\n\n        exit 127\n        fi\n        '''\n\n        import os\n\n        assert os.environ['FOO'] == '123'\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testNoSplittingAroundTermOperators(self):\n    code = textwrap.dedent(\"\"\"\\\n        a_very_long_function_call_yada_yada_etc_etc_etc(long_arg1,\n                                                        long_arg2 / long_arg3)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testNoSplittingAroundCompOperators(self):\n    code = textwrap.dedent(\"\"\"\\\n        c = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa is not bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)\n        c = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)\n        c = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)\n\n        c = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa is bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)\n        c = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <= bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)\n    \"\"\")  # noqa\n    expected_code = textwrap.dedent(\"\"\"\\\n        c = (\n            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n            is not bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)\n        c = (\n            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n            in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)\n        c = (\n            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n            not in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)\n\n        c = (\n            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n            is bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)\n        c = (\n            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n            <= bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n  def testNoSplittingWithinSubscriptList(self):\n    code = textwrap.dedent(\"\"\"\\\n        somequitelongvariablename.somemember[(a, b)] = {\n            'somelongkey': 1,\n            'someotherlongkey': 2\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testExcessCharacters(self):\n    code = textwrap.dedent(\"\"\"\\\n        class foo:\n\n          def bar(self):\n            self.write(s=[\n                '%s%s %s' % ('many of really', 'long strings', '+ just makes up 81')\n            ])\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          if True:\n            if True:\n              if contract == allow_contract and attr_dict.get(if_attribute) == has_value:\n                return True\n    \"\"\")  # noqa\n    expected_code = textwrap.dedent(\"\"\"\\\n        def _():\n          if True:\n            if True:\n              if contract == allow_contract and attr_dict.get(\n                  if_attribute) == has_value:\n                return True\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n  def testDictSetGenerator(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo = {\n            variable: 'hello world. How are you today?'\n            for variable in fnord\n            if variable != 37\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        foo = {\n            x: x\n            for x in fnord\n        }\n    \"\"\")  # noqa\n    expected_code = textwrap.dedent(\"\"\"\\\n        foo = {x: x for x in fnord}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n  def testUnaryOpInDictionaryValue(self):\n    code = textwrap.dedent(\"\"\"\\\n        beta = \"123\"\n\n        test = {'alpha': beta[-1]}\n\n        print(beta[-1])\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testUnaryNotOperator(self):\n    code = textwrap.dedent(\"\"\"\\\n        if True:\n          if True:\n            if True:\n              if True:\n                remote_checksum = self.get_checksum(conn, tmp, dest, inject,\n                                                    not directory_prepended, source)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testRelaxArraySubscriptAffinity(self):\n    code = textwrap.dedent(\"\"\"\\\n        class A(object):\n\n          def f(self, aaaaaaaaa, bbbbbbbbbbbbb, row):\n            if True:\n              if True:\n                if True:\n                  if True:\n                    if row[4] is None or row[5] is None:\n                      bbbbbbbbbbbbb[\n                          '..............'] = row[5] if row[5] is not None else 5\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testFunctionCallInDict(self):\n    code = textwrap.dedent(\"\"\"\\\n        a = {'a': b(c=d, **e)}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testFunctionCallInNestedDict(self):\n    code = textwrap.dedent(\"\"\"\\\n        a = {'a': {'a': {'a': b(c=d, **e)}}}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testUnbreakableNot(self):\n    code = textwrap.dedent(\"\"\"\\\n        def test():\n          if not \"Foooooooooooooooooooooooooooooo\" or \"Foooooooooooooooooooooooooooooo\" == \"Foooooooooooooooooooooooooooooo\":\n            pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testSplitListWithComment(self):\n    code = textwrap.dedent(\"\"\"\\\n        a = [\n            'a',\n            'b',\n            'c'  # hello world\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testOverColumnLimit(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class Test:\n\n          def testSomething(self):\n            expected = {\n                ('aaaaaaaaaaaaa', 'bbbb'): 'ccccccccccccccccccccccccccccccccccccccccccc',\n                ('aaaaaaaaaaaaa', 'bbbb'): 'ccccccccccccccccccccccccccccccccccccccccccc',\n                ('aaaaaaaaaaaaa', 'bbbb'): 'ccccccccccccccccccccccccccccccccccccccccccc',\n            }\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class Test:\n\n          def testSomething(self):\n            expected = {\n                ('aaaaaaaaaaaaa', 'bbbb'):\n                    'ccccccccccccccccccccccccccccccccccccccccccc',\n                ('aaaaaaaaaaaaa', 'bbbb'):\n                    'ccccccccccccccccccccccccccccccccccccccccccc',\n                ('aaaaaaaaaaaaa', 'bbbb'):\n                    'ccccccccccccccccccccccccccccccccccccccccccc',\n            }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testEndingComment(self):\n    code = textwrap.dedent(\"\"\"\\\n        a = f(\n            a=\"something\",\n            b=\"something requiring comment which is quite long\",  # comment about b (pushes line over 79)\n            c=\"something else, about which comment doesn't make sense\")\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testContinuationSpaceRetention(self):\n    code = textwrap.dedent(\"\"\"\\\n        def fn():\n          return module \\\\\n                 .method(Object(data,\n                     fn2(arg)\n                 ))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testIfExpressionWithFunctionCall(self):\n    code = textwrap.dedent(\"\"\"\\\n        if x or z.y(\n            a,\n            c,\n            aaaaaaaaaaaaaaaaaaaaa=aaaaaaaaaaaaaaaaaa,\n            bbbbbbbbbbbbbbbbbbbbb=bbbbbbbbbbbbbbbbbb):\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testUnformattedAfterMultilineString(self):\n    code = textwrap.dedent(\"\"\"\\\n        def foo():\n          com_text = \\\\\n        '''\n        TEST\n        ''' % (input_fname, output_fname)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testNoSpacesAroundKeywordDefaultValues(self):\n    code = textwrap.dedent(\"\"\"\\\n        sources = {\n            'json': request.get_json(silent=True) or {},\n            'json2': request.get_json(silent=True),\n        }\n        json = request.get_json(silent=True) or {}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testNoSplittingBeforeEndingSubscriptBracket(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          if True:\n            status = cf.describe_stacks(StackName=stackname)[u'Stacks'][0][u'StackStatus']\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          if True:\n            status = cf.describe_stacks(\n                StackName=stackname)[u'Stacks'][0][u'StackStatus']\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testNoSplittingOnSingleArgument(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        xxxxxxxxxxxxxx = (re.search(r'(\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.)\\\\d+',\n                                    aaaaaaa.bbbbbbbbbbbb).group(1) +\n                          re.search(r'\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.(\\\\d+)',\n                                    ccccccc).group(1))\n        xxxxxxxxxxxxxx = (re.search(r'(\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.)\\\\d+',\n                                    aaaaaaa.bbbbbbbbbbbb).group(a.b) +\n                          re.search(r'\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.(\\\\d+)',\n                                    ccccccc).group(c.d))\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        xxxxxxxxxxxxxx = (\n            re.search(r'(\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.)\\\\d+', aaaaaaa.bbbbbbbbbbbb).group(1) +\n            re.search(r'\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.(\\\\d+)', ccccccc).group(1))\n        xxxxxxxxxxxxxx = (\n            re.search(r'(\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.)\\\\d+', aaaaaaa.bbbbbbbbbbbb).group(a.b) +\n            re.search(r'\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.(\\\\d+)', ccccccc).group(c.d))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplittingArraysSensibly(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        while True:\n          while True:\n            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = list['bbbbbbbbbbbbbbbbbbbbbbbbb'].split(',')\n            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = list('bbbbbbbbbbbbbbbbbbbbbbbbb').split(',')\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        while True:\n          while True:\n            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = list[\n                'bbbbbbbbbbbbbbbbbbbbbbbbb'].split(',')\n            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = list(\n                'bbbbbbbbbbbbbbbbbbbbbbbbb').split(',')\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testComprehensionForAndIf(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class f:\n\n          def __repr__(self):\n            tokens_repr = ','.join(['{0}({1!r})'.format(tok.name, tok.value) for tok in self._tokens])\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class f:\n\n          def __repr__(self):\n            tokens_repr = ','.join(\n                ['{0}({1!r})'.format(tok.name, tok.value) for tok in self._tokens])\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testFunctionCallArguments(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          if True:\n            pytree_utils.InsertNodesBefore(_CreateCommentsFromPrefix(\n                comment_prefix, comment_lineno, comment_column,\n                standalone=True), ancestor_at_indent)\n            pytree_utils.InsertNodesBefore(_CreateCommentsFromPrefix(\n                comment_prefix, comment_lineno, comment_column,\n                standalone=True))\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          if True:\n            pytree_utils.InsertNodesBefore(\n                _CreateCommentsFromPrefix(\n                    comment_prefix, comment_lineno, comment_column, standalone=True),\n                ancestor_at_indent)\n            pytree_utils.InsertNodesBefore(\n                _CreateCommentsFromPrefix(\n                    comment_prefix, comment_lineno, comment_column, standalone=True))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testBinaryOperators(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a = b ** 37\n        c = (20 ** -3) / (_GRID_ROWS ** (code_length - 10))\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = b**37\n        c = (20**-3) / (_GRID_ROWS**(code_length - 10))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n          if True:\n            if (self.stack[-1].split_before_closing_bracket and\n                # FIXME(morbo): Use the 'matching_bracket' instead of this.\n                # FIXME(morbo): Don't forget about tuples!\n                current.value in ']}'):\n              pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testContiguousList(self):\n    code = textwrap.dedent(\"\"\"\\\n        [retval1, retval2] = a_very_long_function(argument_1, argument2, argument_3,\n                                                  argument_4)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testArgsAndKwargsFormatting(self):\n    code = textwrap.dedent(\"\"\"\\\n        a(a=aaaaaaaaaaaaaaaaaaaaa,\n          b=aaaaaaaaaaaaaaaaaaaaaaaa,\n          c=aaaaaaaaaaaaaaaaaa,\n          *d,\n          **e)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n    code = textwrap.dedent(\"\"\"\\\n        def foo():\n          return [\n              Bar(xxx='some string',\n                  yyy='another long string',\n                  zzz='a third long string')\n          ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testCommentColumnLimitOverflow(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n          if True:\n            TaskManager.get_tags = MagicMock(\n                name='get_tags_mock',\n                return_value=[157031694470475],\n                # side_effect=[(157031694470475), (157031694470475),],\n            )\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testMultilineLambdas(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class SomeClass(object):\n          do_something = True\n\n          def succeeded(self, dddddddddddddd):\n            d = defer.succeed(None)\n\n            if self.do_something:\n              d.addCallback(lambda _: self.aaaaaa.bbbbbbbbbbbbbbbb.cccccccccccccccccccccccccccccccc(dddddddddddddd))\n            return d\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class SomeClass(object):\n          do_something = True\n\n          def succeeded(self, dddddddddddddd):\n            d = defer.succeed(None)\n\n            if self.do_something:\n              d.addCallback(lambda _: self.aaaaaa.bbbbbbbbbbbbbbbb.\n                            cccccccccccccccccccccccccccccccc(dddddddddddddd))\n            return d\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: yapf, allow_multiline_lambdas: true}'))\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testMultilineDictionaryKeys(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        MAP_WITH_LONG_KEYS = {\n            ('lorem ipsum', 'dolor sit amet'):\n                1,\n            ('consectetur adipiscing elit.', 'Vestibulum mauris justo, ornare eget dolor eget'):\n                2,\n            ('vehicula convallis nulla. Vestibulum dictum nisl in malesuada finibus.',):\n                3\n        }\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        MAP_WITH_LONG_KEYS = {\n            ('lorem ipsum', 'dolor sit amet'):\n                1,\n            ('consectetur adipiscing elit.',\n             'Vestibulum mauris justo, ornare eget dolor eget'):\n                2,\n            ('vehicula convallis nulla. Vestibulum dictum nisl in malesuada finibus.',):\n                3\n        }\n    \"\"\")  # noqa\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{based_on_style: yapf, '\n                                      'allow_multiline_dictionary_keys: true}'))\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testStableDictionaryFormatting(self):\n    code = textwrap.dedent(\"\"\"\\\n        class A(object):\n\n          def method(self):\n            filters = {\n                'expressions': [{\n                    'field': {\n                        'search_field': {\n                            'user_field': 'latest_party__number_of_guests'\n                        },\n                    }\n                }]\n            }\n    \"\"\")  # noqa\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{based_on_style: pep8, indent_width: 2, '\n                                      'continuation_indent_width: 4, '\n                                      'indent_dictionary_value: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(code)\n      reformatted_code = reformatter.Reformat(llines)\n      self.assertCodeEqual(code, reformatted_code)\n\n      llines = yapf_test_helper.ParseAndUnwrap(reformatted_code)\n      reformatted_code = reformatter.Reformat(llines)\n      self.assertCodeEqual(code, reformatted_code)\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testStableInlinedDictionaryFormatting(self):\n    try:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          def _():\n              url = \"http://{0}/axis-cgi/admin/param.cgi?{1}\".format(\n                  value, urllib.urlencode({'action': 'update', 'parameter': value}))\n      \"\"\")  # noqa\n      expected_formatted_code = textwrap.dedent(\"\"\"\\\n          def _():\n              url = \"http://{0}/axis-cgi/admin/param.cgi?{1}\".format(\n                  value, urllib.urlencode({\n                      'action': 'update',\n                      'parameter': value\n                  }))\n      \"\"\")\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      reformatted_code = reformatter.Reformat(llines)\n      self.assertCodeEqual(expected_formatted_code, reformatted_code)\n\n      llines = yapf_test_helper.ParseAndUnwrap(reformatted_code)\n      reformatted_code = reformatter.Reformat(llines)\n      self.assertCodeEqual(expected_formatted_code, reformatted_code)\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testDontSplitKeywordValueArguments(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def mark_game_scored(gid):\n          _connect.execute(_games.update().where(_games.c.gid == gid).values(\n              scored=True))\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def mark_game_scored(gid):\n          _connect.execute(\n              _games.update().where(_games.c.gid == gid).values(scored=True))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDontAddBlankLineAfterMultilineString(self):\n    code = textwrap.dedent(\"\"\"\\\n        query = '''SELECT id\n        FROM table\n        WHERE day in {}'''\n        days = \",\".join(days)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testFormattingListComprehensions(self):\n    code = textwrap.dedent(\"\"\"\\\n        def a():\n          if True:\n            if True:\n              if True:\n                columns = [\n                    x for x, y in self._heap_this_is_very_long if x.route[0] == choice\n                ]\n                self._heap = [x for x in self._heap if x.route and x.route[0] == choice]\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testNoSplittingWhenBinPacking(self):\n    code = textwrap.dedent(\"\"\"\\\n        a_very_long_function_name(\n            long_argument_name_1=1,\n            long_argument_name_2=2,\n            long_argument_name_3=3,\n            long_argument_name_4=4,\n        )\n\n        a_very_long_function_name(\n            long_argument_name_1=1, long_argument_name_2=2, long_argument_name_3=3,\n            long_argument_name_4=4\n        )\n    \"\"\")  # noqa\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: pep8, indent_width: 2, '\n              'continuation_indent_width: 4, indent_dictionary_value: True, '\n              'dedent_closing_brackets: True, '\n              'split_before_named_assigns: False}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(code)\n      reformatted_code = reformatter.Reformat(llines)\n      self.assertCodeEqual(code, reformatted_code)\n\n      llines = yapf_test_helper.ParseAndUnwrap(reformatted_code)\n      reformatted_code = reformatter.Reformat(llines)\n      self.assertCodeEqual(code, reformatted_code)\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testNotSplittingAfterSubscript(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.b(c == d[\n                'eeeeee']).ffffff():\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.b(\n            c == d['eeeeee']).ffffff():\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplittingOneArgumentList(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          if True:\n            if True:\n              if True:\n                if True:\n                  if True:\n                    boxes[id_] = np.concatenate((points.min(axis=0), qoints.max(axis=0)))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          if True:\n            if True:\n              if True:\n                if True:\n                  if True:\n                    boxes[id_] = np.concatenate(\n                        (points.min(axis=0), qoints.max(axis=0)))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplittingBeforeFirstElementListArgument(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n          @classmethod\n          def _pack_results_for_constraint_or(cls, combination, constraints):\n            if True:\n              if True:\n                if True:\n                  return cls._create_investigation_result(\n                          (\n                                  clue for clue in combination if not clue == Verifier.UNMATCHED\n                          ), constraints, InvestigationResult.OR\n                  )\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          @classmethod\n          def _pack_results_for_constraint_or(cls, combination, constraints):\n            if True:\n              if True:\n                if True:\n                  return cls._create_investigation_result(\n                      (clue for clue in combination if not clue == Verifier.UNMATCHED),\n                      constraints, InvestigationResult.OR)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplittingArgumentsTerminatedByComma(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        function_name(argument_name_1=1, argument_name_2=2, argument_name_3=3)\n\n        function_name(argument_name_1=1, argument_name_2=2, argument_name_3=3,)\n\n        a_very_long_function_name(long_argument_name_1=1, long_argument_name_2=2, long_argument_name_3=3, long_argument_name_4=4)\n\n        a_very_long_function_name(long_argument_name_1, long_argument_name_2, long_argument_name_3, long_argument_name_4,)\n\n        r =f0 (1,  2,3,)\n\n        r =f0 (1,)\n\n        r =f0 (a=1,)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        function_name(argument_name_1=1, argument_name_2=2, argument_name_3=3)\n\n        function_name(\n            argument_name_1=1,\n            argument_name_2=2,\n            argument_name_3=3,\n        )\n\n        a_very_long_function_name(\n            long_argument_name_1=1,\n            long_argument_name_2=2,\n            long_argument_name_3=3,\n            long_argument_name_4=4)\n\n        a_very_long_function_name(\n            long_argument_name_1,\n            long_argument_name_2,\n            long_argument_name_3,\n            long_argument_name_4,\n        )\n\n        r = f0(\n            1,\n            2,\n            3,\n        )\n\n        r = f0(\n            1,\n        )\n\n        r = f0(\n            a=1,\n        )\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: yapf, '\n              'split_arguments_when_comma_terminated: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      reformatted_code = reformatter.Reformat(llines)\n      self.assertCodeEqual(expected_formatted_code, reformatted_code)\n\n      llines = yapf_test_helper.ParseAndUnwrap(reformatted_code)\n      reformatted_code = reformatter.Reformat(llines)\n      self.assertCodeEqual(expected_formatted_code, reformatted_code)\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testImportAsList(self):\n    code = textwrap.dedent(\"\"\"\\\n        from toto import titi, tata, tutu  # noqa\n        from toto import titi, tata, tutu\n        from toto import (titi, tata, tutu)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testDictionaryValuesOnOwnLines(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a = {\n        'aaaaaaaaaaaaaaaaaaaaaaaa':\n            Check('ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ', '=', True),\n        'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb':\n            Check('YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY', '=', True),\n        'ccccccccccccccc':\n            Check('XXXXXXXXXXXXXXXXXXX', '!=', 'SUSPENDED'),\n        'dddddddddddddddddddddddddddddd':\n            Check('WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW', '=', False),\n        'eeeeeeeeeeeeeeeeeeeeeeeeeeeee':\n            Check('VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV', '=', False),\n        'ffffffffffffffffffffffffff':\n            Check('UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU', '=', True),\n        'ggggggggggggggggg':\n            Check('TTTTTTTTTTTTTTTTTTTTTTTTTTTTTT', '=', True),\n        'hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh':\n            Check('SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS', '=', True),\n        'iiiiiiiiiiiiiiiiiiiiiiii':\n            Check('RRRRRRRRRRRRRRRRRRRRRRRRRRR', '=', True),\n        'jjjjjjjjjjjjjjjjjjjjjjjjjj':\n            Check('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ', '=', False),\n        }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = {\n            'aaaaaaaaaaaaaaaaaaaaaaaa':\n                Check('ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ', '=', True),\n            'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb':\n                Check('YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY', '=', True),\n            'ccccccccccccccc':\n                Check('XXXXXXXXXXXXXXXXXXX', '!=', 'SUSPENDED'),\n            'dddddddddddddddddddddddddddddd':\n                Check('WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW', '=', False),\n            'eeeeeeeeeeeeeeeeeeeeeeeeeeeee':\n                Check('VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV', '=', False),\n            'ffffffffffffffffffffffffff':\n                Check('UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU', '=', True),\n            'ggggggggggggggggg':\n                Check('TTTTTTTTTTTTTTTTTTTTTTTTTTTTTT', '=', True),\n            'hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh':\n                Check('SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS', '=', True),\n            'iiiiiiiiiiiiiiiiiiiiiiii':\n                Check('RRRRRRRRRRRRRRRRRRRRRRRRRRR', '=', True),\n            'jjjjjjjjjjjjjjjjjjjjjjjjjj':\n                Check('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ', '=', False),\n        }\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDictionaryOnOwnLine(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        doc = test_utils.CreateTestDocumentViaController(\n            content={ 'a': 'b' },\n            branch_key=branch.key,\n            collection_key=collection.key)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        doc = test_utils.CreateTestDocumentViaController(\n            content={'a': 'b'}, branch_key=branch.key, collection_key=collection.key)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        doc = test_utils.CreateTestDocumentViaController(\n            content={ 'a': 'b' },\n            branch_key=branch.key,\n            collection_key=collection.key,\n            collection_key2=collection.key2)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        doc = test_utils.CreateTestDocumentViaController(\n            content={'a': 'b'},\n            branch_key=branch.key,\n            collection_key=collection.key,\n            collection_key2=collection.key2)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testNestedListsInDictionary(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        _A = {\n            'cccccccccc': ('^^1',),\n            'rrrrrrrrrrrrrrrrrrrrrrrrr': ('^7913',  # AAAAAAAAAAAAAA.\n                                         ),\n            'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee': ('^6242',  # BBBBBBBBBBBBBBB.\n                                                  ),\n            'vvvvvvvvvvvvvvvvvvv': ('^27959',  # CCCCCCCCCCCCCCCCCC.\n                                    '^19746',  # DDDDDDDDDDDDDDDDDDDDDDD.\n                                    '^22907',  # EEEEEEEEEEEEEEEEEEEEEEEE.\n                                    '^21098',  # FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.\n                                    '^22826',  # GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG.\n                                    '^22769',  # HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH.\n                                    '^22935',  # IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII.\n                                    '^3982',  # JJJJJJJJJJJJJ.\n                                   ),\n            'uuuuuuuuuuuu': ('^19745',  # LLLLLLLLLLLLLLLLLLLLLLLLLL.\n                             '^21324',  # MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM.\n                             '^22831',  # NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN.\n                             '^17081',  # OOOOOOOOOOOOOOOOOOOOO.\n                            ),\n            'eeeeeeeeeeeeee': (\n                '^9416',  # Reporter email. Not necessarily the reporter.\n                '^^3',  # This appears to be the raw email field.\n            ),\n            'cccccccccc': ('^21109',  # PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP.\n                          ),\n        }\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        _A = {\n            'cccccccccc': ('^^1',),\n            'rrrrrrrrrrrrrrrrrrrrrrrrr': (\n                '^7913',  # AAAAAAAAAAAAAA.\n            ),\n            'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee': (\n                '^6242',  # BBBBBBBBBBBBBBB.\n            ),\n            'vvvvvvvvvvvvvvvvvvv': (\n                '^27959',  # CCCCCCCCCCCCCCCCCC.\n                '^19746',  # DDDDDDDDDDDDDDDDDDDDDDD.\n                '^22907',  # EEEEEEEEEEEEEEEEEEEEEEEE.\n                '^21098',  # FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.\n                '^22826',  # GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG.\n                '^22769',  # HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH.\n                '^22935',  # IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII.\n                '^3982',  # JJJJJJJJJJJJJ.\n            ),\n            'uuuuuuuuuuuu': (\n                '^19745',  # LLLLLLLLLLLLLLLLLLLLLLLLLL.\n                '^21324',  # MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM.\n                '^22831',  # NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN.\n                '^17081',  # OOOOOOOOOOOOOOOOOOOOO.\n            ),\n            'eeeeeeeeeeeeee': (\n                '^9416',  # Reporter email. Not necessarily the reporter.\n                '^^3',  # This appears to be the raw email field.\n            ),\n            'cccccccccc': (\n                '^21109',  # PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP.\n            ),\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testNestedDictionary(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n          def _():\n            breadcrumbs = [{'name': 'Admin',\n                            'url': url_for(\".home\")},\n                           {'title': title},]\n            breadcrumbs = [{'name': 'Admin',\n                            'url': url_for(\".home\")},\n                           {'title': title}]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n          def _():\n            breadcrumbs = [\n                {\n                    'name': 'Admin',\n                    'url': url_for(\".home\")\n                },\n                {\n                    'title': title\n                },\n            ]\n            breadcrumbs = [{'name': 'Admin', 'url': url_for(\".home\")}, {'title': title}]\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDictionaryElementsOnOneLine(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          @mock.patch.dict(\n              os.environ,\n              {'HTTP_' + xsrf._XSRF_TOKEN_HEADER.replace('-', '_'): 'atoken'})\n          def _():\n            pass\n\n\n        AAAAAAAAAAAAAAAAAAAAAAAA = {\n            Environment.XXXXXXXXXX: 'some text more text even more tex',\n            Environment.YYYYYYY: 'some text more text even more text yet ag',\n            Environment.ZZZZZZZZZZZ: 'some text more text even more text yet again tex',\n        }\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testNotInParams(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        list(\"a long line to break the line. a long line to break the brk a long lin\", not True)\n    \"\"\")  # noqa\n    expected_code = textwrap.dedent(\"\"\"\\\n        list(\"a long line to break the line. a long line to break the brk a long lin\",\n             not True)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n  def testNamedAssignNotAtEndOfLine(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          if True:\n            with py3compat.open_with_encoding(filename, mode='w',\n                                              encoding=encoding) as fd:\n              pass\n    \"\"\")\n    expected_code = textwrap.dedent(\"\"\"\\\n        def _():\n          if True:\n            with py3compat.open_with_encoding(\n                filename, mode='w', encoding=encoding) as fd:\n              pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n  def testBlankLineBeforeClassDocstring(self):\n    unformatted_code = textwrap.dedent('''\\\n        class A:\n\n          \"\"\"Does something.\n\n          Also, here are some details.\n          \"\"\"\n\n          def __init__(self):\n            pass\n    ''')\n    expected_code = textwrap.dedent('''\\\n        class A:\n          \"\"\"Does something.\n\n          Also, here are some details.\n          \"\"\"\n\n          def __init__(self):\n            pass\n    ''')\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent('''\\\n        class A:\n\n          \"\"\"Does something.\n\n          Also, here are some details.\n          \"\"\"\n\n          def __init__(self):\n            pass\n    ''')\n    expected_formatted_code = textwrap.dedent('''\\\n        class A:\n\n          \"\"\"Does something.\n\n          Also, here are some details.\n          \"\"\"\n\n          def __init__(self):\n            pass\n    ''')\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: yapf, '\n              'blank_line_before_class_docstring: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testBlankLineBeforeModuleDocstring(self):\n    unformatted_code = textwrap.dedent('''\\\n        #!/usr/bin/env python\n        # -*- coding: utf-8 name> -*-\n\n        \"\"\"Some module docstring.\"\"\"\n\n\n        def foobar():\n          pass\n    ''')\n    expected_code = textwrap.dedent('''\\\n        #!/usr/bin/env python\n        # -*- coding: utf-8 name> -*-\n        \"\"\"Some module docstring.\"\"\"\n\n\n        def foobar():\n          pass\n    ''')\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent('''\\\n        #!/usr/bin/env python\n        # -*- coding: utf-8 name> -*-\n        \"\"\"Some module docstring.\"\"\"\n\n\n        def foobar():\n            pass\n    ''')\n    expected_formatted_code = textwrap.dedent('''\\\n        #!/usr/bin/env python\n        # -*- coding: utf-8 name> -*-\n\n        \"\"\"Some module docstring.\"\"\"\n\n\n        def foobar():\n            pass\n    ''')\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: pep8, '\n              'blank_line_before_module_docstring: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testTupleCohesion(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          this_is_a_very_long_function_name(an_extremely_long_variable_name, (\n              'a string that may be too long %s' % 'M15'))\n    \"\"\")\n    expected_code = textwrap.dedent(\"\"\"\\\n        def f():\n          this_is_a_very_long_function_name(\n              an_extremely_long_variable_name,\n              ('a string that may be too long %s' % 'M15'))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n  def testSubscriptExpression(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo = d[not a]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testSubscriptExpressionTerminatedByComma(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        A[B, C,]\n    \"\"\")\n    expected_code = textwrap.dedent(\"\"\"\\\n        A[\n            B,\n            C,\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n  def testListWithFunctionCalls(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          return [\n              Bar(\n                  xxx='some string',\n                  yyy='another long string',\n                  zzz='a third long string'), Bar(\n                      xxx='some string',\n                      yyy='another long string',\n                      zzz='a third long string')\n          ]\n    \"\"\")\n    expected_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          return [\n              Bar(xxx='some string',\n                  yyy='another long string',\n                  zzz='a third long string'),\n              Bar(xxx='some string',\n                  yyy='another long string',\n                  zzz='a third long string')\n          ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n  def testEllipses(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        X=...\n        Y = X if ... else X\n    \"\"\")\n    expected_code = textwrap.dedent(\"\"\"\\\n        X = ...\n        Y = X if ... else X\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n  def testPseudoParens(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        my_dict = {\n            'key':  # Some comment about the key\n                {'nested_key': 1, },\n        }\n    \"\"\")\n    expected_code = textwrap.dedent(\"\"\"\\\n        my_dict = {\n            'key':  # Some comment about the key\n                {\n                    'nested_key': 1,\n                },\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n  def testSplittingBeforeFirstArgumentOnFunctionCall(self):\n    \"\"\"Tests split_before_first_argument on a function call.\"\"\"\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a_very_long_function_name(\"long string with formatting {0:s}\".format(\n            \"mystring\"))\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a_very_long_function_name(\n            \"long string with formatting {0:s}\".format(\"mystring\"))\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: yapf, split_before_first_argument: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testSplittingBeforeFirstArgumentOnFunctionDefinition(self):\n    \"\"\"Tests split_before_first_argument on a function definition.\"\"\"\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _GetNumberOfSecondsFromElements(year, month, day, hours,\n                                            minutes, seconds, microseconds):\n          return\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _GetNumberOfSecondsFromElements(\n            year, month, day, hours, minutes, seconds, microseconds):\n          return\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: yapf, split_before_first_argument: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testSplittingBeforeFirstArgumentOnCompoundStatement(self):\n    \"\"\"Tests split_before_first_argument on a compound statement.\"\"\"\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if (long_argument_name_1 == 1 or\n            long_argument_name_2 == 2 or\n            long_argument_name_3 == 3 or\n            long_argument_name_4 == 4):\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if (long_argument_name_1 == 1 or long_argument_name_2 == 2 or\n            long_argument_name_3 == 3 or long_argument_name_4 == 4):\n          pass\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: yapf, split_before_first_argument: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testCoalesceBracketsOnDict(self):\n    \"\"\"Tests coalesce_brackets on a dictionary.\"\"\"\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        date_time_values = (\n            {\n                u'year': year,\n                u'month': month,\n                u'day_of_month': day_of_month,\n                u'hours': hours,\n                u'minutes': minutes,\n                u'seconds': seconds\n            }\n        )\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        date_time_values = ({\n            u'year': year,\n            u'month': month,\n            u'day_of_month': day_of_month,\n            u'hours': hours,\n            u'minutes': minutes,\n            u'seconds': seconds\n        })\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: yapf, coalesce_brackets: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testSplitAfterComment(self):\n    code = textwrap.dedent(\"\"\"\\\n        if __name__ == \"__main__\":\n          with another_resource:\n            account = {\n                \"validUntil\":\n                    int(time() + (6 * 7 * 24 * 60 * 60))  # in 6 weeks time\n            }\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: yapf, coalesce_brackets: True, '\n              'dedent_closing_brackets: true}'))\n      llines = yapf_test_helper.ParseAndUnwrap(code)\n      self.assertCodeEqual(code, reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testDisableEndingCommaHeuristic(self):\n    code = textwrap.dedent(\"\"\"\\\n        x = [1, 2, 3, 4, 5, 6, 7,]\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{based_on_style: yapf,'\n                                      ' disable_ending_comma_heuristic: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(code)\n      self.assertCodeEqual(code, reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testDedentClosingBracketsWithTypeAnnotationExceedingLineLength(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def function(first_argument_xxxxxxxxxxxxxxxx=(0,), second_argument=None) -> None:\n          pass\n\n\n        def function(first_argument_xxxxxxxxxxxxxxxxxxxxxxx=(0,), second_argument=None) -> None:\n          pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def function(\n            first_argument_xxxxxxxxxxxxxxxx=(0,), second_argument=None\n        ) -> None:\n          pass\n\n\n        def function(\n            first_argument_xxxxxxxxxxxxxxxxxxxxxxx=(0,), second_argument=None\n        ) -> None:\n          pass\n    \"\"\")  # noqa\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{based_on_style: yapf,'\n                                      ' dedent_closing_brackets: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testIndentClosingBracketsWithTypeAnnotationExceedingLineLength(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def function(first_argument_xxxxxxxxxxxxxxxx=(0,), second_argument=None) -> None:\n          pass\n\n\n        def function(first_argument_xxxxxxxxxxxxxxxxxxxxxxx=(0,), second_argument=None) -> None:\n          pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def function(\n            first_argument_xxxxxxxxxxxxxxxx=(0,), second_argument=None\n            ) -> None:\n          pass\n\n\n        def function(\n            first_argument_xxxxxxxxxxxxxxxxxxxxxxx=(0,), second_argument=None\n            ) -> None:\n          pass\n    \"\"\")  # noqa\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{based_on_style: yapf,'\n                                      ' indent_closing_brackets: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testIndentClosingBracketsInFunctionCall(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def function(first_argument_xxxxxxxxxxxxxxxx=(0,), second_argument=None, third_and_final_argument=True):\n          pass\n\n\n        def function(first_argument_xxxxxxxxxxxxxxxxxxxxxxx=(0,), second_and_last_argument=None):\n          pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def function(\n            first_argument_xxxxxxxxxxxxxxxx=(0,),\n            second_argument=None,\n            third_and_final_argument=True\n            ):\n          pass\n\n\n        def function(\n            first_argument_xxxxxxxxxxxxxxxxxxxxxxx=(0,), second_and_last_argument=None\n            ):\n          pass\n    \"\"\")  # noqa\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{based_on_style: yapf,'\n                                      ' indent_closing_brackets: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testIndentClosingBracketsInTuple(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def function():\n          some_var = ('a long element', 'another long element', 'short element', 'really really long element')\n          return True\n\n        def function():\n          some_var = ('a couple', 'small', 'elemens')\n          return False\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def function():\n          some_var = (\n              'a long element', 'another long element', 'short element',\n              'really really long element'\n              )\n          return True\n\n\n        def function():\n          some_var = ('a couple', 'small', 'elemens')\n          return False\n    \"\"\")  # noqa\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{based_on_style: yapf,'\n                                      ' indent_closing_brackets: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testIndentClosingBracketsInList(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def function():\n          some_var = ['a long element', 'another long element', 'short element', 'really really long element']\n          return True\n\n        def function():\n          some_var = ['a couple', 'small', 'elemens']\n          return False\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def function():\n          some_var = [\n              'a long element', 'another long element', 'short element',\n              'really really long element'\n              ]\n          return True\n\n\n        def function():\n          some_var = ['a couple', 'small', 'elemens']\n          return False\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{based_on_style: yapf,'\n                                      ' indent_closing_brackets: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testIndentClosingBracketsInDict(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def function():\n          some_var = {1: ('a long element', 'and another really really long element that is really really amazingly long'), 2: 'another long element', 3: 'short element', 4: 'really really long element'}\n          return True\n\n        def function():\n          some_var = {1: 'a couple', 2: 'small', 3: 'elemens'}\n          return False\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def function():\n          some_var = {\n              1:\n                  (\n                      'a long element',\n                      'and another really really long element that is really really amazingly long'\n                      ),\n              2: 'another long element',\n              3: 'short element',\n              4: 'really really long element'\n              }\n          return True\n\n\n        def function():\n          some_var = {1: 'a couple', 2: 'small', 3: 'elemens'}\n          return False\n    \"\"\")  # noqa\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{based_on_style: yapf,'\n                                      ' indent_closing_brackets: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testMultipleDictionariesInList(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class A:\n            def b():\n                d = {\n                    \"123456\": [\n                        {\n                            \"12\": \"aa\"\n                        },\n                        {\n                            \"12\": \"bb\"\n                        },\n                        {\n                            \"12\": \"cc\",\n                            \"1234567890\": {\n                                \"1234567\": [{\n                                    \"12\": \"dd\",\n                                    \"12345\": \"text 1\"\n                                }, {\n                                    \"12\": \"ee\",\n                                    \"12345\": \"text 2\"\n                                }]\n                            }\n                        }\n                    ]\n                }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class A:\n\n          def b():\n            d = {\n                \"123456\": [{\n                    \"12\": \"aa\"\n                }, {\n                    \"12\": \"bb\"\n                }, {\n                    \"12\": \"cc\",\n                    \"1234567890\": {\n                        \"1234567\": [{\n                            \"12\": \"dd\",\n                            \"12345\": \"text 1\"\n                        }, {\n                            \"12\": \"ee\",\n                            \"12345\": \"text 2\"\n                        }]\n                    }\n                }]\n            }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testForceMultilineDict_True(self):\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{force_multiline_dict: true}'))\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          responseDict = {'childDict': {'spam': 'eggs'}}\n          generatedDict = {x: x for x in 'value'}\n      \"\"\")\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      actual = reformatter.Reformat(llines)\n      expected = textwrap.dedent(\"\"\"\\\n          responseDict = {\n              'childDict': {\n                  'spam': 'eggs'\n              }\n          }\n          generatedDict = {\n              x: x for x in 'value'\n          }\n      \"\"\")\n      self.assertCodeEqual(expected, actual)\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testForceMultilineDict_False(self):\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{force_multiline_dict: false}'))\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          responseDict = {'childDict': {'spam': 'eggs'}}\n          generatedDict = {x: x for x in 'value'}\n      \"\"\")\n      expected_formatted_code = unformatted_code\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testWalrus(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if (x  :=  len([1]*1000)>100):\n          print(f'{x} is pretty big' )\n    \"\"\")\n    expected = textwrap.dedent(\"\"\"\\\n        if (x := len([1] * 1000) > 100):\n          print(f'{x} is pretty big')\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected, reformatter.Reformat(llines))\n\n  def testStructuredPatternMatching(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        match command.split():\n          case[action   ]:\n            ...  # interpret single-verb action\n          case[action,    obj]:\n            ...  # interpret action, obj\n    \"\"\")\n    expected = textwrap.dedent(\"\"\"\\\n        match command.split():\n          case [action]:\n            ...  # interpret single-verb action\n          case [action, obj]:\n            ...  # interpret action, obj\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected, reformatter.Reformat(llines))\n\n  def testParenthesizedContextManagers(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        with (cert_authority.cert_pem.tempfile() as ca_temp_path, patch.object(os, 'environ', os.environ | {'REQUESTS_CA_BUNDLE': ca_temp_path}),):\n            httpserver_url = httpserver.url_for('/resource.jar')\n    \"\"\")  # noqa: E501\n    expected = textwrap.dedent(\"\"\"\\\n        with (\n            cert_authority.cert_pem.tempfile() as ca_temp_path,\n            patch.object(os, 'environ',\n                         os.environ | {'REQUESTS_CA_BUNDLE': ca_temp_path}),\n        ):\n          httpserver_url = httpserver.url_for('/resource.jar')\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected, reformatter.Reformat(llines))\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/reformatter_buganizer_test.py",
    "content": "# Copyright 2016 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Buganizer tests for yapf.reformatter.\"\"\"\n\nimport textwrap\nimport unittest\n\nfrom yapf.yapflib import reformatter\nfrom yapf.yapflib import style\n\nfrom yapftests import yapf_test_helper\n\n\nclass BuganizerFixes(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):\n    style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testB137580392(self):\n    code = textwrap.dedent(\"\"\"\\\n        def _create_testing_simulator_and_sink(\n        ) -> Tuple[_batch_simulator:_batch_simulator.BatchSimulator,\n                   _batch_simulator.SimulationSink]:\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB73279849(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class A:\n            def _(a):\n                return 'hello'  [  a  ]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class A:\n          def _(a):\n            return 'hello'[a]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB122455211(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        _zzzzzzzzzzzzzzzzzzzz = Union[sssssssssssssssssssss.pppppppppppppppp,\n                             sssssssssssssssssssss.pppppppppppppppppppppppppppp]\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        _zzzzzzzzzzzzzzzzzzzz = Union[\n            sssssssssssssssssssss.pppppppppppppppp,\n            sssssssssssssssssssss.pppppppppppppppppppppppppppp]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB119300344(self):\n    code = textwrap.dedent(\"\"\"\\\n        def _GenerateStatsEntries(\n            process_id: Text,\n            timestamp: Optional[rdfvalue.RDFDatetime] = None\n        ) -> Sequence[stats_values.StatsStoreEntry]:\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB132886019(self):\n    code = textwrap.dedent(\"\"\"\\\n        X = {\n            'some_dict_key':\n                frozenset([\n                    # pylint: disable=line-too-long\n                    '//this/path/is/really/too/long/for/this/line/and/probably/should/be/split',\n                ]),\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB26521719(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def _(self):\n            self.stubs.Set(some_type_of_arg, 'ThisIsAStringArgument',\n                           lambda *unused_args, **unused_kwargs: fake_resolver)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB122541552(self):\n    code = textwrap.dedent(\"\"\"\\\n        # pylint: disable=g-explicit-bool-comparison,singleton-comparison\n        _QUERY = account.Account.query(account.Account.enabled == True)\n        # pylint: enable=g-explicit-bool-comparison,singleton-comparison\n\n\n        def _():\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB124415889(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def run_queue_scanners():\n            return xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(\n                {\n                    components.NAME.FNOR: True,\n                    components.NAME.DEVO: True,\n                },\n                default=False)\n\n          def modules_to_install():\n            modules = DeepCopy(GetDef({}))\n            modules.update({\n                'xxxxxxxxxxxxxxxxxxxx':\n                    GetDef('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz', None),\n            })\n            return modules\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB73166511(self):\n    code = textwrap.dedent(\"\"\"\\\n        def _():\n          if min_std is not None:\n            groundtruth_age_variances = tf.maximum(groundtruth_age_variances,\n                                                   min_std**2)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB118624921(self):\n    code = textwrap.dedent(\"\"\"\\\n        def _():\n          function_call(\n              alert_name='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',\n              time_delta='1h',\n              alert_level='bbbbbbbb',\n              metric='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',\n              bork=foo)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB35417079(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def _():\n            X = (\n                _ares_label_prefix +\n                'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'  # pylint: disable=line-too-long\n                'PyTypePyTypePyTypePyTypePyTypePyTypePyTypePyTypePyTypePyTypePyTypePyTypePyType'  # pytype: disable=attribute-error\n                'CopybaraCopybaraCopybaraCopybaraCopybaraCopybaraCopybaraCopybaraCopybara'  # copybara:strip\n            )\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB120047670(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        X = {\n            'NO_PING_COMPONENTS': [\n                79775,          # Releases / FOO API\n                79770,          # Releases / BAZ API\n                79780],         # Releases / MUX API\n\n            'PING_BLOCKED_BUGS': False,\n        }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        X = {\n            'NO_PING_COMPONENTS': [\n                79775,  # Releases / FOO API\n                79770,  # Releases / BAZ API\n                79780\n            ],  # Releases / MUX API\n            'PING_BLOCKED_BUGS': False,\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB120245013(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class Foo(object):\n          def testNoAlertForShortPeriod(self, rutabaga):\n            self.targets[:][streamz_path,self._fillInOtherFields(streamz_path, {streamz_field_of_interest:True})] = series.Counter('1s', '+ 500x10000')\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class Foo(object):\n\n          def testNoAlertForShortPeriod(self, rutabaga):\n            self.targets[:][\n                streamz_path,\n                self._fillInOtherFields(streamz_path, {streamz_field_of_interest: True}\n                                       )] = series.Counter('1s', '+ 500x10000')\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB117841880(self):\n    code = textwrap.dedent(\"\"\"\\\n        def xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(\n            aaaaaaaaaaaaaaaaaaa: AnyStr,\n            bbbbbbbbbbbb: Optional[Sequence[AnyStr]] = None,\n            cccccccccc: AnyStr = cst.DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD,\n            dddddddddd: Sequence[SliceDimension] = (),\n            eeeeeeeeeeee: AnyStr = cst.DEFAULT_CONTROL_NAME,\n            ffffffffffffffffffff: Optional[Callable[[pd.DataFrame],\n                                                    pd.DataFrame]] = None,\n            gggggggggggggg: ooooooooooooo = ooooooooooooo()\n        ) -> pd.DataFrame:\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB111764402(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        x = self.stubs.stub(video_classification_map,              'read_video_classifications',       (lambda external_ids, **unused_kwargs:                     {external_id: self._get_serving_classification('video') for external_id in external_ids}))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        x = self.stubs.stub(video_classification_map, 'read_video_classifications',\n                            (lambda external_ids, **unused_kwargs: {\n                                external_id: self._get_serving_classification('video')\n                                for external_id in external_ids\n                            }))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB116825060(self):\n    code = textwrap.dedent(\"\"\"\\\n        result_df = pd.DataFrame({LEARNED_CTR_COLUMN: learned_ctr},\n                                 index=df_metrics.index)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB112711217(self):\n    code = textwrap.dedent(\"\"\"\\\n        def _():\n          stats['moderated'] = ~stats.moderation_reason.isin(\n              approved_moderation_reasons)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB112867548(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          return flask.make_response(\n              'Records: {}, Problems: {}, More: {}'.format(\n                  process_result.result_ct, process_result.problem_ct,\n                  process_result.has_more),\n              httplib.ACCEPTED if process_result.has_more else httplib.OK,\n              {'content-type': _TEXT_CONTEXT_TYPE})\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          return flask.make_response(\n              'Records: {}, Problems: {}, More: {}'.format(process_result.result_ct,\n                                                           process_result.problem_ct,\n                                                           process_result.has_more),\n              httplib.ACCEPTED if process_result.has_more else httplib.OK,\n              {'content-type': _TEXT_CONTEXT_TYPE})\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB112651423(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def potato(feeditems, browse_use_case=None):\n          for item in turnip:\n            if kumquat:\n              if not feeds_variants.variants['FEEDS_LOAD_PLAYLIST_VIDEOS_FOR_ALL_ITEMS'] and item.video:\n                continue\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def potato(feeditems, browse_use_case=None):\n          for item in turnip:\n            if kumquat:\n              if not feeds_variants.variants[\n                  'FEEDS_LOAD_PLAYLIST_VIDEOS_FOR_ALL_ITEMS'] and item.video:\n                continue\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB80484938(self):\n    code = textwrap.dedent(\"\"\"\\\n        for sssssss, aaaaaaaaaa in [\n            ('ssssssssssssssssssss', 'sssssssssssssssssssssssss'),\n            ('nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn',\n             'nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn'),\n            ('pppppppppppppppppppppppppppp', 'pppppppppppppppppppppppppppppppp'),\n            ('wwwwwwwwwwwwwwwwwwww', 'wwwwwwwwwwwwwwwwwwwwwwwww'),\n            ('sssssssssssssssss', 'sssssssssssssssssssssss'),\n            ('ggggggggggggggggggggggg', 'gggggggggggggggggggggggggggg'),\n            ('ggggggggggggggggg', 'gggggggggggggggggggggg'),\n            ('eeeeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeeeeeeeeee')\n        ]:\n          pass\n\n        for sssssss, aaaaaaaaaa in [\n            ('ssssssssssssssssssss', 'sssssssssssssssssssssssss'),\n            ('nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn', 'nnnnnnnnnnnnnnnnnnnnnnnnn'),\n            ('pppppppppppppppppppppppppppp', 'pppppppppppppppppppppppppppppppp'),\n            ('wwwwwwwwwwwwwwwwwwww', 'wwwwwwwwwwwwwwwwwwwwwwwww'),\n            ('sssssssssssssssss', 'sssssssssssssssssssssss'),\n            ('ggggggggggggggggggggggg', 'gggggggggggggggggggggggggggg'),\n            ('ggggggggggggggggg', 'gggggggggggggggggggggg'),\n            ('eeeeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeeeeeeeeee')\n        ]:\n          pass\n\n        for sssssss, aaaaaaaaaa in [\n            ('ssssssssssssssssssss', 'sssssssssssssssssssssssss'),\n            ('nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn',\n             'nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn'),\n            ('pppppppppppppppppppppppppppp', 'pppppppppppppppppppppppppppppppp'),\n            ('wwwwwwwwwwwwwwwwwwww', 'wwwwwwwwwwwwwwwwwwwwwwwww'),\n            ('sssssssssssssssss', 'sssssssssssssssssssssss'),\n            ('ggggggggggggggggggggggg', 'gggggggggggggggggggggggggggg'),\n            ('ggggggggggggggggg', 'gggggggggggggggggggggg'),\n            ('eeeeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeeeeeeeeee'),\n        ]:\n          pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB120771563(self):\n    code = textwrap.dedent(\"\"\"\\\n        class A:\n\n          def b():\n            d = {\n                \"123456\": [{\n                    \"12\": \"aa\"\n                }, {\n                    \"12\": \"bb\"\n                }, {\n                    \"12\": \"cc\",\n                    \"1234567890\": {\n                        \"1234567\": [{\n                            \"12\": \"dd\",\n                            \"12345\": \"text 1\"\n                        }, {\n                            \"12\": \"ee\",\n                            \"12345\": \"text 2\"\n                        }]\n                    }\n                }]\n            }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB79462249(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo.bar(baz, [\n            quux(thud=42),\n            norf,\n        ])\n        foo.bar(baz, [\n            quux(),\n            norf,\n        ])\n        foo.bar(baz, quux(thud=42), aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbb,\n                ccccccccccccccccccc)\n        foo.bar(\n            baz,\n            quux(thud=42),\n            aaaaaaaaaaaaaaaaaaaaaa=1,\n            bbbbbbbbbbbbbbbbbbbbb=2,\n            ccccccccccccccccccc=3)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB113210278(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          aaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccccccccccc(\\\n        eeeeeeeeeeeeeeeeeeeeeeeeee.fffffffffffffffffffffffffffffffffffffff.\\\n        ggggggggggggggggggggggggggggggggg.hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh())\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          aaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccccccccccc(\n              eeeeeeeeeeeeeeeeeeeeeeeeee.fffffffffffffffffffffffffffffffffffffff\n              .ggggggggggggggggggggggggggggggggg.hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh())\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB77923341(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n          if (aaaaaaaaaaaaaa.bbbbbbbbbbbb.ccccc <= 0 and  # pytype: disable=attribute-error\n              ddddddddddd.eeeeeeeee == constants.FFFFFFFFFFFFFF):\n            raise \"yo\"\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB77329955(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          @parameterized.named_parameters(\n              ('ReadyExpiredSuccess', True, True, True, None, None),\n              ('SpannerUpdateFails', True, False, True, None, None),\n              ('ReadyNotExpired', False, True, True, True, None),\n              # ('ReadyNotExpiredNotHealthy', False, True, True, False, True),\n              # ('ReadyNotExpiredNotHealthyErrorFails', False, True, True, False, False\n              # ('ReadyNotExpiredNotHealthyUpdateFails', False, False, True, False, True\n          )\n          def _():\n            pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB65197969(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def _():\n            return timedelta(seconds=max(float(time_scale), small_interval) *\n                           1.41 ** min(num_attempts, 9))\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def _():\n            return timedelta(\n                seconds=max(float(time_scale), small_interval) *\n                1.41**min(num_attempts, 9))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB65546221(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        SUPPORTED_PLATFORMS = (\n            \"centos-6\",\n            \"centos-7\",\n            \"ubuntu-1204-precise\",\n            \"ubuntu-1404-trusty\",\n            \"ubuntu-1604-xenial\",\n            \"debian-7-wheezy\",\n            \"debian-8-jessie\",\n            \"debian-9-stretch\",)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        SUPPORTED_PLATFORMS = (\n            \"centos-6\",\n            \"centos-7\",\n            \"ubuntu-1204-precise\",\n            \"ubuntu-1404-trusty\",\n            \"ubuntu-1604-xenial\",\n            \"debian-7-wheezy\",\n            \"debian-8-jessie\",\n            \"debian-9-stretch\",\n        )\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB30500455(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        INITIAL_SYMTAB = dict([(name, 'exception#' + name) for name in INITIAL_EXCEPTIONS\n        ] * [(name, 'type#' + name) for name in INITIAL_TYPES] + [\n            (name, 'function#' + name) for name in INITIAL_FUNCTIONS\n        ] + [(name, 'const#' + name) for name in INITIAL_CONSTS])\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        INITIAL_SYMTAB = dict(\n            [(name, 'exception#' + name) for name in INITIAL_EXCEPTIONS] *\n            [(name, 'type#' + name) for name in INITIAL_TYPES] +\n            [(name, 'function#' + name) for name in INITIAL_FUNCTIONS] +\n            [(name, 'const#' + name) for name in INITIAL_CONSTS])\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB38343525(self):\n    code = textwrap.dedent(\"\"\"\\\n        # This does foo.\n        @arg.String('some_path_to_a_file', required=True)\n        # This does bar.\n        @arg.String('some_path_to_a_file', required=True)\n        def f():\n          print(1)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB37099651(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        _MEMCACHE = lazy.MakeLazy(\n            # pylint: disable=g-long-lambda\n            lambda: function.call.mem.clients(FLAGS.some_flag_thingy, default_namespace=_LAZY_MEM_NAMESPACE, allow_pickle=True)\n            # pylint: enable=g-long-lambda\n        )\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        _MEMCACHE = lazy.MakeLazy(\n            # pylint: disable=g-long-lambda\n            lambda: function.call.mem.clients(\n                FLAGS.some_flag_thingy,\n                default_namespace=_LAZY_MEM_NAMESPACE,\n                allow_pickle=True)\n            # pylint: enable=g-long-lambda\n        )\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB33228502(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          success_rate_stream_table = module.Precompute(\n              query_function=module.DefineQueryFunction(\n                  name='Response error ratio',\n                  expression=((m.Fetch(\n                          m.Raw('monarch.BorgTask',\n                                '/corp/travel/trips2/dispatcher/email/response'),\n                          {'borg_job': module_config.job, 'metric:response_type': 'SUCCESS'}),\n                       m.Fetch(m.Raw('monarch.BorgTask', '/corp/travel/trips2/dispatcher/email/response'), {'borg_job': module_config.job}))\n                      | m.Window(m.Delta('1h'))\n                      | m.Join('successes', 'total')\n                      | m.Point(m.VAL['successes'] / m.VAL['total']))))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          success_rate_stream_table = module.Precompute(\n              query_function=module.DefineQueryFunction(\n                  name='Response error ratio',\n                  expression=(\n                      (m.Fetch(\n                          m.Raw('monarch.BorgTask',\n                                '/corp/travel/trips2/dispatcher/email/response'), {\n                                    'borg_job': module_config.job,\n                                    'metric:response_type': 'SUCCESS'\n                                }),\n                       m.Fetch(\n                           m.Raw('monarch.BorgTask',\n                                 '/corp/travel/trips2/dispatcher/email/response'),\n                           {'borg_job': module_config.job}))\n                      | m.Window(m.Delta('1h'))\n                      | m.Join('successes', 'total')\n                      | m.Point(m.VAL['successes'] / m.VAL['total']))))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB30394228(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def _(self):\n            return some.randome.function.calling(\n                wf, None, alert.Format(alert.subject, alert=alert, threshold=threshold),\n                alert.Format(alert.body, alert=alert, threshold=threshold),\n                alert.html_formatting)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB65246454(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def _(self):\n            self.assertEqual({i.id\n                              for i in successful_instances},\n                             {i.id\n                              for i in self._statuses.successful_instances})\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def _(self):\n            self.assertEqual({i.id for i in successful_instances},\n                             {i.id for i in self._statuses.successful_instances})\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB67935450(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          return (\n              (Gauge(\n                  metric='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',\n                  group_by=group_by + ['metric:process_name'],\n                  metric_filter={'metric:process_name': process_name_re}),\n               Gauge(\n                   metric='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',\n                   group_by=group_by + ['metric:process_name'],\n                   metric_filter={'metric:process_name': process_name_re}))\n              | expr.Join(\n                  left_name='start', left_default=0, right_name='end', right_default=0)\n              | m.Point(\n                  m.Cond(m.VAL['end'] != 0, m.VAL['end'], k.TimestampMicros() /\n                         1000000L) - m.Cond(m.VAL['start'] != 0, m.VAL['start'],\n                                            m.TimestampMicros() / 1000000L)))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          return (\n              (Gauge(\n                  metric='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',\n                  group_by=group_by + ['metric:process_name'],\n                  metric_filter={'metric:process_name': process_name_re}),\n               Gauge(\n                   metric='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',\n                   group_by=group_by + ['metric:process_name'],\n                   metric_filter={'metric:process_name': process_name_re}))\n              | expr.Join(\n                  left_name='start', left_default=0, right_name='end', right_default=0)\n              | m.Point(\n                  m.Cond(m.VAL['end'] != 0, m.VAL['end'],\n                         k.TimestampMicros() / 1000000L) -\n                  m.Cond(m.VAL['start'] != 0, m.VAL['start'],\n                         m.TimestampMicros() / 1000000L)))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB66011084(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        X = {\n        \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\":  # Comment 1.\n        ([] if True else [ # Comment 2.\n            \"bbbbbbbbbbbbbbbbbbb\",  # Comment 3.\n            \"cccccccccccccccccccccccc\", # Comment 4.\n            \"ddddddddddddddddddddddddd\", # Comment 5.\n            \"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\", # Comment 6.\n            \"fffffffffffffffffffffffffffffff\", # Comment 7.\n            \"ggggggggggggggggggggggggggg\", # Comment 8.\n            \"hhhhhhhhhhhhhhhhhh\",  # Comment 9.\n        ]),\n        }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        X = {\n            \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\":  # Comment 1.\n                ([] if True else [  # Comment 2.\n                    \"bbbbbbbbbbbbbbbbbbb\",  # Comment 3.\n                    \"cccccccccccccccccccccccc\",  # Comment 4.\n                    \"ddddddddddddddddddddddddd\",  # Comment 5.\n                    \"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\",  # Comment 6.\n                    \"fffffffffffffffffffffffffffffff\",  # Comment 7.\n                    \"ggggggggggggggggggggggggggg\",  # Comment 8.\n                    \"hhhhhhhhhhhhhhhhhh\",  # Comment 9.\n                ]),\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB67455376(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        sponge_ids.extend(invocation.id() for invocation in self._client.GetInvocationsByLabels(labels))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        sponge_ids.extend(invocation.id()\n                          for invocation in self._client.GetInvocationsByLabels(labels))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB35210351(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          config.AnotherRuleThing(\n              'the_title_to_the_thing_here',\n              {'monitorname': 'firefly',\n               'service': ACCOUNTING_THING,\n               'severity': 'the_bug',\n               'monarch_module_name': alerts.TheLabel(qa_module_regexp, invert=True)},\n              fanout,\n              alerts.AlertUsToSomething(\n                  GetTheAlertToIt('the_title_to_the_thing_here'),\n                  GetNotificationTemplate('your_email_here')))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          config.AnotherRuleThing(\n              'the_title_to_the_thing_here', {\n                  'monitorname': 'firefly',\n                  'service': ACCOUNTING_THING,\n                  'severity': 'the_bug',\n                  'monarch_module_name': alerts.TheLabel(qa_module_regexp, invert=True)\n              }, fanout,\n              alerts.AlertUsToSomething(\n                  GetTheAlertToIt('the_title_to_the_thing_here'),\n                  GetNotificationTemplate('your_email_here')))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB34774905(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        x=[VarExprType(ir_name=IrName( value='x',\n        expr_type=UnresolvedAttrExprType( atom=UnknownExprType(), attr_name=IrName(\n            value='x', expr_type=UnknownExprType(), usage='UNKNOWN', fqn=None,\n            astn=None), usage='REF'), usage='ATTR', fqn='<attr>.x', astn=None))]\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        x = [\n            VarExprType(\n                ir_name=IrName(\n                    value='x',\n                    expr_type=UnresolvedAttrExprType(\n                        atom=UnknownExprType(),\n                        attr_name=IrName(\n                            value='x',\n                            expr_type=UnknownExprType(),\n                            usage='UNKNOWN',\n                            fqn=None,\n                            astn=None),\n                        usage='REF'),\n                    usage='ATTR',\n                    fqn='<attr>.x',\n                    astn=None))\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB65176185(self):\n    code = textwrap.dedent(\"\"\"\\\n        xx = zip(*[(a, b) for (a, b, c) in yy])\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB35210166(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          query = (\n              m.Fetch(n.Raw('monarch.BorgTask', '/proc/container/memory/usage'), { 'borg_user': borguser, 'borg_job': jobname })\n              | o.Window(m.Align('5m')) | p.GroupBy(['borg_user', 'borg_job', 'borg_cell'], q.Mean()))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          query = (\n              m.Fetch(\n                  n.Raw('monarch.BorgTask', '/proc/container/memory/usage'), {\n                      'borg_user': borguser,\n                      'borg_job': jobname\n                  })\n              | o.Window(m.Align('5m'))\n              | p.GroupBy(['borg_user', 'borg_job', 'borg_cell'], q.Mean()))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB32167774(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        X = (\n            'is_official',\n            'is_cover',\n            'is_remix',\n            'is_instrumental',\n            'is_live',\n            'has_lyrics',\n            'is_album',\n            'is_compilation',)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        X = (\n            'is_official',\n            'is_cover',\n            'is_remix',\n            'is_instrumental',\n            'is_live',\n            'has_lyrics',\n            'is_album',\n            'is_compilation',\n        )\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB66912275(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          with self.assertRaisesRegexp(errors.HttpError, 'Invalid'):\n            patch_op = api_client.forwardingRules().patch(\n                project=project_id,\n                region=region,\n                forwardingRule=rule_name,\n                body={'fingerprint': base64.urlsafe_b64encode('invalid_fingerprint')}).execute()\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          with self.assertRaisesRegexp(errors.HttpError, 'Invalid'):\n            patch_op = api_client.forwardingRules().patch(\n                project=project_id,\n                region=region,\n                forwardingRule=rule_name,\n                body={\n                    'fingerprint': base64.urlsafe_b64encode('invalid_fingerprint')\n                }).execute()\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB67312284(self):\n    code = textwrap.dedent(\"\"\"\\\n        def _():\n          self.assertEqual(\n              [u'to be published 2', u'to be published 1', u'to be published 0'],\n              [el.text for el in page.first_column_tds])\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB65241516(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        checkpoint_files = gfile.Glob(os.path.join(TrainTraceDir(unit_key, \"*\", \"*\"), embedding_model.CHECKPOINT_FILENAME + \"-*\"))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        checkpoint_files = gfile.Glob(\n            os.path.join(\n                TrainTraceDir(unit_key, \"*\", \"*\"),\n                embedding_model.CHECKPOINT_FILENAME + \"-*\"))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB37460004(self):\n    code = textwrap.dedent(\"\"\"\\\n        assert all(s not in (_SENTINEL, None) for s in nested_schemas\n                  ), 'Nested schemas should never contain None/_SENTINEL'\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB36806207(self):\n    code = textwrap.dedent(\"\"\"\\\n        def _():\n          linearity_data = [[row] for row in [\n              \"%.1f mm\" % (np.mean(linearity_values[\"pos_error\"]) * 1000.0),\n              \"%.1f mm\" % (np.max(linearity_values[\"pos_error\"]) * 1000.0),\n              \"%.1f mm\" % (np.mean(linearity_values[\"pos_error_chunk_mean\"]) * 1000.0),\n              \"%.1f mm\" % (np.max(linearity_values[\"pos_error_chunk_max\"]) * 1000.0),\n              \"%.1f deg\" % math.degrees(np.mean(linearity_values[\"rot_noise\"])),\n              \"%.1f deg\" % math.degrees(np.max(linearity_values[\"rot_noise\"])),\n              \"%.1f deg\" % math.degrees(np.mean(linearity_values[\"rot_drift\"])),\n              \"%.1f deg\" % math.degrees(np.max(linearity_values[\"rot_drift\"])),\n              \"%.1f%%\" % (np.max(linearity_values[\"pos_discontinuity\"]) * 100.0),\n              \"%.1f%%\" % (np.max(linearity_values[\"rot_discontinuity\"]) * 100.0)\n          ]]\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB36215507(self):\n    code = textwrap.dedent(\"\"\"\\\n        class X():\n\n          def _():\n            aaaaaaaaaaaaa._bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n                mmmmmmmmmmmmm, nnnnn, ooooooooo,\n                _(ppppppppppppppppppppppppppppppppppppp),\n                *(qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq),\n                **(qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB35212469(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          X = {\n            'retain': {\n                'loadtest':  # This is a comment in the middle of a dictionary entry\n                    ('/some/path/to/a/file/that/is/needed/by/this/process')\n              }\n          }\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          X = {\n              'retain': {\n                  'loadtest':  # This is a comment in the middle of a dictionary entry\n                      ('/some/path/to/a/file/that/is/needed/by/this/process')\n              }\n          }\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB31063453(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          while ((not mpede_proc) or ((time_time() - last_modified) < FLAGS_boot_idle_timeout)):\n            pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          while ((not mpede_proc) or\n                 ((time_time() - last_modified) < FLAGS_boot_idle_timeout)):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB35021894(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          labelacl = Env(qa={\n              'read': 'name/some-type-of-very-long-name-for-reading-perms',\n              'modify': 'name/some-other-type-of-very-long-name-for-modifying'\n          },\n                         prod={\n                            'read': 'name/some-type-of-very-long-name-for-reading-perms',\n                            'modify': 'name/some-other-type-of-very-long-name-for-modifying'\n                         })\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          labelacl = Env(\n              qa={\n                  'read': 'name/some-type-of-very-long-name-for-reading-perms',\n                  'modify': 'name/some-other-type-of-very-long-name-for-modifying'\n              },\n              prod={\n                  'read': 'name/some-type-of-very-long-name-for-reading-perms',\n                  'modify': 'name/some-other-type-of-very-long-name-for-modifying'\n              })\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB34682902(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        logging.info(\"Mean angular velocity norm: %.3f\", np.linalg.norm(np.mean(ang_vel_arr, axis=0)))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        logging.info(\"Mean angular velocity norm: %.3f\",\n                     np.linalg.norm(np.mean(ang_vel_arr, axis=0)))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB33842726(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n          def _():\n            hints.append(('hg tag -f -l -r %s %s # %s' % (short(ctx.node(\n            )), candidatetag, firstline))[:78])\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n          def _():\n            hints.append(('hg tag -f -l -r %s %s # %s' %\n                          (short(ctx.node()), candidatetag, firstline))[:78])\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB32931780(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        environments = {\n            'prod': {\n                # this is a comment before the first entry.\n                'entry one':\n                    'an entry.',\n                # this is the comment before the second entry.\n                'entry number 2.':\n                    'something',\n                # this is the comment before the third entry and it's a doozy. So big!\n                'who':\n                    'allin',\n                # This is an entry that has a dictionary in it. It's ugly\n                'something': {\n                    'page': ['this-is-a-page@xxxxxxxx.com', 'something-for-eml@xxxxxx.com'],\n                    'bug': ['bugs-go-here5300@xxxxxx.com'],\n                    'email': ['sometypeof-email@xxxxxx.com'],\n                },\n                # a short comment\n                'yolo!!!!!':\n                    'another-email-address@xxxxxx.com',\n                # this entry has an implicit string concatenation\n                'implicit':\n                    'https://this-is-very-long.url-addr.com/'\n                    '?something=something%20some%20more%20stuff..',\n                # A more normal entry.\n                '.....':\n                    'this is an entry',\n            }\n        }\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        environments = {\n            'prod': {\n                # this is a comment before the first entry.\n                'entry one': 'an entry.',\n                # this is the comment before the second entry.\n                'entry number 2.': 'something',\n                # this is the comment before the third entry and it's a doozy. So big!\n                'who': 'allin',\n                # This is an entry that has a dictionary in it. It's ugly\n                'something': {\n                    'page': [\n                        'this-is-a-page@xxxxxxxx.com', 'something-for-eml@xxxxxx.com'\n                    ],\n                    'bug': ['bugs-go-here5300@xxxxxx.com'],\n                    'email': ['sometypeof-email@xxxxxx.com'],\n                },\n                # a short comment\n                'yolo!!!!!': 'another-email-address@xxxxxx.com',\n                # this entry has an implicit string concatenation\n                'implicit': 'https://this-is-very-long.url-addr.com/'\n                            '?something=something%20some%20more%20stuff..',\n                # A more normal entry.\n                '.....': 'this is an entry',\n            }\n        }\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB33047408(self):\n    code = textwrap.dedent(\"\"\"\\\n        def _():\n          for sort in (sorts or []):\n            request['sorts'].append({\n                'field': {\n                    'user_field': sort\n                },\n                'order': 'ASCENDING'\n            })\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB32714745(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def _BlankDefinition():\n            '''Return a generic blank dictionary for a new field.'''\n            return {\n                'type': '',\n                'validation': '',\n                'name': 'fieldname',\n                'label': 'Field Label',\n                'help': '',\n                'initial': '',\n                'required': False,\n                'required_msg': 'Required',\n                'invalid_msg': 'Please enter a valid value',\n                'options': {\n                    'regex': '',\n                    'widget_attr': '',\n                    'choices_checked': '',\n                    'choices_count': '',\n                    'choices': {}\n                },\n                'isnew': True,\n                'dirty': False,\n            }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB32737279(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        here_is_a_dict = {\n            'key':\n            # Comment.\n            'value'\n        }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        here_is_a_dict = {\n            'key':  # Comment.\n                'value'\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB32570937(self):\n    code = textwrap.dedent(\"\"\"\\\n      def _():\n        if (job_message.ball not in ('*', ball) or\n            job_message.call not in ('*', call) or\n            job_message.mall not in ('*', job_name)):\n          return False\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB31937033(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def __init__(self, metric, fields_cb=None):\n            self._fields_cb = fields_cb or (lambda *unused_args, **unused_kwargs: {})\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB31911533(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          @parameterized.NamedParameters(\n              ('IncludingModInfoWithHeaderList', AAAA, aaaa),\n              ('IncludingModInfoWithoutHeaderList', BBBB, bbbbb),\n              ('ExcludingModInfoWithHeaderList', CCCCC, cccc),\n              ('ExcludingModInfoWithoutHeaderList', DDDDD, ddddd),\n          )\n          def _():\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB31847238(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def aaaaa(self, bbbbb, cccccccccccccc=None):  # TODO(who): pylint: disable=unused-argument\n            return 1\n\n          def xxxxx(self, yyyyy, zzzzzzzzzzzzzz=None):  # A normal comment that runs over the column limit.\n            return 1\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def aaaaa(self, bbbbb, cccccccccccccc=None):  # TODO(who): pylint: disable=unused-argument\n            return 1\n\n          def xxxxx(\n              self,\n              yyyyy,\n              zzzzzzzzzzzzzz=None):  # A normal comment that runs over the column limit.\n            return 1\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB30760569(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        {'1234567890123456789012345678901234567890123456789012345678901234567890':\n             '1234567890123456789012345678901234567890'}\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        {\n            '1234567890123456789012345678901234567890123456789012345678901234567890':\n                '1234567890123456789012345678901234567890'\n        }\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB26034238(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class Thing:\n\n          def Function(self):\n            thing.Scrape('/aaaaaaaaa/bbbbbbbbbb/ccccc/dddd/eeeeeeeeeeeeee/ffffffffffffff').AndReturn(42)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class Thing:\n\n          def Function(self):\n            thing.Scrape(\n                '/aaaaaaaaa/bbbbbbbbbb/ccccc/dddd/eeeeeeeeeeeeee/ffffffffffffff'\n            ).AndReturn(42)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB30536435(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def main(unused_argv):\n          if True:\n            if True:\n              aaaaaaaaaaa.comment('import-from[{}] {} {}'.format(\n                  bbbbbbbbb.usage,\n                  ccccccccc.within,\n                  imports.ddddddddddddddddddd(name_item.ffffffffffffffff)))\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def main(unused_argv):\n          if True:\n            if True:\n              aaaaaaaaaaa.comment('import-from[{}] {} {}'.format(\n                  bbbbbbbbb.usage, ccccccccc.within,\n                  imports.ddddddddddddddddddd(name_item.ffffffffffffffff)))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB30442148(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def lulz():\n          return (some_long_module_name.SomeLongClassName.\n                  some_long_attribute_name.some_long_method_name())\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def lulz():\n          return (some_long_module_name.SomeLongClassName.some_long_attribute_name\n                  .some_long_method_name())\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB26868213(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n      def _():\n        xxxxxxxxxxxxxxxxxxx = {\n            'ssssss': {'ddddd': 'qqqqq',\n                       'p90': aaaaaaaaaaaaaaaaa,\n                       'p99': bbbbbbbbbbbbbbbbb,\n                       'lllllllllllll': yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy(),},\n            'bbbbbbbbbbbbbbbbbbbbbbbbbbbb': {\n                'ddddd': 'bork bork bork bo',\n                'p90': wwwwwwwwwwwwwwwww,\n                'p99': wwwwwwwwwwwwwwwww,\n                'lllllllllllll': None,  # use the default\n            }\n        }\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n      def _():\n        xxxxxxxxxxxxxxxxxxx = {\n            'ssssss': {\n                'ddddd': 'qqqqq',\n                'p90': aaaaaaaaaaaaaaaaa,\n                'p99': bbbbbbbbbbbbbbbbb,\n                'lllllllllllll': yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy(),\n            },\n            'bbbbbbbbbbbbbbbbbbbbbbbbbbbb': {\n                'ddddd': 'bork bork bork bo',\n                'p90': wwwwwwwwwwwwwwwww,\n                'p99': wwwwwwwwwwwwwwwww,\n                'lllllllllllll': None,  # use the default\n            }\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB30173198(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def _():\n            self.assertFalse(\n                evaluation_runner.get_larps_in_eval_set('these_arent_the_larps'))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB29908765(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n\n          def __repr__(self):\n            return '<session %s on %s>' % (\n                self._id, self._stub._stub.rpc_channel().target())  # pylint:disable=protected-access\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB30087362(self):\n    code = textwrap.dedent(\"\"\"\\\n        def _():\n          for s in sorted(env['foo']):\n            bar()\n            # This is a comment\n\n          # This is another comment\n          foo()\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB30087363(self):\n    code = textwrap.dedent(\"\"\"\\\n        if False:\n          bar()\n          # This is a comment\n        # This is another comment\n        elif True:\n          foo()\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB29093579(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          _xxxxxxxxxxxxxxx(aaaaaaaa, bbbbbbbbbbbbbb.cccccccccc[\n              dddddddddddddddddddddddddddd.eeeeeeeeeeeeeeeeeeeeee.fffffffffffffffffffff])\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          _xxxxxxxxxxxxxxx(\n              aaaaaaaa,\n              bbbbbbbbbbbbbb.cccccccccc[dddddddddddddddddddddddddddd\n                                        .eeeeeeeeeeeeeeeeeeeeee.fffffffffffffffffffff])\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB26382315(self):\n    code = textwrap.dedent(\"\"\"\\\n        @hello_world\n        # This is a first comment\n\n        # Comment\n        def foo():\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB27616132(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          query.fetch_page.assert_has_calls([\n              mock.call(100,\n                        start_cursor=None),\n              mock.call(100,\n                        start_cursor=cursor_1),\n              mock.call(100,\n                        start_cursor=cursor_2),\n          ])\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          query.fetch_page.assert_has_calls([\n              mock.call(100, start_cursor=None),\n              mock.call(100, start_cursor=cursor_1),\n              mock.call(100, start_cursor=cursor_2),\n          ])\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB27590179(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          if True:\n            self.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = (\n                { True:\n                     self.bbb.cccccccccc(ddddddddddddddddddddddd.eeeeeeeeeeeeeeeeeeeeee),\n                 False:\n                     self.bbb.cccccccccc(ddddddddddddddddddddddd.eeeeeeeeeeeeeeeeeeeeee)\n                })\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          if True:\n            self.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = ({\n                True:\n                    self.bbb.cccccccccc(ddddddddddddddddddddddd.eeeeeeeeeeeeeeeeeeeeee),\n                False:\n                    self.bbb.cccccccccc(ddddddddddddddddddddddd.eeeeeeeeeeeeeeeeeeeeee)\n            })\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB27266946(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = (self.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccccccccccccccccccc)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = (\n              self.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n              .cccccccccccccccccccccccccccccccccccc)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB25505359(self):\n    code = textwrap.dedent(\"\"\"\\\n        _EXAMPLE = {\n            'aaaaaaaaaaaaaa': [{\n                'bbbb': 'cccccccccccccccccccccc',\n                'dddddddddddd': []\n            }, {\n                'bbbb': 'ccccccccccccccccccc',\n                'dddddddddddd': []\n            }]\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB25324261(self):\n    code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaa = set(bbbb.cccc\n                        for ddd in eeeeee.fffffffffff.gggggggggggggggg\n                        for cccc in ddd.specification)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB25136704(self):\n    code = textwrap.dedent(\"\"\"\\\n        class f:\n\n          def test(self):\n            self.bbbbbbb[0]['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', {\n                'xxxxxx': 'yyyyyy'\n            }] = cccccc.ddd('1m', '10x1+1')\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB25165602(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n          ids = {u: i for u, i in zip(self.aaaaa, xrange(42, 42 + len(self.aaaaaa)))}\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB25157123(self):\n    code = textwrap.dedent(\"\"\"\\\n        def ListArgs():\n          FairlyLongMethodName([relatively_long_identifier_for_a_list],\n                               another_argument_with_a_long_identifier)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB25136820(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          return collections.OrderedDict({\n              # Preceding comment.\n              'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa':\n              '$bbbbbbbbbbbbbbbbbbbbbbbb',\n          })\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          return collections.OrderedDict({\n              # Preceding comment.\n              'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa':\n                  '$bbbbbbbbbbbbbbbbbbbbbbbb',\n          })\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB25131481(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        APPARENT_ACTIONS = ('command_type', {\n            'materialize': lambda x: some_type_of_function('materialize ' + x.command_def),\n            '#': lambda x: x  # do nothing\n        })\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        APPARENT_ACTIONS = (\n            'command_type',\n            {\n                'materialize':\n                    lambda x: some_type_of_function('materialize ' + x.command_def),\n                '#':\n                    lambda x: x  # do nothing\n            })\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB23445244(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          if True:\n            return xxxxxxxxxxxxxxxx(\n                command,\n                extra_env={\n                    \"OOOOOOOOOOOOOOOOOOOOO\": FLAGS.zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,\n                    \"PPPPPPPPPPPPPPPPPPPPP\":\n                        FLAGS.aaaaaaaaaaaaaa + FLAGS.bbbbbbbbbbbbbbbbbbb,\n                })\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          if True:\n            return xxxxxxxxxxxxxxxx(\n                command,\n                extra_env={\n                    \"OOOOOOOOOOOOOOOOOOOOO\":\n                        FLAGS.zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,\n                    \"PPPPPPPPPPPPPPPPPPPPP\":\n                        FLAGS.aaaaaaaaaaaaaa + FLAGS.bbbbbbbbbbbbbbbbbbb,\n                })\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB20559654(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n      class A(object):\n\n        def foo(self):\n          unused_error, result = server.Query(\n              ['AA BBBB CCC DDD EEEEEEEE X YY ZZZZ FFF EEE AAAAAAAA'],\n              aaaaaaaaaaa=True, bbbbbbbb=None)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n      class A(object):\n\n        def foo(self):\n          unused_error, result = server.Query(\n              ['AA BBBB CCC DDD EEEEEEEE X YY ZZZZ FFF EEE AAAAAAAA'],\n              aaaaaaaaaaa=True,\n              bbbbbbbb=None)\n        \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB23943842(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class F():\n          def f():\n            self.assertDictEqual(\n                accounts, {\n                    'foo':\n                    {'account': 'foo',\n                     'lines': 'l1\\\\nl2\\\\nl3\\\\n1 line(s) were elided.'},\n                    'bar': {'account': 'bar',\n                            'lines': 'l5\\\\nl6\\\\nl7'},\n                    'wiz': {'account': 'wiz',\n                            'lines': 'l8'}\n                })\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class F():\n\n          def f():\n            self.assertDictEqual(\n                accounts, {\n                    'foo': {\n                        'account': 'foo',\n                        'lines': 'l1\\\\nl2\\\\nl3\\\\n1 line(s) were elided.'\n                    },\n                    'bar': {\n                        'account': 'bar',\n                        'lines': 'l5\\\\nl6\\\\nl7'\n                    },\n                    'wiz': {\n                        'account': 'wiz',\n                        'lines': 'l8'\n                    }\n                })\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB20551180(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          if True:\n            return (struct.pack('aaaa', bbbbbbbbbb, ccccccccccccccc, dddddddd) + eeeeeee)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          if True:\n            return (struct.pack('aaaa', bbbbbbbbbb, ccccccccccccccc, dddddddd) +\n                    eeeeeee)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB23944849(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class A(object):\n          def xxxxxxxxx(self, aaaaaaa, bbbbbbb=ccccccccccc, dddddd=300, eeeeeeeeeeeeee=None, fffffffffffffff=0):\n            pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class A(object):\n\n          def xxxxxxxxx(self,\n                        aaaaaaa,\n                        bbbbbbb=ccccccccccc,\n                        dddddd=300,\n                        eeeeeeeeeeeeee=None,\n                        fffffffffffffff=0):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB23935890(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class F():\n          def functioni(self, aaaaaaa, bbbbbbb, cccccc, dddddddddddddd, eeeeeeeeeeeeeee):\n            pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class F():\n\n          def functioni(self, aaaaaaa, bbbbbbb, cccccc, dddddddddddddd,\n                        eeeeeeeeeeeeeee):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB28414371(self):\n    code = textwrap.dedent(\"\"\"\\\n        def _():\n          return ((m.fffff(\n              m.rrr('mmmmmmmmmmmmmmmm', 'ssssssssssssssssssssssssss'), ffffffffffffffff)\n                   | m.wwwwww(m.ddddd('1h'))\n                   | m.ggggggg(bbbbbbbbbbbbbbb)\n                   | m.ppppp(\n                       (1 - m.ffffffffffffffff(llllllllllllllllllllll * 1000000, m.vvv))\n                       * m.ddddddddddddddddd(m.vvv)),\n                   m.fffff(\n                       m.rrr('mmmmmmmmmmmmmmmm', 'sssssssssssssssssssssss'),\n                       dict(\n                           ffffffffffffffff, **{\n                               'mmmmmm:ssssss':\n                                   m.rrrrrrrrrrr('|'.join(iiiiiiiiiiiiii), iiiiii=True)\n                           }))\n                   | m.wwwwww(m.rrrr('1h'))\n                   | m.ggggggg(bbbbbbbbbbbbbbb))\n                  | m.jjjj()\n                  | m.ppppp(m.vvv[0] + m.vvv[1]))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB20127686(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n          if True:\n            return ((m.fffff(\n                m.rrr('xxxxxxxxxxxxxxxx',\n                      'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'),\n                mmmmmmmm)\n                     | m.wwwwww(m.rrrr(self.tttttttttt, self.mmmmmmmmmmmmmmmmmmmmm))\n                     | m.ggggggg(self.gggggggg, m.sss()), m.fffff('aaaaaaaaaaaaaaaa')\n                     | m.wwwwww(m.ddddd(self.tttttttttt, self.mmmmmmmmmmmmmmmmmmmmm))\n                     | m.ggggggg(self.gggggggg))\n                    | m.jjjj()\n                    | m.ppppp(m.VAL[0] / m.VAL[1]))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB20016122(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        from a_very_long_or_indented_module_name_yada_yada import (long_argument_1,\n                                                                   long_argument_2)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        from a_very_long_or_indented_module_name_yada_yada import (\n            long_argument_1, long_argument_2)\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: pep8, split_penalty_import_names: 350}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n\n    code = textwrap.dedent(\"\"\"\\\n        class foo():\n\n          def __eq__(self, other):\n            return (isinstance(other, type(self))\n                    and self.xxxxxxxxxxx == other.xxxxxxxxxxx\n                    and self.xxxxxxxx == other.xxxxxxxx\n                    and self.aaaaaaaaaaaa == other.aaaaaaaaaaaa\n                    and self.bbbbbbbbbbb == other.bbbbbbbbbbb\n                    and self.ccccccccccccccccc == other.ccccccccccccccccc\n                    and self.ddddddddddddddddddddddd == other.ddddddddddddddddddddddd\n                    and self.eeeeeeeeeeee == other.eeeeeeeeeeee\n                    and self.ffffffffffffff == other.time_completed\n                    and self.gggggg == other.gggggg and self.hhh == other.hhh\n                    and len(self.iiiiiiii) == len(other.iiiiiiii)\n                    and all(jjjjjjj in other.iiiiiiii for jjjjjjj in self.iiiiiiii))\n    \"\"\")  # noqa\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{based_on_style: yapf, '\n                                      'split_before_logical_operator: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(code)\n      self.assertCodeEqual(code, reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testB22527411(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          if True:\n            aaaaaa.bbbbbbbbbbbbbbbbbbbb[-1].cccccccccccccc.ddd().eeeeeeee(ffffffffffffff)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          if True:\n            aaaaaa.bbbbbbbbbbbbbbbbbbbb[-1].cccccccccccccc.ddd().eeeeeeee(\n                ffffffffffffff)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB20849933(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def main(unused_argv):\n          if True:\n            aaaaaaaa = {\n                'xxx': '%s/cccccc/ddddddddddddddddddd.jar' %\n                       (eeeeee.FFFFFFFFFFFFFFFFFF),\n            }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def main(unused_argv):\n          if True:\n            aaaaaaaa = {\n                'xxx':\n                    '%s/cccccc/ddddddddddddddddddd.jar' % (eeeeee.FFFFFFFFFFFFFFFFFF),\n            }\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB20813997(self):\n    code = textwrap.dedent(\"\"\"\\\n        def myfunc_1():\n          myarray = numpy.zeros((2, 2, 2))\n          print(myarray[:, 1, :])\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB20605036(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo = {\n            'aaaa': {\n                # A comment for no particular reason.\n                'xxxxxxxx': 'bbbbbbbbb',\n                'yyyyyyyyyyyyyyyyyy': 'cccccccccccccccccccccccccccccc'\n                                      'dddddddddddddddddddddddddddddddddddddddddd',\n            }\n        }\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB20562732(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo = [\n            # Comment about first list item\n            'First item',\n            # Comment about second list item\n            'Second item',\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB20128830(self):\n    code = textwrap.dedent(\"\"\"\\\n        a = {\n            'xxxxxxxxxxxxxxxxxxxx': {\n                'aaaa':\n                    'mmmmmmm',\n                'bbbbb':\n                    'mmmmmmmmmmmmmmmmmmmmm',\n                'cccccccccc': [\n                    'nnnnnnnnnnn',\n                    'ooooooooooo',\n                    'ppppppppppp',\n                    'qqqqqqqqqqq',\n                ],\n            },\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB20073838(self):\n    code = textwrap.dedent(\"\"\"\\\n        class DummyModel(object):\n\n          def do_nothing(self, class_1_count):\n            if True:\n              class_0_count = num_votes - class_1_count\n              return ('{class_0_name}={class_0_count}, {class_1_name}={class_1_count}'\n                      .format(\n                          class_0_name=self.class_0_name,\n                          class_0_count=class_0_count,\n                          class_1_name=self.class_1_name,\n                          class_1_count=class_1_count))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB19626808(self):\n    code = textwrap.dedent(\"\"\"\\\n        if True:\n          aaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbb(\n              'ccccccccccc', ddddddddd='eeeee').fffffffff([ggggggggggggggggggggg])\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB19547210(self):\n    code = textwrap.dedent(\"\"\"\\\n        while True:\n          if True:\n            if True:\n              if True:\n                if xxxxxxxxxxxx.yyyyyyy(aa).zzzzzzz() not in (\n                    xxxxxxxxxxxx.yyyyyyyyyyyyyy.zzzzzzzz,\n                    xxxxxxxxxxxx.yyyyyyyyyyyyyy.zzzzzzzz):\n                  continue\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB19377034(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n          if (aaaaaaaaaaaaaaa.start >= aaaaaaaaaaaaaaa.end or\n              bbbbbbbbbbbbbbb.start >= bbbbbbbbbbbbbbb.end):\n            return False\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB19372573(self):\n    code = textwrap.dedent(\"\"\"\\\n        def f():\n            if a: return 42\n            while True:\n                if b: continue\n                if c: break\n            return 0\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n\n      llines = yapf_test_helper.ParseAndUnwrap(code)\n      self.assertCodeEqual(code, reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def testB19353268(self):\n    code = textwrap.dedent(\"\"\"\\\n        a = {1, 2, 3}[x]\n        b = {'foo': 42, 'bar': 37}['foo']\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB19287512(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class Foo(object):\n\n          def bar(self):\n            with xxxxxxxxxx.yyyyy(\n                'aaaaaaa.bbbbbbbb.ccccccc.dddddddddddddddddddd.eeeeeeeeeee',\n                fffffffffff=(aaaaaaa.bbbbbbbb.ccccccc.dddddddddddddddddddd\n                             .Mmmmmmmmmmmmmmmmmm(-1, 'permission error'))):\n              self.assertRaises(nnnnnnnnnnnnnnnn.ooooo, ppppp.qqqqqqqqqqqqqqqqq)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class Foo(object):\n\n          def bar(self):\n            with xxxxxxxxxx.yyyyy(\n                'aaaaaaa.bbbbbbbb.ccccccc.dddddddddddddddddddd.eeeeeeeeeee',\n                fffffffffff=(\n                    aaaaaaa.bbbbbbbb.ccccccc.dddddddddddddddddddd.Mmmmmmmmmmmmmmmmmm(\n                        -1, 'permission error'))):\n              self.assertRaises(nnnnnnnnnnnnnnnn.ooooo, ppppp.qqqqqqqqqqqqqqqqq)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB19194420(self):\n    code = textwrap.dedent(\"\"\"\\\n        method.Set(\n            'long argument goes here that causes the line to break',\n            lambda arg2=0.5: arg2)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB19073499(self):\n    code = textwrap.dedent(\"\"\"\\\n        instance = (\n            aaaaaaa.bbbbbbb().ccccccccccccccccc().ddddddddddd({\n                'aa': 'context!'\n            }).eeeeeeeeeeeeeeeeeee({  # Inline comment about why fnord has the value 6.\n                'fnord': 6\n            }))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB18257115(self):\n    code = textwrap.dedent(\"\"\"\\\n        if True:\n          if True:\n            self._Test(aaaa, bbbbbbb.cccccccccc, dddddddd, eeeeeeeeeee,\n                       [ffff, ggggggggggg, hhhhhhhhhhhh, iiiiii, jjjj])\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB18256666(self):\n    code = textwrap.dedent(\"\"\"\\\n        class Foo(object):\n\n          def Bar(self):\n            aaaaa.bbbbbbb(\n                ccc='ddddddddddddddd',\n                eeee='ffffffffffffffffffffff-%s-%s' % (gggg, int(time.time())),\n                hhhhhh={\n                    'iiiiiiiiiii': iiiiiiiiiii,\n                    'jjjj': jjjj.jjjjj(),\n                    'kkkkkkkkkkkk': kkkkkkkkkkkk,\n                },\n                llllllllll=mmmmmm.nnnnnnnnnnnnnnnn)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB18256826(self):\n    code = textwrap.dedent(\"\"\"\\\n        if True:\n          pass\n        # A multiline comment.\n        # Line two.\n        elif False:\n          pass\n\n        if True:\n          pass\n          # A multiline comment.\n          # Line two.\n        elif False:\n          pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB18255697(self):\n    code = textwrap.dedent(\"\"\"\\\n        AAAAAAAAAAAAAAA = {\n            'XXXXXXXXXXXXXX': 4242,  # Inline comment\n            # Next comment\n            'YYYYYYYYYYYYYYYY': ['zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'],\n        }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testB17534869(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          self.assertLess(abs(time.time()-aaaa.bbbbbbbbbbb(\n                              datetime.datetime.now())), 1)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          self.assertLess(\n              abs(time.time() - aaaa.bbbbbbbbbbb(datetime.datetime.now())), 1)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB17489866(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          if True:\n            if True:\n              return aaaa.bbbbbbbbb(ccccccc=dddddddddddddd({('eeee', 'ffffffff'): str(j)}))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          if True:\n            if True:\n              return aaaa.bbbbbbbbb(\n                  ccccccc=dddddddddddddd({('eeee', 'ffffffff'): str(j)}))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB17133019(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class aaaaaaaaaaaaaa(object):\n\n          def bbbbbbbbbb(self):\n            with io.open(\"/dev/null\", \"rb\"):\n              with io.open(os.path.join(aaaaa.bbbbb.ccccccccccc,\n                                        DDDDDDDDDDDDDDD,\n                                        \"eeeeeeeee ffffffffff\"\n                                       ), \"rb\") as gggggggggggggggggggg:\n                print(gggggggggggggggggggg)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class aaaaaaaaaaaaaa(object):\n\n          def bbbbbbbbbb(self):\n            with io.open(\"/dev/null\", \"rb\"):\n              with io.open(\n                  os.path.join(aaaaa.bbbbb.ccccccccccc, DDDDDDDDDDDDDDD,\n                               \"eeeeeeeee ffffffffff\"), \"rb\") as gggggggggggggggggggg:\n                print(gggggggggggggggggggg)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB17011869(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        '''blah......'''\n\n        class SomeClass(object):\n          '''blah.'''\n\n          AAAAAAAAAAAA = {                        # Comment.\n              'BBB': 1.0,\n                'DDDDDDDD': 0.4811\n                                      }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        '''blah......'''\n\n\n        class SomeClass(object):\n          '''blah.'''\n\n          AAAAAAAAAAAA = {  # Comment.\n              'BBB': 1.0,\n              'DDDDDDDD': 0.4811\n          }\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB16783631(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          with aaaaaaaaaaaaaa.bbbbbbbbbbbbb.ccccccc(ddddddddddddd,\n                                                      eeeeeeeee=self.fffffffffffff\n                                                      )as gggg:\n            pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          with aaaaaaaaaaaaaa.bbbbbbbbbbbbb.ccccccc(\n              ddddddddddddd, eeeeeeeee=self.fffffffffffff) as gggg:\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB16572361(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo(self):\n         def bar(my_dict_name):\n          self.my_dict_name['foo-bar-baz-biz-boo-baa-baa'].IncrementBy.assert_called_once_with('foo_bar_baz_boo')\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo(self):\n\n          def bar(my_dict_name):\n            self.my_dict_name[\n                'foo-bar-baz-biz-boo-baa-baa'].IncrementBy.assert_called_once_with(\n                    'foo_bar_baz_boo')\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB15884241(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if 1:\n          if 1:\n            for row in AAAA:\n              self.create(aaaaaaaa=\"/aaa/bbbb/cccc/dddddd/eeeeeeeeeeeeeeeeeeeeeeeeee/%s\" % row [0].replace(\".foo\", \".bar\"), aaaaa=bbb[1], ccccc=bbb[2], dddd=bbb[3], eeeeeeeeeee=[s.strip() for s in bbb[4].split(\",\")], ffffffff=[s.strip() for s in bbb[5].split(\",\")], gggggg=bbb[6])\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if 1:\n          if 1:\n            for row in AAAA:\n              self.create(\n                  aaaaaaaa=\"/aaa/bbbb/cccc/dddddd/eeeeeeeeeeeeeeeeeeeeeeeeee/%s\" %\n                  row[0].replace(\".foo\", \".bar\"),\n                  aaaaa=bbb[1],\n                  ccccc=bbb[2],\n                  dddd=bbb[3],\n                  eeeeeeeeeee=[s.strip() for s in bbb[4].split(\",\")],\n                  ffffffff=[s.strip() for s in bbb[5].split(\",\")],\n                  gggggg=bbb[6])\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB15697268(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def main(unused_argv):\n          ARBITRARY_CONSTANT_A = 10\n          an_array_with_an_exceedingly_long_name = range(ARBITRARY_CONSTANT_A + 1)\n          ok = an_array_with_an_exceedingly_long_name[:ARBITRARY_CONSTANT_A]\n          bad_slice = map(math.sqrt, an_array_with_an_exceedingly_long_name[:ARBITRARY_CONSTANT_A])\n          a_long_name_slicing = an_array_with_an_exceedingly_long_name[:ARBITRARY_CONSTANT_A]\n          bad_slice = (\"I am a crazy, no good, string what's too long, etc.\" + \" no really \")[:ARBITRARY_CONSTANT_A]\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def main(unused_argv):\n          ARBITRARY_CONSTANT_A = 10\n          an_array_with_an_exceedingly_long_name = range(ARBITRARY_CONSTANT_A + 1)\n          ok = an_array_with_an_exceedingly_long_name[:ARBITRARY_CONSTANT_A]\n          bad_slice = map(math.sqrt,\n                          an_array_with_an_exceedingly_long_name[:ARBITRARY_CONSTANT_A])\n          a_long_name_slicing = an_array_with_an_exceedingly_long_name[:\n                                                                       ARBITRARY_CONSTANT_A]\n          bad_slice = (\"I am a crazy, no good, string what's too long, etc.\" +\n                       \" no really \")[:ARBITRARY_CONSTANT_A]\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB15597568(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          if True:\n            if True:\n              print((\"Return code was %d\" + (\", and the process timed out.\" if did_time_out else \".\")) % errorcode)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          if True:\n            if True:\n              print((\"Return code was %d\" +\n                     (\", and the process timed out.\" if did_time_out else \".\")) %\n                    errorcode)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB15542157(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaaaa = bbbb.ccccccccccccccc(dddddd.eeeeeeeeeeeeee, ffffffffffffffffff, gggggg.hhhhhhhhhhhhhhhhh)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaaaa = bbbb.ccccccccccccccc(dddddd.eeeeeeeeeeeeee, ffffffffffffffffff,\n                                            gggggg.hhhhhhhhhhhhhhhhh)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB15438132(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if aaaaaaa.bbbbbbbbbb:\n           cccccc.dddddddddd(eeeeeeeeeee=fffffffffffff.gggggggggggggggggg)\n           if hhhhhh.iiiii.jjjjjjjjjjjjj:\n             # This is a comment in the middle of it all.\n             kkkkkkk.llllllllll.mmmmmmmmmmmmm = True\n           if (aaaaaa.bbbbb.ccccccccccccc != ddddddd.eeeeeeeeee.fffffffffffff or\n               eeeeee.fffff.ggggggggggggggggggggggggggg() != hhhhhhh.iiiiiiiiii.jjjjjjjjjjjj):\n             aaaaaaaa.bbbbbbbbbbbb(\n                 aaaaaa.bbbbb.cc,\n                 dddddddddddd=eeeeeeeeeeeeeeeeeee.fffffffffffffffff(\n                     gggggg.hh,\n                     iiiiiiiiiiiiiiiiiii.jjjjjjjjjj.kkkkkkk,\n                     lllll.mm),\n                 nnnnnnnnnn=ooooooo.pppppppppp)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if aaaaaaa.bbbbbbbbbb:\n          cccccc.dddddddddd(eeeeeeeeeee=fffffffffffff.gggggggggggggggggg)\n          if hhhhhh.iiiii.jjjjjjjjjjjjj:\n            # This is a comment in the middle of it all.\n            kkkkkkk.llllllllll.mmmmmmmmmmmmm = True\n          if (aaaaaa.bbbbb.ccccccccccccc != ddddddd.eeeeeeeeee.fffffffffffff or\n              eeeeee.fffff.ggggggggggggggggggggggggggg()\n              != hhhhhhh.iiiiiiiiii.jjjjjjjjjjjj):\n            aaaaaaaa.bbbbbbbbbbbb(\n                aaaaaa.bbbbb.cc,\n                dddddddddddd=eeeeeeeeeeeeeeeeeee.fffffffffffffffff(\n                    gggggg.hh, iiiiiiiiiiiiiiiiiii.jjjjjjjjjj.kkkkkkk, lllll.mm),\n                nnnnnnnnnn=ooooooo.pppppppppp)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB14468247(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        call(a=1,\n            b=2,\n        )\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        call(\n            a=1,\n            b=2,\n        )\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB14406499(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo1(parameter_1, parameter_2, parameter_3, parameter_4, \\\nparameter_5, parameter_6): pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo1(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5,\n                 parameter_6):\n          pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB13900309(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        self.aaaaaaaaaaa(  # A comment in the middle of it all.\n               948.0/3600, self.bbb.ccccccccccccccccccccc(dddddddddddddddd.eeee, True))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        self.aaaaaaaaaaa(  # A comment in the middle of it all.\n            948.0 / 3600, self.bbb.ccccccccccccccccccccc(dddddddddddddddd.eeee, True))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccccccccccccc(\n            DC_1, (CL - 50, CL), AAAAAAAA, BBBBBBBBBBBBBBBB, 98.0,\n            CCCCCCC).ddddddddd(  # Look! A comment is here.\n                AAAAAAAA - (20 * 60 - 5))\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbb.ccccccccccccccccccccccccc().dddddddddddddddddddddddddd(1, 2, 3, 4)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbb.ccccccccccccccccccccccccc(\n        ).dddddddddddddddddddddddddd(1, 2, 3, 4)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbb.ccccccccccccccccccccccccc(x).dddddddddddddddddddddddddd(1, 2, 3, 4)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbb.ccccccccccccccccccccccccc(\n            x).dddddddddddddddddddddddddd(1, 2, 3, 4)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaaaaaaaaaaaaaaaa(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx).dddddddddddddddddddddddddd(1, 2, 3, 4)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaaaaaaaaaaaaaaaa(\n            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx).dddddddddddddddddddddddddd(1, 2, 3, 4)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaaaaaaaaaaaaaaaa().bbbbbbbbbbbbbbbbbbbbbbbb().ccccccccccccccccccc().\\\ndddddddddddddddddd().eeeeeeeeeeeeeeeeeeeee().fffffffffffffffff().gggggggggggggggggg()\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        aaaaaaaaaaaaaaaaaaaaaaaa().bbbbbbbbbbbbbbbbbbbbbbbb().ccccccccccccccccccc(\n        ).dddddddddddddddddd().eeeeeeeeeeeeeeeeeeeee().fffffffffffffffff(\n        ).gggggggggggggggggg()\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testB67935687(self):\n    code = textwrap.dedent(\"\"\"\\\n        Fetch(\n            Raw('monarch.BorgTask', '/union/row_operator_action_delay'),\n            {'borg_user': self.borg_user})\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        shelf_renderer.expand_text = text.translate_to_unicode(\n            expand_text % {\n                'creator': creator\n            })\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        shelf_renderer.expand_text = text.translate_to_unicode(expand_text %\n                                                               {'creator': creator})\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/reformatter_facebook_test.py",
    "content": "# Copyright 2016 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Facebook tests for yapf.reformatter.\"\"\"\n\nimport textwrap\nimport unittest\n\nfrom yapf.yapflib import reformatter\nfrom yapf.yapflib import style\n\nfrom yapftests import yapf_test_helper\n\n\nclass TestsForFacebookStyle(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):\n    style.SetGlobalStyle(style.CreateFacebookStyle())\n\n  def testNoNeedForLineBreaks(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(\n          just_one_arg, **kwargs):\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(just_one_arg, **kwargs):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDedentClosingBracket(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(\n          first_argument_on_the_same_line,\n          second_argument_makes_the_line_too_long):\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(\n            first_argument_on_the_same_line, second_argument_makes_the_line_too_long\n        ):\n            pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testBreakAfterOpeningBracketIfContentsTooBig(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(a, b, c, d, e, f, g, h, i, j, k, l, m,\n          n, o, p, q, r, s, t, u, v, w, x, y, z):\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(\n            a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z\n        ):\n            pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDedentClosingBracketWithComments(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(\n          # comment about the first argument\n          first_argument_with_a_very_long_name_or_so,\n          # comment about the second argument\n          second_argument_makes_the_line_too_long):\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(\n            # comment about the first argument\n            first_argument_with_a_very_long_name_or_so,\n            # comment about the second argument\n            second_argument_makes_the_line_too_long\n        ):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDedentImportAsNames(self):\n    code = textwrap.dedent(\"\"\"\\\n        from module import (\n            internal_function as function,\n            SOME_CONSTANT_NUMBER1,\n            SOME_CONSTANT_NUMBER2,\n            SOME_CONSTANT_NUMBER3,\n        )\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testDedentTestListGexp(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        try:\n            pass\n        except (\n            IOError, OSError, LookupError, RuntimeError, OverflowError\n        ) as exception:\n            pass\n\n        try:\n            pass\n        except (\n            IOError, OSError, LookupError, RuntimeError, OverflowError,\n        ) as exception:\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        try:\n            pass\n        except (\n            IOError, OSError, LookupError, RuntimeError, OverflowError\n        ) as exception:\n            pass\n\n        try:\n            pass\n        except (\n            IOError,\n            OSError,\n            LookupError,\n            RuntimeError,\n            OverflowError,\n        ) as exception:\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testBrokenIdempotency(self):\n    # TODO(ambv): The following behaviour should be fixed.\n    pass0_code = textwrap.dedent(\"\"\"\\\n        try:\n            pass\n        except (IOError, OSError, LookupError, RuntimeError, OverflowError) as exception:\n            pass\n    \"\"\")  # noqa\n    pass1_code = textwrap.dedent(\"\"\"\\\n        try:\n            pass\n        except (\n            IOError, OSError, LookupError, RuntimeError, OverflowError\n        ) as exception:\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(pass0_code)\n    self.assertCodeEqual(pass1_code, reformatter.Reformat(llines))\n\n    pass2_code = textwrap.dedent(\"\"\"\\\n        try:\n            pass\n        except (\n            IOError, OSError, LookupError, RuntimeError, OverflowError\n        ) as exception:\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(pass1_code)\n    self.assertCodeEqual(pass2_code, reformatter.Reformat(llines))\n\n  def testIfExprHangingIndent(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            if True:\n                if True:\n                    if not self.frobbies and (\n                       self.foobars.counters['db.cheeses'] != 1 or\n                       self.foobars.counters['db.marshmellow_skins'] != 1):\n                        pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            if True:\n                if True:\n                    if not self.frobbies and (\n                        self.foobars.counters['db.cheeses'] != 1 or\n                        self.foobars.counters['db.marshmellow_skins'] != 1\n                    ):\n                        pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSimpleDedenting(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            self.assertEqual(result.reason_not_added, \"current preflight is still running\")\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            self.assertEqual(\n                result.reason_not_added, \"current preflight is still running\"\n            )\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDedentingWithSubscripts(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class Foo:\n            class Bar:\n                @classmethod\n                def baz(cls, clues_list, effect, constraints, constraint_manager):\n                    if clues_lists:\n                       return cls.single_constraint_not(clues_lists, effect, constraints[0], constraint_manager)\n\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class Foo:\n            class Bar:\n                @classmethod\n                def baz(cls, clues_list, effect, constraints, constraint_manager):\n                    if clues_lists:\n                        return cls.single_constraint_not(\n                            clues_lists, effect, constraints[0], constraint_manager\n                        )\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDedentingCallsWithInnerLists(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n            def _():\n                cls.effect_clues = {\n                    'effect': Clue((cls.effect_time, 'apache_host'), effect_line, 40)\n                }\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testDedentingListComprehension(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class Foo():\n            def _pack_results_for_constraint_or():\n                self.param_groups = dict(\n                    (\n                        key + 1, ParamGroup(groups[key], default_converter)\n                    ) for key in six.moves.range(len(groups))\n                )\n\n                for combination in cls._clues_combinations(clues_lists):\n                    if all(\n                        cls._verify_constraint(combination, effect, constraint)\n                        for constraint in constraints\n                    ):\n                        pass\n\n                guessed_dict = dict(\n                    (\n                        key, guessed_pattern_matches[key]\n                    ) for key in six.moves.range(len(guessed_pattern_matches))\n                )\n\n                content = \"\".join(\n                    itertools.chain(\n                        (first_line_fragment, ), lines_between, (last_line_fragment, )\n                    )\n                )\n\n                rule = Rule(\n                    [self.cause1, self.cause2, self.cause1, self.cause2], self.effect, constraints1,\n                    Rule.LINKAGE_AND\n                )\n\n                assert sorted(log_type.files_to_parse) == [\n                    ('localhost', os.path.join(path, 'node_1.log'), super_parser),\n                    ('localhost', os.path.join(path, 'node_2.log'), super_parser)\n                ]\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class Foo():\n            def _pack_results_for_constraint_or():\n                self.param_groups = dict(\n                    (key + 1, ParamGroup(groups[key], default_converter))\n                    for key in six.moves.range(len(groups))\n                )\n\n                for combination in cls._clues_combinations(clues_lists):\n                    if all(\n                        cls._verify_constraint(combination, effect, constraint)\n                        for constraint in constraints\n                    ):\n                        pass\n\n                guessed_dict = dict(\n                    (key, guessed_pattern_matches[key])\n                    for key in six.moves.range(len(guessed_pattern_matches))\n                )\n\n                content = \"\".join(\n                    itertools.chain(\n                        (first_line_fragment, ), lines_between, (last_line_fragment, )\n                    )\n                )\n\n                rule = Rule(\n                    [self.cause1, self.cause2, self.cause1, self.cause2], self.effect,\n                    constraints1, Rule.LINKAGE_AND\n                )\n\n                assert sorted(log_type.files_to_parse) == [\n                    ('localhost', os.path.join(path, 'node_1.log'), super_parser),\n                    ('localhost', os.path.join(path, 'node_2.log'), super_parser)\n                ]\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testMustSplitDedenting(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n            def _():\n                effect_line = FrontInput(\n                    effect_line_offset, line_content,\n                    LineSource('localhost', xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)\n                )\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testDedentIfConditional(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n            def _():\n                if True:\n                    if not self.frobbies and (\n                        self.foobars.counters['db.cheeses'] != 1 or\n                        self.foobars.counters['db.marshmellow_skins'] != 1\n                    ):\n                        pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testDedentSet(self):\n    code = textwrap.dedent(\"\"\"\\\n        class _():\n            def _():\n                assert set(self.constraint_links.get_links()) == set(\n                    [\n                        (2, 10, 100),\n                        (2, 10, 200),\n                        (2, 20, 100),\n                        (2, 20, 200),\n                    ]\n                )\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testDedentingInnerScope(self):\n    code = textwrap.dedent(\"\"\"\\\n        class Foo():\n            @classmethod\n            def _pack_results_for_constraint_or(cls, combination, constraints):\n                return cls._create_investigation_result(\n                    (clue for clue in combination if not clue == Verifier.UNMATCHED),\n                    constraints, InvestigationResult.OR\n                )\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    reformatted_code = reformatter.Reformat(llines)\n    self.assertCodeEqual(code, reformatted_code)\n\n    llines = yapf_test_helper.ParseAndUnwrap(reformatted_code)\n    reformatted_code = reformatter.Reformat(llines)\n    self.assertCodeEqual(code, reformatted_code)\n\n  def testCommentWithNewlinesInPrefix(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n            if 0:\n                return False\n\n\n            #a deadly comment\n            elif 1:\n                return True\n\n\n        print(foo())\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n            if 0:\n                return False\n\n            #a deadly comment\n            elif 1:\n                return True\n\n\n        print(foo())\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testIfStmtClosingBracket(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if (isinstance(value  , (StopIteration  , StopAsyncIteration  )) and exc.__cause__ is value_asdfasdfasdfasdfsafsafsafdasfasdfs):\n            return False\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if (\n            isinstance(value, (StopIteration, StopAsyncIteration)) and\n            exc.__cause__ is value_asdfasdfasdfasdfsafsafsafdasfasdfs\n        ):\n            return False\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/reformatter_pep8_test.py",
    "content": "# Copyright 2016 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"PEP8 tests for yapf.reformatter.\"\"\"\n\nimport textwrap\nimport unittest\n\nfrom yapf.yapflib import reformatter\nfrom yapf.yapflib import style\n\nfrom yapftests import yapf_test_helper\n\n\nclass TestsForPEP8Style(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):  # pylint: disable=g-missing-super-call\n    style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testIndent4(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if a+b:\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if a + b:\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSingleLineIfStatements(self):\n    code = textwrap.dedent(\"\"\"\\\n        if True: a = 42\n        elif False: b = 42\n        else: c = 42\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testBlankBetweenClassAndDef(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class Foo:\n          def joe():\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class Foo:\n\n            def joe():\n                pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testBlankBetweenDefsInClass(self):\n    unformatted_code = textwrap.dedent('''\\\n        class TestClass:\n            def __init__(self):\n                self.running = False\n            def run(self):\n                \"\"\"Override in subclass\"\"\"\n            def is_running(self):\n                return self.running\n    ''')\n    expected_formatted_code = textwrap.dedent('''\\\n        class TestClass:\n\n            def __init__(self):\n                self.running = False\n\n            def run(self):\n                \"\"\"Override in subclass\"\"\"\n\n            def is_running(self):\n                return self.running\n    ''')\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSingleWhiteBeforeTrailingComment(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if a+b: # comment\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if a + b:  # comment\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSpaceBetweenEndingCommandAndClosingBracket(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a = (\n            1,\n        )\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = (1, )\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testContinuedNonOutdentedLine(self):\n    code = textwrap.dedent(\"\"\"\\\n        class eld(d):\n            if str(geom.geom_type).upper(\n            ) != self.geom_type and not self.geom_type == 'GEOMETRY':\n                ror(code='om_type')\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testWrappingPercentExpressions(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n            if True:\n                zzzzz = '%s-%s' % (xxxxxxxxxxxxxxxxxxxxxxxxxx + 1, xxxxxxxxxxxxxxxxx.yyy + 1)\n                zzzzz = '%s-%s'.ww(xxxxxxxxxxxxxxxxxxxxxxxxxx + 1, xxxxxxxxxxxxxxxxx.yyy + 1)\n                zzzzz = '%s-%s' % (xxxxxxxxxxxxxxxxxxxxxxx + 1, xxxxxxxxxxxxxxxxxxxxx + 1)\n                zzzzz = '%s-%s'.ww(xxxxxxxxxxxxxxxxxxxxxxx + 1, xxxxxxxxxxxxxxxxxxxxx + 1)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n            if True:\n                zzzzz = '%s-%s' % (xxxxxxxxxxxxxxxxxxxxxxxxxx + 1,\n                                   xxxxxxxxxxxxxxxxx.yyy + 1)\n                zzzzz = '%s-%s'.ww(xxxxxxxxxxxxxxxxxxxxxxxxxx + 1,\n                                   xxxxxxxxxxxxxxxxx.yyy + 1)\n                zzzzz = '%s-%s' % (xxxxxxxxxxxxxxxxxxxxxxx + 1,\n                                   xxxxxxxxxxxxxxxxxxxxx + 1)\n                zzzzz = '%s-%s'.ww(xxxxxxxxxxxxxxxxxxxxxxx + 1,\n                                   xxxxxxxxxxxxxxxxxxxxx + 1)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testAlignClosingBracketWithVisualIndentation(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        TEST_LIST = ('foo', 'bar',  # first comment\n                     'baz'  # second comment\n                    )\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        TEST_LIST = (\n            'foo',\n            'bar',  # first comment\n            'baz'  # second comment\n        )\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n\n          def g():\n            while (xxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz]) == 'aaaaaaaaaaa' and\n                   xxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz].aaaaaaaa[0]) == 'bbbbbbb'\n                  ):\n              pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n\n            def g():\n                while (xxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz]) == 'aaaaaaaaaaa'\n                       and xxxxxxxxxxxxxxxxxxxx(\n                           yyyyyyyyyyyyy[zzzzz].aaaaaaaa[0]) == 'bbbbbbb'):\n                    pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testIndentSizeChanging(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          runtime_mins = (program_end_time - program_start_time).total_seconds() / 60.0\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            runtime_mins = (program_end_time -\n                            program_start_time).total_seconds() / 60.0\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testHangingIndentCollision(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if (aaaaaaaaaaaaaa + bbbbbbbbbbbbbbbb == ccccccccccccccccc and xxxxxxxxxxxxx or yyyyyyyyyyyyyyyyy):\n            pass\n        elif (xxxxxxxxxxxxxxx(aaaaaaaaaaa, bbbbbbbbbbbbbb, cccccccccccc, dddddddddd=None)):\n            pass\n\n\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n            for connection in itertools.chain(branch.contact, branch.address, morestuff.andmore.andmore.andmore.andmore.andmore.andmore.andmore):\n                dosomething(connection)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if (aaaaaaaaaaaaaa + bbbbbbbbbbbbbbbb == ccccccccccccccccc and xxxxxxxxxxxxx\n                or yyyyyyyyyyyyyyyyy):\n            pass\n        elif (xxxxxxxxxxxxxxx(aaaaaaaaaaa,\n                              bbbbbbbbbbbbbb,\n                              cccccccccccc,\n                              dddddddddd=None)):\n            pass\n\n\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and\n                    xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n            for connection in itertools.chain(\n                    branch.contact, branch.address,\n                    morestuff.andmore.andmore.andmore.andmore.andmore.andmore.andmore):\n                dosomething(connection)\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplittingBeforeLogicalOperator(self):\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: pep8, split_before_logical_operator: True}'))\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          def foo():\n              return bool(update.message.new_chat_member or update.message.left_chat_member or\n                          update.message.new_chat_title or update.message.new_chat_photo or\n                          update.message.delete_chat_photo or update.message.group_chat_created or\n                          update.message.supergroup_chat_created or update.message.channel_chat_created\n                          or update.message.migrate_to_chat_id or update.message.migrate_from_chat_id or\n                          update.message.pinned_message)\n      \"\"\")  # noqa\n      expected_formatted_code = textwrap.dedent(\"\"\"\\\n          def foo():\n              return bool(\n                  update.message.new_chat_member or update.message.left_chat_member\n                  or update.message.new_chat_title or update.message.new_chat_photo\n                  or update.message.delete_chat_photo\n                  or update.message.group_chat_created\n                  or update.message.supergroup_chat_created\n                  or update.message.channel_chat_created\n                  or update.message.migrate_to_chat_id\n                  or update.message.migrate_from_chat_id\n                  or update.message.pinned_message)\n      \"\"\")  # noqa\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testContiguousListEndingWithComment(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            if True:\n                keys.append(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)  # may be unassigned.\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            if True:\n                keys.append(\n                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)  # may be unassigned.\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplittingBeforeFirstArgument(self):\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: pep8, split_before_first_argument: True}'))\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          a_very_long_function_name(long_argument_name_1=1, long_argument_name_2=2,\n                                    long_argument_name_3=3, long_argument_name_4=4)\n      \"\"\")  # noqa\n      expected_formatted_code = textwrap.dedent(\"\"\"\\\n          a_very_long_function_name(\n              long_argument_name_1=1,\n              long_argument_name_2=2,\n              long_argument_name_3=3,\n              long_argument_name_4=4)\n      \"\"\")\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testSplittingExpressionsInsideSubscripts(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n            df = df[(df['campaign_status'] == 'LIVE') & (df['action_status'] == 'LIVE')]\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n            df = df[(df['campaign_status'] == 'LIVE')\n                    & (df['action_status'] == 'LIVE')]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplitListsAndDictSetMakersIfCommaTerminated(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        DJANGO_TEMPLATES_OPTIONS = {\"context_processors\": []}\n        DJANGO_TEMPLATES_OPTIONS = {\"context_processors\": [],}\n        x = [\"context_processors\"]\n        x = [\"context_processors\",]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        DJANGO_TEMPLATES_OPTIONS = {\"context_processors\": []}\n        DJANGO_TEMPLATES_OPTIONS = {\n            \"context_processors\": [],\n        }\n        x = [\"context_processors\"]\n        x = [\n            \"context_processors\",\n        ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplitAroundNamedAssigns(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class a():\n\n            def a(): return a(\n             aaaaaaaaaa=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class a():\n\n            def a():\n                return a(\n                    aaaaaaaaaa=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n                )\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testUnaryOperator(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if not -3 < x < 3:\n          pass\n        if -3 < x < 3:\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if not -3 < x < 3:\n            pass\n        if -3 < x < 3:\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testNoSplitBeforeDictValue(self):\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig('{based_on_style: pep8, '\n                                      'allow_split_before_dict_value: false, '\n                                      'coalesce_brackets: true, '\n                                      'dedent_closing_brackets: true, '\n                                      'each_dict_entry_on_separate_line: true, '\n                                      'split_before_logical_operator: true}'))\n\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          some_dict = {\n              'title': _(\"I am example data\"),\n              'description': _(\"Lorem ipsum dolor met sit amet elit, si vis pacem para bellum \"\n                               \"elites nihi very long string.\"),\n          }\n      \"\"\")  # noqa\n      expected_formatted_code = textwrap.dedent(\"\"\"\\\n          some_dict = {\n              'title': _(\"I am example data\"),\n              'description': _(\n                  \"Lorem ipsum dolor met sit amet elit, si vis pacem para bellum \"\n                  \"elites nihi very long string.\"\n              ),\n          }\n      \"\"\")  # noqa\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          X = {'a': 1, 'b': 2, 'key': this_is_a_function_call_that_goes_over_the_column_limit_im_pretty_sure()}\n      \"\"\")  # noqa\n      expected_formatted_code = textwrap.dedent(\"\"\"\\\n          X = {\n              'a': 1,\n              'b': 2,\n              'key': this_is_a_function_call_that_goes_over_the_column_limit_im_pretty_sure()\n          }\n      \"\"\")  # noqa\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          attrs = {\n              'category': category,\n              'role': forms.ModelChoiceField(label=_(\"Role\"), required=False, queryset=category_roles, initial=selected_role, empty_label=_(\"No access\"),),\n          }\n      \"\"\")  # noqa\n      expected_formatted_code = textwrap.dedent(\"\"\"\\\n          attrs = {\n              'category': category,\n              'role': forms.ModelChoiceField(\n                  label=_(\"Role\"),\n                  required=False,\n                  queryset=category_roles,\n                  initial=selected_role,\n                  empty_label=_(\"No access\"),\n              ),\n          }\n      \"\"\")\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          css_class = forms.CharField(\n              label=_(\"CSS class\"),\n              required=False,\n              help_text=_(\"Optional CSS class used to customize this category appearance from templates.\"),\n          )\n      \"\"\")  # noqa\n      expected_formatted_code = textwrap.dedent(\"\"\"\\\n          css_class = forms.CharField(\n              label=_(\"CSS class\"),\n              required=False,\n              help_text=_(\n                  \"Optional CSS class used to customize this category appearance from templates.\"\n              ),\n          )\n      \"\"\")  # noqa\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testBitwiseOperandSplitting(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n            include_values = np.where(\n                        (cdffile['Quality_Flag'][:] >= 5) & (\n                        cdffile['Day_Night_Flag'][:] == 1) & (\n                        cdffile['Longitude'][:] >= select_lon - radius) & (\n                        cdffile['Longitude'][:] <= select_lon + radius) & (\n                        cdffile['Latitude'][:] >= select_lat - radius) & (\n                        cdffile['Latitude'][:] <= select_lat + radius))\n    \"\"\")  # noqa\n    expected_code = textwrap.dedent(\"\"\"\\\n        def _():\n            include_values = np.where(\n                (cdffile['Quality_Flag'][:] >= 5) & (cdffile['Day_Night_Flag'][:] == 1)\n                & (cdffile['Longitude'][:] >= select_lon - radius)\n                & (cdffile['Longitude'][:] <= select_lon + radius)\n                & (cdffile['Latitude'][:] >= select_lat - radius)\n                & (cdffile['Latitude'][:] <= select_lat + radius))\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertEqual(expected_code, reformatter.Reformat(llines))\n\n  def testNoBlankLinesOnlyForFirstNestedObject(self):\n    unformatted_code = textwrap.dedent('''\\\n        class Demo:\n            \"\"\"\n            Demo docs\n            \"\"\"\n            def foo(self):\n                \"\"\"\n                foo docs\n                \"\"\"\n            def bar(self):\n                \"\"\"\n                bar docs\n                \"\"\"\n    ''')\n    expected_code = textwrap.dedent('''\\\n        class Demo:\n            \"\"\"\n            Demo docs\n            \"\"\"\n\n            def foo(self):\n                \"\"\"\n                foo docs\n                \"\"\"\n\n            def bar(self):\n                \"\"\"\n                bar docs\n                \"\"\"\n    ''')\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertEqual(expected_code, reformatter.Reformat(llines))\n\n  def testSplitBeforeArithmeticOperators(self):\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: pep8, split_before_arithmetic_operator: true}'))\n\n      unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n            raise ValueError('This is a long message that ends with an argument: ' + str(42))\n      \"\"\")  # noqa\n      expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n            raise ValueError('This is a long message that ends with an argument: '\n                             + str(42))\n      \"\"\")  # noqa\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testListSplitting(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        foo([(1,1), (1,1), (1,1), (1,1), (1,1), (1,1), (1,1),\n             (1,1), (1,1), (1,1), (1,1), (1,1), (1,1), (1,1),\n             (1,10), (1,11), (1, 10), (1,11), (10,11)])\n    \"\"\")\n    expected_code = textwrap.dedent(\"\"\"\\\n        foo([(1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1),\n             (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 10), (1, 11), (1, 10),\n             (1, 11), (10, 11)])\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_code, reformatter.Reformat(llines))\n\n  def testNoBlankLineBeforeNestedFuncOrClass(self):\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: pep8, '\n              'blank_line_before_nested_class_or_def: false}'))\n\n      unformatted_code = textwrap.dedent('''\\\n        def normal_function():\n            \"\"\"Return the nested function.\"\"\"\n\n            def nested_function():\n                \"\"\"Do nothing just nest within.\"\"\"\n\n                @nested(klass)\n                class nested_class():\n                    pass\n\n                pass\n\n            return nested_function\n      ''')\n      expected_formatted_code = textwrap.dedent('''\\\n        def normal_function():\n            \"\"\"Return the nested function.\"\"\"\n            def nested_function():\n                \"\"\"Do nothing just nest within.\"\"\"\n                @nested(klass)\n                class nested_class():\n                    pass\n\n                pass\n\n            return nested_function\n      ''')\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testParamListIndentationCollision1(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n\n            def __init__(self, title: Optional[str], diffs: Collection[BinaryDiff] = (), charset: Union[Type[AsciiCharset], Type[LineCharset]] = AsciiCharset, preprocess: Callable[[str], str] = identity,\n                    # TODO(somebody): Make this a Literal type.\n                    justify: str = 'rjust'):\n                self._cs = charset\n                self._preprocess = preprocess\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class _():\n\n            def __init__(\n                    self,\n                    title: Optional[str],\n                    diffs: Collection[BinaryDiff] = (),\n                    charset: Union[Type[AsciiCharset],\n                                   Type[LineCharset]] = AsciiCharset,\n                    preprocess: Callable[[str], str] = identity,\n                    # TODO(somebody): Make this a Literal type.\n                    justify: str = 'rjust'):\n                self._cs = charset\n                self._preprocess = preprocess\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testParamListIndentationCollision2(self):\n    code = textwrap.dedent(\"\"\"\\\n        def simple_pass_function_with_an_extremely_long_name_and_some_arguments(\n                argument0, argument1):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testParamListIndentationCollision3(self):\n    code = textwrap.dedent(\"\"\"\\\n        def func1(\n            arg1,\n            arg2,\n        ) -> None:\n            pass\n\n\n        def func2(\n            arg1,\n            arg2,\n        ):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testTwoWordComparisonOperators(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        _ = (klsdfjdklsfjksdlfjdklsfjdslkfjsdkl is not ksldfjsdklfjdklsfjdklsfjdklsfjdsklfjdklsfj)\n        _ = (klsdfjdklsfjksdlfjdklsfjdslkfjsdkl not in {ksldfjsdklfjdklsfjdklsfjdklsfjdsklfjdklsfj})\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        _ = (klsdfjdklsfjksdlfjdklsfjdslkfjsdkl\n             is not ksldfjsdklfjdklsfjdklsfjdklsfjdsklfjdklsfj)\n        _ = (klsdfjdklsfjksdlfjdklsfjdslkfjsdkl\n             not in {ksldfjsdklfjdklsfjdklsfjdklsfjdsklfjdklsfj})\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testStableInlinedDictionaryFormatting(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n            url = \"http://{0}/axis-cgi/admin/param.cgi?{1}\".format(\n                value, urllib.urlencode({'action': 'update', 'parameter': value}))\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _():\n            url = \"http://{0}/axis-cgi/admin/param.cgi?{1}\".format(\n                value, urllib.urlencode({\n                    'action': 'update',\n                    'parameter': value\n                }))\n    \"\"\")\n\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    reformatted_code = reformatter.Reformat(llines)\n    self.assertCodeEqual(expected_formatted_code, reformatted_code)\n\n    llines = yapf_test_helper.ParseAndUnwrap(reformatted_code)\n    reformatted_code = reformatter.Reformat(llines)\n    self.assertCodeEqual(expected_formatted_code, reformatted_code)\n\n\nclass TestsForSpacesInsideBrackets(yapf_test_helper.YAPFTest):\n  \"\"\"Test the SPACE_INSIDE_BRACKETS style option.\"\"\"\n  unformatted_code = textwrap.dedent(\"\"\"\\\n      foo()\n      foo(1)\n      foo(1,2)\n      foo((1,))\n      foo((1, 2))\n      foo((1, 2,))\n      foo(bar['baz'][0])\n      set1 = {1, 2, 3}\n      dict1 = {1: 1, foo: 2, 3: bar}\n      dict2 = {\n          1: 1,\n          foo: 2,\n          3: bar,\n      }\n      dict3[3][1][get_index(*args,**kwargs)]\n      dict4[3][1][get_index(**kwargs)]\n      x = dict5[4](foo(*args))\n      a = list1[:]\n      b = list2[slice_start:]\n      c = list3[slice_start:slice_end]\n      d = list4[slice_start:slice_end:]\n      e = list5[slice_start:slice_end:slice_step]\n      # Print gets special handling\n      print(set2)\n      compound = ((10+3)/(5-2**(6+x)))\n      string_idx = \"mystring\"[3]\n  \"\"\")\n\n  def testEnabled(self):\n    style.SetGlobalStyle(\n        style.CreateStyleFromConfig('{space_inside_brackets: True}'))\n\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        foo()\n        foo( 1 )\n        foo( 1, 2 )\n        foo( ( 1, ) )\n        foo( ( 1, 2 ) )\n        foo( (\n            1,\n            2,\n        ) )\n        foo( bar[ 'baz' ][ 0 ] )\n        set1 = { 1, 2, 3 }\n        dict1 = { 1: 1, foo: 2, 3: bar }\n        dict2 = {\n            1: 1,\n            foo: 2,\n            3: bar,\n        }\n        dict3[ 3 ][ 1 ][ get_index( *args, **kwargs ) ]\n        dict4[ 3 ][ 1 ][ get_index( **kwargs ) ]\n        x = dict5[ 4 ]( foo( *args ) )\n        a = list1[ : ]\n        b = list2[ slice_start: ]\n        c = list3[ slice_start:slice_end ]\n        d = list4[ slice_start:slice_end: ]\n        e = list5[ slice_start:slice_end:slice_step ]\n        # Print gets special handling\n        print( set2 )\n        compound = ( ( 10 + 3 ) / ( 5 - 2**( 6 + x ) ) )\n        string_idx = \"mystring\"[ 3 ]\n   \"\"\")\n\n    llines = yapf_test_helper.ParseAndUnwrap(self.unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDefault(self):\n    style.SetGlobalStyle(style.CreatePEP8Style())\n\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        foo()\n        foo(1)\n        foo(1, 2)\n        foo((1, ))\n        foo((1, 2))\n        foo((\n            1,\n            2,\n        ))\n        foo(bar['baz'][0])\n        set1 = {1, 2, 3}\n        dict1 = {1: 1, foo: 2, 3: bar}\n        dict2 = {\n            1: 1,\n            foo: 2,\n            3: bar,\n        }\n        dict3[3][1][get_index(*args, **kwargs)]\n        dict4[3][1][get_index(**kwargs)]\n        x = dict5[4](foo(*args))\n        a = list1[:]\n        b = list2[slice_start:]\n        c = list3[slice_start:slice_end]\n        d = list4[slice_start:slice_end:]\n        e = list5[slice_start:slice_end:slice_step]\n        # Print gets special handling\n        print(set2)\n        compound = ((10 + 3) / (5 - 2**(6 + x)))\n        string_idx = \"mystring\"[3]\n    \"\"\")\n\n    llines = yapf_test_helper.ParseAndUnwrap(self.unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n\nclass TestsForSpacesAroundSubscriptColon(yapf_test_helper.YAPFTest):\n  \"\"\"Test the SPACES_AROUND_SUBSCRIPT_COLON style option.\"\"\"\n  unformatted_code = textwrap.dedent(\"\"\"\\\n      a = list1[ : ]\n      b = list2[ slice_start: ]\n      c = list3[ slice_start:slice_end ]\n      d = list4[ slice_start:slice_end: ]\n      e = list5[ slice_start:slice_end:slice_step ]\n      a1 = list1[ : ]\n      b1 = list2[ 1: ]\n      c1 = list3[ 1:20 ]\n      d1 = list4[ 1:20: ]\n      e1 = list5[ 1:20:3 ]\n  \"\"\")\n\n  def testEnabled(self):\n    style.SetGlobalStyle(\n        style.CreateStyleFromConfig('{spaces_around_subscript_colon: True}'))\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = list1[:]\n        b = list2[slice_start :]\n        c = list3[slice_start : slice_end]\n        d = list4[slice_start : slice_end :]\n        e = list5[slice_start : slice_end : slice_step]\n        a1 = list1[:]\n        b1 = list2[1 :]\n        c1 = list3[1 : 20]\n        d1 = list4[1 : 20 :]\n        e1 = list5[1 : 20 : 3]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(self.unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testWithSpaceInsideBrackets(self):\n    style.SetGlobalStyle(\n        style.CreateStyleFromConfig('{spaces_around_subscript_colon: true, '\n                                    'space_inside_brackets: true,}'))\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = list1[ : ]\n        b = list2[ slice_start : ]\n        c = list3[ slice_start : slice_end ]\n        d = list4[ slice_start : slice_end : ]\n        e = list5[ slice_start : slice_end : slice_step ]\n        a1 = list1[ : ]\n        b1 = list2[ 1 : ]\n        c1 = list3[ 1 : 20 ]\n        d1 = list4[ 1 : 20 : ]\n        e1 = list5[ 1 : 20 : 3 ]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(self.unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testDefault(self):\n    style.SetGlobalStyle(style.CreatePEP8Style())\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = list1[:]\n        b = list2[slice_start:]\n        c = list3[slice_start:slice_end]\n        d = list4[slice_start:slice_end:]\n        e = list5[slice_start:slice_end:slice_step]\n        a1 = list1[:]\n        b1 = list2[1:]\n        c1 = list3[1:20]\n        d1 = list4[1:20:]\n        e1 = list5[1:20:3]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(self.unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/reformatter_python3_test.py",
    "content": "# Copyright 2016 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Python 3 tests for yapf.reformatter.\"\"\"\n\nimport sys\nimport textwrap\nimport unittest\n\nfrom yapf.yapflib import reformatter\nfrom yapf.yapflib import style\n\nfrom yapftests import yapf_test_helper\n\n\nclass TestsForPython3Code(yapf_test_helper.YAPFTest):\n  \"\"\"Test a few constructs that are new Python 3 syntax.\"\"\"\n\n  @classmethod\n  def setUpClass(cls):  # pylint: disable=g-missing-super-call\n    style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testTypedNames(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def x(aaaaaaaaaaaaaaa:int,bbbbbbbbbbbbbbbb:str,ccccccccccccccc:dict,eeeeeeeeeeeeee:set={1, 2, 3})->bool:\n          pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def x(aaaaaaaaaaaaaaa: int,\n              bbbbbbbbbbbbbbbb: str,\n              ccccccccccccccc: dict,\n              eeeeeeeeeeeeee: set = {1, 2, 3}) -> bool:\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testTypedNameWithLongNamedArg(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def func(arg=long_function_call_that_pushes_the_line_over_eighty_characters()) -> ReturnType:\n          pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def func(arg=long_function_call_that_pushes_the_line_over_eighty_characters()\n                 ) -> ReturnType:\n            pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testKeywordOnlyArgSpecifier(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo(a, *, kw):\n          return a+kw\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo(a, *, kw):\n            return a + kw\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testAnnotations(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo(a: list, b: \"bar\") -> dict:\n          return a+b\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo(a: list, b: \"bar\") -> dict:\n            return a + b\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testExecAsNonKeyword(self):\n    unformatted_code = 'methods.exec( sys.modules[name])\\n'\n    expected_formatted_code = 'methods.exec(sys.modules[name])\\n'\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testAsyncFunctions(self):\n    code = textwrap.dedent(\"\"\"\\\n        import asyncio\n        import time\n\n\n        @print_args\n        async def slow_operation():\n            await asyncio.sleep(1)\n            # print(\"Slow operation {} complete\".format(n))\n\n\n        async def main():\n            start = time.time()\n            if (await get_html()):\n                pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testNoSpacesAroundPowerOperator(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a**b\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a ** b\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: pep8, SPACES_AROUND_POWER_OPERATOR: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testSpacesAroundDefaultOrNamedAssign(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        f(a=5)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        f(a = 5)\n    \"\"\")\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: pep8, '\n              'SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN: True}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testTypeHint(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo(x: int=42):\n            pass\n\n\n        def foo2(x: 'int' =42):\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo(x: int = 42):\n            pass\n\n\n        def foo2(x: 'int' = 42):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testMatrixMultiplication(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a=b@c\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = b @ c\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testNoneKeyword(self):\n    code = textwrap.dedent(\"\"\"\\\n        None.__ne__()\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testAsyncWithPrecedingComment(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        import asyncio\n\n        # Comment\n        async def bar():\n            pass\n\n        async def foo():\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        import asyncio\n\n\n        # Comment\n        async def bar():\n            pass\n\n\n        async def foo():\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testAsyncFunctionsNested(self):\n    code = textwrap.dedent(\"\"\"\\\n        async def outer():\n\n            async def inner():\n                pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testKeepTypesIntact(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def _ReduceAbstractContainers(\n            self, *args: Optional[automation_converter.PyiCollectionAbc]) -> List[\n                automation_converter.PyiCollectionAbc]:\n            pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def _ReduceAbstractContainers(\n            self, *args: Optional[automation_converter.PyiCollectionAbc]\n        ) -> List[automation_converter.PyiCollectionAbc]:\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testContinuationIndentWithAsync(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        async def start_websocket():\n            async with session.ws_connect(\n                r\"ws://a_really_long_long_long_long_long_long_url\") as ws:\n                pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        async def start_websocket():\n            async with session.ws_connect(\n                    r\"ws://a_really_long_long_long_long_long_long_url\") as ws:\n                pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testSplittingArguments(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        async def open_file(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):\n            pass\n\n        async def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):\n            pass\n\n        def open_file(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):\n            pass\n\n        def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):\n            pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        async def open_file(\n            file,\n            mode='r',\n            buffering=-1,\n            encoding=None,\n            errors=None,\n            newline=None,\n            closefd=True,\n            opener=None\n        ):\n            pass\n\n\n        async def run_sync_in_worker_thread(\n            sync_fn, *args, cancellable=False, limiter=None\n        ):\n            pass\n\n\n        def open_file(\n            file,\n            mode='r',\n            buffering=-1,\n            encoding=None,\n            errors=None,\n            newline=None,\n            closefd=True,\n            opener=None\n        ):\n            pass\n\n\n        def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):\n            pass\n    \"\"\")  # noqa\n\n    try:\n      style.SetGlobalStyle(\n          style.CreateStyleFromConfig(\n              '{based_on_style: pep8, '\n              'dedent_closing_brackets: true, '\n              'coalesce_brackets: false, '\n              'space_between_ending_comma_and_closing_bracket: false, '\n              'split_arguments_when_comma_terminated: true, '\n              'split_before_first_argument: true}'))\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testDictUnpacking(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class Foo:\n            def foo(self):\n                foofoofoofoofoofoofoofoo('foofoofoofoofoo', {\n\n                    'foo': 'foo',\n\n                    **foofoofoo\n                })\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class Foo:\n\n            def foo(self):\n                foofoofoofoofoofoofoofoo('foofoofoofoofoo', {\n                    'foo': 'foo',\n                    **foofoofoo\n                })\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testMultilineFormatString(self):\n    # https://github.com/google/yapf/issues/513\n    code = textwrap.dedent(\"\"\"\\\n        # yapf: disable\n        (f'''\n          ''')\n        # yapf: enable\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testEllipses(self):\n    # https://github.com/google/yapf/issues/533\n    code = textwrap.dedent(\"\"\"\\\n        def dirichlet(x12345678901234567890123456789012345678901234567890=...) -> None:\n            return\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testFunctionTypedReturnNextLine(self):\n    code = textwrap.dedent(\"\"\"\\\n        def _GenerateStatsEntries(\n            process_id: Text,\n            timestamp: Optional[ffffffff.FFFFFFFFFFF] = None\n        ) -> Sequence[ssssssssssss.SSSSSSSSSSSSSSS]:\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testFunctionTypedReturnSameLine(self):\n    code = textwrap.dedent(\"\"\"\\\n        def rrrrrrrrrrrrrrrrrrrrrr(\n                ccccccccccccccccccccccc: Tuple[Text, Text]) -> List[Tuple[Text, Text]]:\n            pass\n    \"\"\")  # noqa\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testAsyncForElseNotIndentedInsideBody(self):\n    code = textwrap.dedent(\"\"\"\\\n        async def fn():\n            async for message in websocket:\n                for i in range(10):\n                    pass\n                else:\n                    pass\n            else:\n                pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testForElseInAsyncNotMixedWithAsyncFor(self):\n    code = textwrap.dedent(\"\"\"\\\n        async def fn():\n            for i in range(10):\n                pass\n            else:\n                pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testParameterListIndentationConflicts(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def raw_message(  # pylint: disable=too-many-arguments\n                    self, text, user_id=1000, chat_type='private', forward_date=None, forward_from=None):\n                pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def raw_message(  # pylint: disable=too-many-arguments\n                self,\n                text,\n                user_id=1000,\n                chat_type='private',\n                forward_date=None,\n                forward_from=None):\n            pass\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testTypeHintedYieldExpression(self):\n    # https://github.com/google/yapf/issues/1092\n    code = textwrap.dedent(\"\"\"\\\n       def my_coroutine():\n           x: int = yield\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testSyntaxMatch(self):\n    # https://github.com/google/yapf/issues/1045\n    # https://github.com/google/yapf/issues/1085\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a=3\n        b=0\n        match a :\n            case 0 :\n                b=1\n            case _\t:\n                b=2\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = 3\n        b = 0\n        match a:\n            case 0:\n                b = 1\n            case _:\n                b = 2\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testParenthsizedContextManager(self):\n    # https://github.com/google/yapf/issues/1064\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def test_copy_dimension(self):\n            with (Dataset() as target_ds,\n                  Dataset() as source_ds):\n                do_something\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def test_copy_dimension(self):\n            with (Dataset() as target_ds, Dataset() as source_ds):\n                do_something\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testUnpackedTuple(self):\n    # https://github.com/google/yapf/issues/830\n    # https://github.com/google/yapf/issues/1060\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def a():\n          t = (2,3)\n          for i in range(5):\n            yield i,*t\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def a():\n            t = (2, 3)\n            for i in range(5):\n                yield i, *t\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testTypedTuple(self):\n    # https://github.com/google/yapf/issues/412\n    # https://github.com/google/yapf/issues/1058\n    code = textwrap.dedent(\"\"\"\\\n        t: tuple = 1, 2\n        args = tuple(x for x in [2], )\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n  def testWalrusOperator(self):\n    # https://github.com/google/yapf/issues/894\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        import os\n        a=[1,2,3,4]\n        if (n:=len(a))>2:\n            print()\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        import os\n\n        a = [1, 2, 3, 4]\n        if (n := len(a)) > 2:\n            print()\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testCondAssign(self):\n    # https://github.com/google/yapf/issues/856\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def json(self) -> JSONTask:\n                result: JSONTask = {\n                    \"id\": self.id,\n                    \"text\": self.text,\n                    \"status\": self.status,\n                    \"last_mod\": self.last_mod_time\n                }\n                for i in \"parent_id\", \"deadline\", \"reminder\":\n                    if x := getattr(self , i):\n                        result[i] = x  # type: ignore\n                return result\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def json(self) -> JSONTask:\n            result: JSONTask = {\n                \"id\": self.id,\n                \"text\": self.text,\n                \"status\": self.status,\n                \"last_mod\": self.last_mod_time\n            }\n            for i in \"parent_id\", \"deadline\", \"reminder\":\n                if x := getattr(self, i):\n                    result[i] = x  # type: ignore\n            return result\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testCopyDictionary(self):\n    # https://github.com/google/yapf/issues/233\n    # https://github.com/google/yapf/issues/402\n    code = textwrap.dedent(\"\"\"\\\n        a_dict = {'key': 'value'}\n        a_dict_copy = {**a_dict}\n        print('a_dict:', a_dict)\n        print('a_dict_copy:', a_dict_copy)\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self.assertCodeEqual(code, reformatter.Reformat(llines))\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/reformatter_style_config_test.py",
    "content": "# Copyright 2016 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Style config tests for yapf.reformatter.\"\"\"\n\nimport textwrap\nimport unittest\n\nfrom yapf.yapflib import reformatter\nfrom yapf.yapflib import style\n\nfrom yapftests import yapf_test_helper\n\n\nclass TestsForStyleConfig(yapf_test_helper.YAPFTest):\n\n  def setUp(self):\n    self.current_style = style.DEFAULT_STYLE\n\n  def testSetGlobalStyle(self):\n    try:\n      style.SetGlobalStyle(style.CreateYapfStyle())\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          for i in range(5):\n           print('bar')\n      \"\"\")\n      expected_formatted_code = textwrap.dedent(\"\"\"\\\n          for i in range(5):\n            print('bar')\n      \"\"\")\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n      style.DEFAULT_STYLE = self.current_style\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        for i in range(5):\n         print('bar')\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        for i in range(5):\n            print('bar')\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))\n\n  def testOperatorNoSpaceStyle(self):\n    try:\n      sympy_style = style.CreatePEP8Style()\n      sympy_style['NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS'] = \\\n        style._StringSetConverter('*,/')\n      style.SetGlobalStyle(sympy_style)\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          a = 1+2 * 3 - 4 / 5\n          b = '0' * 1\n      \"\"\")\n      expected_formatted_code = textwrap.dedent(\"\"\"\\\n          a = 1 + 2*3 - 4/5\n          b = '0'*1\n      \"\"\")\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n      style.DEFAULT_STYLE = self.current_style\n\n  def testOperatorPrecedenceStyle(self):\n    try:\n      pep8_with_precedence = style.CreatePEP8Style()\n      pep8_with_precedence['ARITHMETIC_PRECEDENCE_INDICATION'] = True\n      style.SetGlobalStyle(pep8_with_precedence)\n      unformatted_code = textwrap.dedent(\"\"\"\\\n          1+2\n          (1 + 2) * (3 - (4 / 5))\n          a = 1 * 2 + 3 / 4\n          b = 1 / 2 - 3 * 4\n          c = (1 + 2) * (3 - 4)\n          d = (1 - 2) / (3 + 4)\n          e = 1 * 2 - 3\n          f = 1 + 2 + 3 + 4\n          g = 1 * 2 * 3 * 4\n          h = 1 + 2 - 3 + 4\n          i = 1 * 2 / 3 * 4\n          j = (1 * 2 - 3) + 4\n          k = (1 * 2 * 3) + (4 * 5 * 6 * 7 * 8)\n      \"\"\")\n      expected_formatted_code = textwrap.dedent(\"\"\"\\\n          1 + 2\n          (1+2) * (3 - (4/5))\n          a = 1*2 + 3/4\n          b = 1/2 - 3*4\n          c = (1+2) * (3-4)\n          d = (1-2) / (3+4)\n          e = 1*2 - 3\n          f = 1 + 2 + 3 + 4\n          g = 1 * 2 * 3 * 4\n          h = 1 + 2 - 3 + 4\n          i = 1 * 2 / 3 * 4\n          j = (1*2 - 3) + 4\n          k = (1*2*3) + (4*5*6*7*8)\n      \"\"\")\n\n      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)\n      self.assertCodeEqual(expected_formatted_code,\n                           reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n      style.DEFAULT_STYLE = self.current_style\n\n  def testNoSplitBeforeFirstArgumentStyle1(self):\n    try:\n      pep8_no_split_before_first = style.CreatePEP8Style()\n      pep8_no_split_before_first['SPLIT_BEFORE_FIRST_ARGUMENT'] = False\n      pep8_no_split_before_first['SPLIT_BEFORE_NAMED_ASSIGNS'] = False\n      style.SetGlobalStyle(pep8_no_split_before_first)\n      formatted_code = textwrap.dedent(\"\"\"\\\n          # Example from in-code MustSplit comments\n          foo = outer_function_call(fitting_inner_function_call(inner_arg1, inner_arg2),\n                                    outer_arg1, outer_arg2)\n\n          foo = outer_function_call(\n              not_fitting_inner_function_call(inner_arg1, inner_arg2), outer_arg1,\n              outer_arg2)\n\n          # Examples Issue#424\n          a_super_long_version_of_print(argument1, argument2, argument3, argument4,\n                                        argument5, argument6, argument7)\n\n          CREDS_FILE = os.path.join(os.path.expanduser('~'),\n                                    'apis/super-secret-admin-creds.json')\n\n          # Examples Issue#556\n          i_take_a_lot_of_params(arg1, param1=very_long_expression1(),\n                                 param2=very_long_expression2(),\n                                 param3=very_long_expression3(),\n                                 param4=very_long_expression4())\n\n          # Examples Issue#590\n          plt.plot(numpy.linspace(0, 1, 10), numpy.linspace(0, 1, 10), marker=\"x\",\n                   color=\"r\")\n\n          plt.plot(veryverylongvariablename, veryverylongvariablename, marker=\"x\",\n                   color=\"r\")\n      \"\"\")  # noqa\n      llines = yapf_test_helper.ParseAndUnwrap(formatted_code)\n      self.assertCodeEqual(formatted_code, reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n      style.DEFAULT_STYLE = self.current_style\n\n  def testNoSplitBeforeFirstArgumentStyle2(self):\n    try:\n      pep8_no_split_before_first = style.CreatePEP8Style()\n      pep8_no_split_before_first['SPLIT_BEFORE_FIRST_ARGUMENT'] = False\n      pep8_no_split_before_first['SPLIT_BEFORE_NAMED_ASSIGNS'] = True\n      style.SetGlobalStyle(pep8_no_split_before_first)\n      formatted_code = textwrap.dedent(\"\"\"\\\n          # Examples Issue#556\n          i_take_a_lot_of_params(arg1,\n                                 param1=very_long_expression1(),\n                                 param2=very_long_expression2(),\n                                 param3=very_long_expression3(),\n                                 param4=very_long_expression4())\n\n          # Examples Issue#590\n          plt.plot(numpy.linspace(0, 1, 10),\n                   numpy.linspace(0, 1, 10),\n                   marker=\"x\",\n                   color=\"r\")\n\n          plt.plot(veryverylongvariablename,\n                   veryverylongvariablename,\n                   marker=\"x\",\n                   color=\"r\")\n      \"\"\")\n      llines = yapf_test_helper.ParseAndUnwrap(formatted_code)\n      self.assertCodeEqual(formatted_code, reformatter.Reformat(llines))\n    finally:\n      style.SetGlobalStyle(style.CreatePEP8Style())\n      style.DEFAULT_STYLE = self.current_style\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/split_penalty_test.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.split_penalty.\"\"\"\n\nimport sys\nimport textwrap\nimport unittest\n\nfrom yapf_third_party._ylib2to3 import pytree\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.pytree import pytree_visitor\nfrom yapf.pytree import split_penalty\nfrom yapf.yapflib import style\n\nfrom yapftests import yapf_test_helper\n\nUNBREAKABLE = split_penalty.UNBREAKABLE\nVERY_STRONGLY_CONNECTED = split_penalty.VERY_STRONGLY_CONNECTED\nDOTTED_NAME = split_penalty.DOTTED_NAME\nSTRONGLY_CONNECTED = split_penalty.STRONGLY_CONNECTED\n\n\nclass SplitPenaltyTest(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):\n    style.SetGlobalStyle(style.CreateYapfStyle())\n\n  def _ParseAndComputePenalties(self, code, dumptree=False):\n    \"\"\"Parses the code and computes split penalties.\n\n    Arguments:\n      code: code to parse as a string\n      dumptree: if True, the parsed pytree (after penalty assignment) is dumped\n        to stderr. Useful for debugging.\n\n    Returns:\n      Parse tree.\n    \"\"\"\n    tree = pytree_utils.ParseCodeToTree(code)\n    split_penalty.ComputeSplitPenalties(tree)\n    if dumptree:\n      pytree_visitor.DumpPyTree(tree, target_stream=sys.stderr)\n    return tree\n\n  def _CheckPenalties(self, tree, list_of_expected):\n    \"\"\"Check that the tokens in the tree have the correct penalties.\n\n    Args:\n      tree: the pytree.\n      list_of_expected: list of (name, penalty) pairs. Non-semantic tokens are\n        filtered out from the expected values.\n    \"\"\"\n\n    def FlattenRec(tree):\n      if pytree_utils.NodeName(tree) in pytree_utils.NONSEMANTIC_TOKENS:\n        return []\n      if isinstance(tree, pytree.Leaf):\n        return [(tree.value,\n                 pytree_utils.GetNodeAnnotation(\n                     tree, pytree_utils.Annotation.SPLIT_PENALTY))]\n      nodes = []\n      for node in tree.children:\n        nodes += FlattenRec(node)\n      return nodes\n\n    self.assertEqual(list_of_expected, FlattenRec(tree))\n\n  def testUnbreakable(self):\n    # Test function definitions.\n    code = textwrap.dedent(\"\"\"\\\n        def foo(x):\n          pass\n    \"\"\")\n    tree = self._ParseAndComputePenalties(code)\n    self._CheckPenalties(tree, [\n        ('def', None),\n        ('foo', UNBREAKABLE),\n        ('(', UNBREAKABLE),\n        ('x', None),\n        (')', STRONGLY_CONNECTED),\n        (':', UNBREAKABLE),\n        ('pass', None),\n    ])\n\n    # Test function definition with trailing comment.\n    code = textwrap.dedent(\"\"\"\\\n        def foo(x):  # trailing comment\n          pass\n    \"\"\")\n    tree = self._ParseAndComputePenalties(code)\n    self._CheckPenalties(tree, [\n        ('def', None),\n        ('foo', UNBREAKABLE),\n        ('(', UNBREAKABLE),\n        ('x', None),\n        (')', STRONGLY_CONNECTED),\n        (':', UNBREAKABLE),\n        ('pass', None),\n    ])\n\n    # Test class definitions.\n    code = textwrap.dedent(\"\"\"\\\n        class A:\n          pass\n        class B(A):\n          pass\n    \"\"\")\n    tree = self._ParseAndComputePenalties(code)\n    self._CheckPenalties(tree, [\n        ('class', None),\n        ('A', UNBREAKABLE),\n        (':', UNBREAKABLE),\n        ('pass', None),\n        ('class', None),\n        ('B', UNBREAKABLE),\n        ('(', UNBREAKABLE),\n        ('A', None),\n        (')', None),\n        (':', UNBREAKABLE),\n        ('pass', None),\n    ])\n\n    # Test lambda definitions.\n    code = textwrap.dedent(\"\"\"\\\n        lambda a, b: None\n    \"\"\")\n    tree = self._ParseAndComputePenalties(code)\n    self._CheckPenalties(tree, [\n        ('lambda', None),\n        ('a', VERY_STRONGLY_CONNECTED),\n        (',', VERY_STRONGLY_CONNECTED),\n        ('b', VERY_STRONGLY_CONNECTED),\n        (':', VERY_STRONGLY_CONNECTED),\n        ('None', VERY_STRONGLY_CONNECTED),\n    ])\n\n    # Test dotted names.\n    code = textwrap.dedent(\"\"\"\\\n        import a.b.c\n    \"\"\")\n    tree = self._ParseAndComputePenalties(code)\n    self._CheckPenalties(tree, [\n        ('import', None),\n        ('a', None),\n        ('.', UNBREAKABLE),\n        ('b', UNBREAKABLE),\n        ('.', UNBREAKABLE),\n        ('c', UNBREAKABLE),\n    ])\n\n  def testStronglyConnected(self):\n    # Test dictionary keys.\n    code = textwrap.dedent(\"\"\"\\\n        a = {\n            'x': 42,\n            y(lambda a: 23): 37,\n        }\n    \"\"\")\n    tree = self._ParseAndComputePenalties(code)\n    self._CheckPenalties(tree, [\n        ('a', None),\n        ('=', None),\n        ('{', None),\n        (\"'x'\", None),\n        (':', STRONGLY_CONNECTED),\n        ('42', None),\n        (',', None),\n        ('y', None),\n        ('(', UNBREAKABLE),\n        ('lambda', STRONGLY_CONNECTED),\n        ('a', VERY_STRONGLY_CONNECTED),\n        (':', VERY_STRONGLY_CONNECTED),\n        ('23', VERY_STRONGLY_CONNECTED),\n        (')', VERY_STRONGLY_CONNECTED),\n        (':', STRONGLY_CONNECTED),\n        ('37', None),\n        (',', None),\n        ('}', None),\n    ])\n\n    # Test list comprehension.\n    code = textwrap.dedent(\"\"\"\\\n        [a for a in foo if a.x == 37]\n    \"\"\")\n    tree = self._ParseAndComputePenalties(code)\n    self._CheckPenalties(tree, [\n        ('[', None),\n        ('a', None),\n        ('for', 0),\n        ('a', STRONGLY_CONNECTED),\n        ('in', STRONGLY_CONNECTED),\n        ('foo', STRONGLY_CONNECTED),\n        ('if', 0),\n        ('a', STRONGLY_CONNECTED),\n        ('.', VERY_STRONGLY_CONNECTED),\n        ('x', DOTTED_NAME),\n        ('==', STRONGLY_CONNECTED),\n        ('37', STRONGLY_CONNECTED),\n        (']', None),\n    ])\n\n  def testFuncCalls(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo(1, 2, 3)\n    \"\"\")\n    tree = self._ParseAndComputePenalties(code)\n    self._CheckPenalties(tree, [\n        ('foo', None),\n        ('(', UNBREAKABLE),\n        ('1', None),\n        (',', UNBREAKABLE),\n        ('2', None),\n        (',', UNBREAKABLE),\n        ('3', None),\n        (')', VERY_STRONGLY_CONNECTED),\n    ])\n\n    # Now a method call, which has more than one trailer\n    code = textwrap.dedent(\"\"\"\\\n        foo.bar.baz(1, 2, 3)\n    \"\"\")\n    tree = self._ParseAndComputePenalties(code)\n    self._CheckPenalties(tree, [\n        ('foo', None),\n        ('.', VERY_STRONGLY_CONNECTED),\n        ('bar', DOTTED_NAME),\n        ('.', VERY_STRONGLY_CONNECTED),\n        ('baz', DOTTED_NAME),\n        ('(', STRONGLY_CONNECTED),\n        ('1', None),\n        (',', UNBREAKABLE),\n        ('2', None),\n        (',', UNBREAKABLE),\n        ('3', None),\n        (')', VERY_STRONGLY_CONNECTED),\n    ])\n\n    # Test single generator argument.\n    code = textwrap.dedent(\"\"\"\\\n        max(i for i in xrange(10))\n    \"\"\")\n    tree = self._ParseAndComputePenalties(code)\n    self._CheckPenalties(tree, [\n        ('max', None),\n        ('(', UNBREAKABLE),\n        ('i', 0),\n        ('for', 0),\n        ('i', STRONGLY_CONNECTED),\n        ('in', STRONGLY_CONNECTED),\n        ('xrange', STRONGLY_CONNECTED),\n        ('(', UNBREAKABLE),\n        ('10', STRONGLY_CONNECTED),\n        (')', VERY_STRONGLY_CONNECTED),\n        (')', VERY_STRONGLY_CONNECTED),\n    ])\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/style_test.py",
    "content": "# -*- coding: utf-8 -*-\n# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.style.\"\"\"\n\nimport os\nimport shutil\nimport tempfile\nimport textwrap\nimport unittest\n\nfrom yapf.yapflib import style\n\nfrom yapftests import utils\nfrom yapftests import yapf_test_helper\n\n\nclass UtilsTest(yapf_test_helper.YAPFTest):\n\n  def testContinuationAlignStyleStringConverter(self):\n    for cont_align_space in ('', 'space', '\"space\"', '\\'space\\''):\n      self.assertEqual(\n          style._ContinuationAlignStyleStringConverter(cont_align_space),\n          'SPACE')\n    for cont_align_fixed in ('fixed', '\"fixed\"', '\\'fixed\\''):\n      self.assertEqual(\n          style._ContinuationAlignStyleStringConverter(cont_align_fixed),\n          'FIXED')\n    for cont_align_valignright in (\n        'valign-right',\n        '\"valign-right\"',\n        '\\'valign-right\\'',\n        'valign_right',\n        '\"valign_right\"',\n        '\\'valign_right\\'',\n    ):\n      self.assertEqual(\n          style._ContinuationAlignStyleStringConverter(cont_align_valignright),\n          'VALIGN-RIGHT')\n    with self.assertRaises(ValueError) as ctx:\n      style._ContinuationAlignStyleStringConverter('blahblah')\n    self.assertIn(\"unknown continuation align style: 'blahblah'\",\n                  str(ctx.exception))\n\n  def testStringListConverter(self):\n    self.assertEqual(style._StringListConverter('foo, bar'), ['foo', 'bar'])\n    self.assertEqual(style._StringListConverter('foo,bar'), ['foo', 'bar'])\n    self.assertEqual(style._StringListConverter('  foo'), ['foo'])\n    self.assertEqual(\n        style._StringListConverter('joe  ,foo,  bar'), ['joe', 'foo', 'bar'])\n\n  def testBoolConverter(self):\n    self.assertEqual(style._BoolConverter('true'), True)\n    self.assertEqual(style._BoolConverter('1'), True)\n    self.assertEqual(style._BoolConverter('false'), False)\n    self.assertEqual(style._BoolConverter('0'), False)\n\n  def testIntListConverter(self):\n    self.assertEqual(style._IntListConverter('1, 2, 3'), [1, 2, 3])\n    self.assertEqual(style._IntListConverter('[ 1, 2, 3 ]'), [1, 2, 3])\n    self.assertEqual(style._IntListConverter('[ 1, 2, 3, ]'), [1, 2, 3])\n\n  def testIntOrIntListConverter(self):\n    self.assertEqual(style._IntOrIntListConverter('10'), 10)\n    self.assertEqual(style._IntOrIntListConverter('1, 2, 3'), [1, 2, 3])\n\n\ndef _LooksLikeGoogleStyle(cfg):\n  return cfg['COLUMN_LIMIT'] == 80 and cfg['SPLIT_COMPLEX_COMPREHENSION']\n\n\ndef _LooksLikePEP8Style(cfg):\n  return cfg['COLUMN_LIMIT'] == 79\n\n\ndef _LooksLikeFacebookStyle(cfg):\n  return cfg['DEDENT_CLOSING_BRACKETS']\n\n\ndef _LooksLikeYapfStyle(cfg):\n  return cfg['SPLIT_BEFORE_DOT']\n\n\nclass PredefinedStylesByNameTest(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):  # pylint: disable=g-missing-super-call\n    style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testDefault(self):\n    # default is PEP8\n    cfg = style.CreateStyleFromConfig(None)\n    self.assertTrue(_LooksLikePEP8Style(cfg))\n\n  def testPEP8ByName(self):\n    for pep8_name in ('PEP8', 'pep8', 'Pep8'):\n      cfg = style.CreateStyleFromConfig(pep8_name)\n      self.assertTrue(_LooksLikePEP8Style(cfg))\n\n  def testGoogleByName(self):\n    for google_name in ('google', 'Google', 'GOOGLE'):\n      cfg = style.CreateStyleFromConfig(google_name)\n      self.assertTrue(_LooksLikeGoogleStyle(cfg))\n\n  def testYapfByName(self):\n    for yapf_name in ('yapf', 'YAPF'):\n      cfg = style.CreateStyleFromConfig(yapf_name)\n      self.assertTrue(_LooksLikeYapfStyle(cfg))\n\n  def testFacebookByName(self):\n    for fb_name in ('facebook', 'FACEBOOK', 'Facebook'):\n      cfg = style.CreateStyleFromConfig(fb_name)\n      self.assertTrue(_LooksLikeFacebookStyle(cfg))\n\n\nclass StyleFromFileTest(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):  # pylint: disable=g-missing-super-call\n    cls.test_tmpdir = tempfile.mkdtemp()\n    style.SetGlobalStyle(style.CreatePEP8Style())\n\n  @classmethod\n  def tearDownClass(cls):  # pylint: disable=g-missing-super-call\n    shutil.rmtree(cls.test_tmpdir)\n\n  def testDefaultBasedOnStyle(self):\n    cfg = textwrap.dedent(\"\"\"\\\n        [style]\n        continuation_indent_width = 20\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:\n      cfg = style.CreateStyleFromConfig(filepath)\n      self.assertTrue(_LooksLikePEP8Style(cfg))\n      self.assertEqual(cfg['CONTINUATION_INDENT_WIDTH'], 20)\n\n  def testDefaultBasedOnPEP8Style(self):\n    cfg = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = pep8\n        continuation_indent_width = 40\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:\n      cfg = style.CreateStyleFromConfig(filepath)\n      self.assertTrue(_LooksLikePEP8Style(cfg))\n      self.assertEqual(cfg['CONTINUATION_INDENT_WIDTH'], 40)\n\n  def testDefaultBasedOnGoogleStyle(self):\n    cfg = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = google\n        continuation_indent_width = 20\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:\n      cfg = style.CreateStyleFromConfig(filepath)\n      self.assertTrue(_LooksLikeGoogleStyle(cfg))\n      self.assertEqual(cfg['CONTINUATION_INDENT_WIDTH'], 20)\n\n  def testDefaultBasedOnFacebookStyle(self):\n    cfg = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = facebook\n        continuation_indent_width = 20\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:\n      cfg = style.CreateStyleFromConfig(filepath)\n      self.assertTrue(_LooksLikeFacebookStyle(cfg))\n      self.assertEqual(cfg['CONTINUATION_INDENT_WIDTH'], 20)\n\n  def testBoolOptionValue(self):\n    cfg = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = pep8\n        SPLIT_BEFORE_NAMED_ASSIGNS=False\n        split_before_logical_operator = true\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:\n      cfg = style.CreateStyleFromConfig(filepath)\n      self.assertTrue(_LooksLikePEP8Style(cfg))\n      self.assertEqual(cfg['SPLIT_BEFORE_NAMED_ASSIGNS'], False)\n      self.assertEqual(cfg['SPLIT_BEFORE_LOGICAL_OPERATOR'], True)\n\n  def testStringListOptionValue(self):\n    cfg = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = pep8\n        I18N_FUNCTION_CALL = N_, V_, T_\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:\n      cfg = style.CreateStyleFromConfig(filepath)\n      self.assertTrue(_LooksLikePEP8Style(cfg))\n      self.assertEqual(cfg['I18N_FUNCTION_CALL'], ['N_', 'V_', 'T_'])\n\n  def testErrorNoStyleFile(self):\n    with self.assertRaisesRegex(style.StyleConfigError,\n                                'is not a valid style or file path'):\n      style.CreateStyleFromConfig('/8822/xyznosuchfile')\n\n  def testErrorNoStyleSection(self):\n    cfg = textwrap.dedent(\"\"\"\\\n        [s]\n        indent_width=2\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:\n      with self.assertRaisesRegex(style.StyleConfigError,\n                                  'Unable to find section'):\n        style.CreateStyleFromConfig(filepath)\n\n  def testErrorUnknownStyleOption(self):\n    cfg = textwrap.dedent(\"\"\"\\\n        [style]\n        indent_width=2\n        hummus=2\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:\n      with self.assertRaisesRegex(style.StyleConfigError,\n                                  'Unknown style option'):\n        style.CreateStyleFromConfig(filepath)\n\n  def testPyprojectTomlNoYapfSection(self):\n    filepath = os.path.join(self.test_tmpdir, 'pyproject.toml')\n    _ = open(filepath, 'w')\n    with self.assertRaisesRegex(style.StyleConfigError,\n                                'Unable to find section'):\n      style.CreateStyleFromConfig(filepath)\n\n  def testPyprojectTomlParseYapfSection(self):\n\n    cfg = textwrap.dedent(\"\"\"\\\n        [tool.yapf]\n        based_on_style = \"pep8\"\n        continuation_indent_width = 40\n    \"\"\")\n    filepath = os.path.join(self.test_tmpdir, 'pyproject.toml')\n    with open(filepath, 'w') as f:\n      f.write(cfg)\n    cfg = style.CreateStyleFromConfig(filepath)\n    self.assertTrue(_LooksLikePEP8Style(cfg))\n    self.assertEqual(cfg['CONTINUATION_INDENT_WIDTH'], 40)\n\n\nclass StyleFromDict(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):  # pylint: disable=g-missing-super-call\n    style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testDefaultBasedOnStyle(self):\n    config_dict = {\n        'based_on_style': 'pep8',\n        'indent_width': 2,\n        'blank_line_before_nested_class_or_def': True\n    }\n    cfg = style.CreateStyleFromConfig(config_dict)\n    self.assertTrue(_LooksLikePEP8Style(cfg))\n    self.assertEqual(cfg['INDENT_WIDTH'], 2)\n\n  def testDefaultBasedOnStyleBadDict(self):\n    self.assertRaisesRegex(style.StyleConfigError, 'Unknown style option',\n                           style.CreateStyleFromConfig,\n                           {'based_on_styl': 'pep8'})\n    self.assertRaisesRegex(style.StyleConfigError, 'not a valid',\n                           style.CreateStyleFromConfig,\n                           {'INDENT_WIDTH': 'FOUR'})\n\n\nclass StyleFromCommandLine(yapf_test_helper.YAPFTest):\n\n  @classmethod\n  def setUpClass(cls):  # pylint: disable=g-missing-super-call\n    style.SetGlobalStyle(style.CreatePEP8Style())\n\n  def testDefaultBasedOnStyle(self):\n    cfg = style.CreateStyleFromConfig(\n        '{based_on_style: pep8,'\n        ' indent_width: 2,'\n        ' blank_line_before_nested_class_or_def: True}')\n    self.assertTrue(_LooksLikePEP8Style(cfg))\n    self.assertEqual(cfg['INDENT_WIDTH'], 2)\n\n  def testDefaultBasedOnStyleNotStrict(self):\n    cfg = style.CreateStyleFromConfig(\n        '{based_on_style : pep8,'\n        ' indent_width=2'\n        ' blank_line_before_nested_class_or_def:True}')\n    self.assertTrue(_LooksLikePEP8Style(cfg))\n    self.assertEqual(cfg['INDENT_WIDTH'], 2)\n\n  def testDefaultBasedOnExplicitlyUnicodeTypeString(self):\n    cfg = style.CreateStyleFromConfig('{}')\n    self.assertIsInstance(cfg, dict)\n\n  def testDefaultBasedOnDetaultTypeString(self):\n    cfg = style.CreateStyleFromConfig('{}')\n    self.assertIsInstance(cfg, dict)\n\n  def testDefaultBasedOnStyleBadString(self):\n    self.assertRaisesRegex(style.StyleConfigError, 'Unknown style option',\n                           style.CreateStyleFromConfig, '{based_on_styl: pep8}')\n    self.assertRaisesRegex(style.StyleConfigError, 'not a valid',\n                           style.CreateStyleFromConfig, '{INDENT_WIDTH: FOUR}')\n    self.assertRaisesRegex(style.StyleConfigError, 'Invalid style dict',\n                           style.CreateStyleFromConfig, '{based_on_style: pep8')\n\n\nclass StyleHelp(yapf_test_helper.YAPFTest):\n\n  def testHelpKeys(self):\n    settings = sorted(style.Help())\n    expected = sorted(style._style)\n    self.assertListEqual(settings, expected)\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/subtype_assigner_test.py",
    "content": "# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.subtype_assigner.\"\"\"\n\nimport textwrap\nimport unittest\n\nfrom yapf.pytree import pytree_utils\nfrom yapf.yapflib import format_token\nfrom yapf.yapflib import subtypes\n\nfrom yapftests import yapf_test_helper\n\n\nclass SubtypeAssignerTest(yapf_test_helper.YAPFTest):\n\n  def _CheckFormatTokenSubtypes(self, llines, list_of_expected):\n    \"\"\"Check that the tokens in the LogicalLines have the expected subtypes.\n\n    Args:\n      llines: list of LogicalLine.\n      list_of_expected: list of (name, subtype) pairs. Non-semantic tokens are\n        filtered out from the expected values.\n    \"\"\"\n    actual = []\n    for lline in llines:\n      filtered_values = [(ft.value, ft.subtypes)\n                         for ft in lline.tokens\n                         if ft.name not in pytree_utils.NONSEMANTIC_TOKENS]\n      if filtered_values:\n        actual.append(filtered_values)\n\n    self.assertEqual(list_of_expected, actual)\n\n  def testFuncDefDefaultAssign(self):\n    self.maxDiff = None  # pylint: disable=invalid-name\n    code = textwrap.dedent(\"\"\"\\\n        def foo(a=37, *b, **c):\n          return -x[:42]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('def', {subtypes.NONE}),\n            ('foo', {subtypes.FUNC_DEF}),\n            ('(', {subtypes.NONE}),\n            ('a', {\n                subtypes.NONE,\n                subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST,\n                subtypes.PARAMETER_START,\n            }),\n            ('=', {\n                subtypes.DEFAULT_OR_NAMED_ASSIGN,\n                subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST,\n            }),\n            ('37', {\n                subtypes.NONE,\n                subtypes.PARAMETER_STOP,\n                subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST,\n            }),\n            (',', {subtypes.NONE}),\n            ('*', {\n                subtypes.PARAMETER_START,\n                subtypes.VARARGS_STAR,\n                subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST,\n            }),\n            ('b', {\n                subtypes.NONE,\n                subtypes.PARAMETER_STOP,\n                subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST,\n            }),\n            (',', {subtypes.NONE}),\n            ('**', {\n                subtypes.PARAMETER_START,\n                subtypes.KWARGS_STAR_STAR,\n                subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST,\n            }),\n            ('c', {\n                subtypes.NONE,\n                subtypes.PARAMETER_STOP,\n                subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST,\n            }),\n            (')', {subtypes.NONE}),\n            (':', {subtypes.NONE}),\n        ],\n        [\n            ('return', {subtypes.NONE}),\n            ('-', {subtypes.UNARY_OPERATOR}),\n            ('x', {subtypes.NONE}),\n            ('[', {subtypes.SUBSCRIPT_BRACKET}),\n            (':', {subtypes.SUBSCRIPT_COLON}),\n            ('42', {subtypes.NONE}),\n            (']', {subtypes.SUBSCRIPT_BRACKET}),\n        ],\n    ])\n\n  def testFuncCallWithDefaultAssign(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo(x, a='hello world')\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('foo', {subtypes.NONE}),\n            ('(', {subtypes.NONE}),\n            ('x', {\n                subtypes.NONE,\n                subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST,\n            }),\n            (',', {subtypes.NONE}),\n            ('a', {\n                subtypes.NONE,\n                subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST,\n            }),\n            ('=', {subtypes.DEFAULT_OR_NAMED_ASSIGN}),\n            (\"'hello world'\", {subtypes.NONE}),\n            (')', {subtypes.NONE}),\n        ],\n    ])\n\n  def testSetComprehension(self):\n    code = textwrap.dedent(\"\"\"\\\n        def foo(value):\n          return {value.lower()}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('def', {subtypes.NONE}),\n            ('foo', {subtypes.FUNC_DEF}),\n            ('(', {subtypes.NONE}),\n            ('value', {\n                subtypes.NONE,\n                subtypes.PARAMETER_START,\n                subtypes.PARAMETER_STOP,\n            }),\n            (')', {subtypes.NONE}),\n            (':', {subtypes.NONE}),\n        ],\n        [\n            ('return', {subtypes.NONE}),\n            ('{', {subtypes.NONE}),\n            ('value', {subtypes.NONE}),\n            ('.', {subtypes.NONE}),\n            ('lower', {subtypes.NONE}),\n            ('(', {subtypes.NONE}),\n            (')', {subtypes.NONE}),\n            ('}', {subtypes.NONE}),\n        ],\n    ])\n\n    code = textwrap.dedent(\"\"\"\\\n        def foo(strs):\n          return {s.lower() for s in strs}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('def', {subtypes.NONE}),\n            ('foo', {subtypes.FUNC_DEF}),\n            ('(', {subtypes.NONE}),\n            ('strs', {\n                subtypes.NONE,\n                subtypes.PARAMETER_START,\n                subtypes.PARAMETER_STOP,\n            }),\n            (')', {subtypes.NONE}),\n            (':', {subtypes.NONE}),\n        ],\n        [\n            ('return', {subtypes.NONE}),\n            ('{', {subtypes.NONE}),\n            ('s', {subtypes.COMP_EXPR}),\n            ('.', {subtypes.COMP_EXPR}),\n            ('lower', {subtypes.COMP_EXPR}),\n            ('(', {subtypes.COMP_EXPR}),\n            (')', {subtypes.COMP_EXPR}),\n            ('for', {\n                subtypes.DICT_SET_GENERATOR,\n                subtypes.COMP_FOR,\n            }),\n            ('s', {subtypes.COMP_FOR}),\n            ('in', {subtypes.COMP_FOR}),\n            ('strs', {subtypes.COMP_FOR}),\n            ('}', {subtypes.NONE}),\n        ],\n    ])\n\n    code = textwrap.dedent(\"\"\"\\\n        def foo(strs):\n          return {s + s.lower() for s in strs}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('def', {subtypes.NONE}),\n            ('foo', {subtypes.FUNC_DEF}),\n            ('(', {subtypes.NONE}),\n            ('strs', {\n                subtypes.NONE,\n                subtypes.PARAMETER_START,\n                subtypes.PARAMETER_STOP,\n            }),\n            (')', {subtypes.NONE}),\n            (':', {subtypes.NONE}),\n        ],\n        [\n            ('return', {subtypes.NONE}),\n            ('{', {subtypes.NONE}),\n            ('s', {subtypes.COMP_EXPR}),\n            ('+', {subtypes.BINARY_OPERATOR, subtypes.COMP_EXPR}),\n            ('s', {subtypes.COMP_EXPR}),\n            ('.', {subtypes.COMP_EXPR}),\n            ('lower', {subtypes.COMP_EXPR}),\n            ('(', {subtypes.COMP_EXPR}),\n            (')', {subtypes.COMP_EXPR}),\n            ('for', {\n                subtypes.DICT_SET_GENERATOR,\n                subtypes.COMP_FOR,\n            }),\n            ('s', {subtypes.COMP_FOR}),\n            ('in', {subtypes.COMP_FOR}),\n            ('strs', {subtypes.COMP_FOR}),\n            ('}', {subtypes.NONE}),\n        ],\n    ])\n\n    code = textwrap.dedent(\"\"\"\\\n        def foo(strs):\n          return {c.lower() for s in strs for c in s}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('def', {subtypes.NONE}),\n            ('foo', {subtypes.FUNC_DEF}),\n            ('(', {subtypes.NONE}),\n            ('strs', {\n                subtypes.NONE,\n                subtypes.PARAMETER_START,\n                subtypes.PARAMETER_STOP,\n            }),\n            (')', {subtypes.NONE}),\n            (':', {subtypes.NONE}),\n        ],\n        [\n            ('return', {subtypes.NONE}),\n            ('{', {subtypes.NONE}),\n            ('c', {subtypes.COMP_EXPR}),\n            ('.', {subtypes.COMP_EXPR}),\n            ('lower', {subtypes.COMP_EXPR}),\n            ('(', {subtypes.COMP_EXPR}),\n            (')', {subtypes.COMP_EXPR}),\n            ('for', {\n                subtypes.DICT_SET_GENERATOR,\n                subtypes.COMP_FOR,\n                subtypes.COMP_EXPR,\n            }),\n            ('s', {subtypes.COMP_FOR, subtypes.COMP_EXPR}),\n            ('in', {subtypes.COMP_FOR, subtypes.COMP_EXPR}),\n            ('strs', {subtypes.COMP_FOR, subtypes.COMP_EXPR}),\n            ('for', {\n                subtypes.DICT_SET_GENERATOR,\n                subtypes.COMP_FOR,\n            }),\n            ('c', {subtypes.COMP_FOR}),\n            ('in', {subtypes.COMP_FOR}),\n            ('s', {subtypes.COMP_FOR}),\n            ('}', {subtypes.NONE}),\n        ],\n    ])\n\n  def testDictComprehension(self):\n    code = textwrap.dedent(\"\"\"\\\n        def foo(value):\n          return {value: value.lower()}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('def', {subtypes.NONE}),\n            ('foo', {subtypes.FUNC_DEF}),\n            ('(', {subtypes.NONE}),\n            ('value', {\n                subtypes.NONE,\n                subtypes.PARAMETER_START,\n                subtypes.PARAMETER_STOP,\n            }),\n            (')', {subtypes.NONE}),\n            (':', {subtypes.NONE}),\n        ],\n        [\n            ('return', {subtypes.NONE}),\n            ('{', {subtypes.NONE}),\n            ('value', {subtypes.DICTIONARY_KEY, subtypes.DICTIONARY_KEY_PART}),\n            (':', {subtypes.NONE}),\n            ('value', {subtypes.DICTIONARY_VALUE}),\n            ('.', {subtypes.NONE}),\n            ('lower', {subtypes.NONE}),\n            ('(', {subtypes.NONE}),\n            (')', {subtypes.NONE}),\n            ('}', {subtypes.NONE}),\n        ],\n    ])\n\n    code = textwrap.dedent(\"\"\"\\\n        def foo(strs):\n          return {s: s.lower() for s in strs}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('def', {subtypes.NONE}),\n            ('foo', {subtypes.FUNC_DEF}),\n            ('(', {subtypes.NONE}),\n            ('strs', {\n                subtypes.NONE,\n                subtypes.PARAMETER_START,\n                subtypes.PARAMETER_STOP,\n            }),\n            (')', {subtypes.NONE}),\n            (':', {subtypes.NONE}),\n        ],\n        [\n            ('return', {subtypes.NONE}),\n            ('{', {subtypes.NONE}),\n            ('s', {\n                subtypes.DICTIONARY_KEY, subtypes.DICTIONARY_KEY_PART,\n                subtypes.COMP_EXPR\n            }),\n            (':', {subtypes.COMP_EXPR}),\n            ('s', {subtypes.DICTIONARY_VALUE, subtypes.COMP_EXPR}),\n            ('.', {subtypes.COMP_EXPR}),\n            ('lower', {subtypes.COMP_EXPR}),\n            ('(', {subtypes.COMP_EXPR}),\n            (')', {subtypes.COMP_EXPR}),\n            ('for', {\n                subtypes.DICT_SET_GENERATOR,\n                subtypes.COMP_FOR,\n            }),\n            ('s', {subtypes.COMP_FOR}),\n            ('in', {subtypes.COMP_FOR}),\n            ('strs', {subtypes.COMP_FOR}),\n            ('}', {subtypes.NONE}),\n        ],\n    ])\n\n    code = textwrap.dedent(\"\"\"\\\n        def foo(strs):\n          return {c: c.lower() for s in strs for c in s}\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('def', {subtypes.NONE}),\n            ('foo', {subtypes.FUNC_DEF}),\n            ('(', {subtypes.NONE}),\n            ('strs', {\n                subtypes.NONE,\n                subtypes.PARAMETER_START,\n                subtypes.PARAMETER_STOP,\n            }),\n            (')', {subtypes.NONE}),\n            (':', {subtypes.NONE}),\n        ],\n        [\n            ('return', {subtypes.NONE}),\n            ('{', {subtypes.NONE}),\n            ('c', {\n                subtypes.DICTIONARY_KEY, subtypes.DICTIONARY_KEY_PART,\n                subtypes.COMP_EXPR\n            }),\n            (':', {subtypes.COMP_EXPR}),\n            ('c', {subtypes.DICTIONARY_VALUE, subtypes.COMP_EXPR}),\n            ('.', {subtypes.COMP_EXPR}),\n            ('lower', {subtypes.COMP_EXPR}),\n            ('(', {subtypes.COMP_EXPR}),\n            (')', {subtypes.COMP_EXPR}),\n            ('for', {\n                subtypes.DICT_SET_GENERATOR,\n                subtypes.COMP_FOR,\n                subtypes.COMP_EXPR,\n            }),\n            ('s', {subtypes.COMP_FOR, subtypes.COMP_EXPR}),\n            ('in', {subtypes.COMP_FOR, subtypes.COMP_EXPR}),\n            ('strs', {subtypes.COMP_FOR, subtypes.COMP_EXPR}),\n            ('for', {\n                subtypes.DICT_SET_GENERATOR,\n                subtypes.COMP_FOR,\n            }),\n            ('c', {subtypes.COMP_FOR}),\n            ('in', {subtypes.COMP_FOR}),\n            ('s', {subtypes.COMP_FOR}),\n            ('}', {subtypes.NONE}),\n        ],\n    ])\n\n  def testUnaryNotOperator(self):\n    code = textwrap.dedent(\"\"\"\\\n        not a\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [[('not', {subtypes.UNARY_OPERATOR}),\n                                             ('a', {subtypes.NONE})]])\n\n  def testBitwiseOperators(self):\n    code = textwrap.dedent(\"\"\"\\\n        x = ((a | (b ^ 3) & c) << 3) >> 1\n        \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('x', {subtypes.NONE}),\n            ('=', {subtypes.ASSIGN_OPERATOR}),\n            ('(', {subtypes.NONE}),\n            ('(', {subtypes.NONE}),\n            ('a', {subtypes.NONE}),\n            ('|', {subtypes.BINARY_OPERATOR}),\n            ('(', {subtypes.NONE}),\n            ('b', {subtypes.NONE}),\n            ('^', {subtypes.BINARY_OPERATOR}),\n            ('3', {subtypes.NONE}),\n            (')', {subtypes.NONE}),\n            ('&', {subtypes.BINARY_OPERATOR}),\n            ('c', {subtypes.NONE}),\n            (')', {subtypes.NONE}),\n            ('<<', {subtypes.BINARY_OPERATOR}),\n            ('3', {subtypes.NONE}),\n            (')', {subtypes.NONE}),\n            ('>>', {subtypes.BINARY_OPERATOR}),\n            ('1', {subtypes.NONE}),\n        ],\n    ])\n\n  def testArithmeticOperators(self):\n    code = textwrap.dedent(\"\"\"\\\n        x = ((a + (b - 3) * (1 % c) @ d) / 3) // 1\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('x', {subtypes.NONE}),\n            ('=', {subtypes.ASSIGN_OPERATOR}),\n            ('(', {subtypes.NONE}),\n            ('(', {subtypes.NONE}),\n            ('a', {subtypes.NONE}),\n            ('+', {subtypes.BINARY_OPERATOR}),\n            ('(', {subtypes.NONE}),\n            ('b', {subtypes.NONE}),\n            ('-', {\n                subtypes.BINARY_OPERATOR,\n                subtypes.SIMPLE_EXPRESSION,\n            }),\n            ('3', {subtypes.NONE}),\n            (')', {subtypes.NONE}),\n            ('*', {subtypes.BINARY_OPERATOR}),\n            ('(', {subtypes.NONE}),\n            ('1', {subtypes.NONE}),\n            ('%', {\n                subtypes.BINARY_OPERATOR,\n                subtypes.SIMPLE_EXPRESSION,\n            }),\n            ('c', {subtypes.NONE}),\n            (')', {subtypes.NONE}),\n            ('@', {subtypes.BINARY_OPERATOR}),\n            ('d', {subtypes.NONE}),\n            (')', {subtypes.NONE}),\n            ('/', {subtypes.BINARY_OPERATOR}),\n            ('3', {subtypes.NONE}),\n            (')', {subtypes.NONE}),\n            ('//', {subtypes.BINARY_OPERATOR}),\n            ('1', {subtypes.NONE}),\n        ],\n    ])\n\n  def testSubscriptColon(self):\n    code = textwrap.dedent(\"\"\"\\\n        x[0:42:1]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('x', {subtypes.NONE}),\n            ('[', {subtypes.SUBSCRIPT_BRACKET}),\n            ('0', {subtypes.NONE}),\n            (':', {subtypes.SUBSCRIPT_COLON}),\n            ('42', {subtypes.NONE}),\n            (':', {subtypes.SUBSCRIPT_COLON}),\n            ('1', {subtypes.NONE}),\n            (']', {subtypes.SUBSCRIPT_BRACKET}),\n        ],\n    ])\n\n  def testFunctionCallWithStarExpression(self):\n    code = textwrap.dedent(\"\"\"\\\n        [a, *b]\n    \"\"\")\n    llines = yapf_test_helper.ParseAndUnwrap(code)\n    self._CheckFormatTokenSubtypes(llines, [\n        [\n            ('[', {subtypes.NONE}),\n            ('a', {subtypes.NONE}),\n            (',', {subtypes.NONE}),\n            ('*', {\n                subtypes.UNARY_OPERATOR,\n                subtypes.VARARGS_STAR,\n            }),\n            ('b', {subtypes.NONE}),\n            (']', {subtypes.NONE}),\n        ],\n    ])\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/utils.py",
    "content": "# -*- coding: utf-8 -*-\n# Copyright 2017 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Utilities for tests.\"\"\"\n\nimport contextlib\nimport io\nimport os\nimport sys\nimport tempfile\n\n\n@contextlib.contextmanager\ndef stdout_redirector(stream):  # pylint: disable=invalid-name\n  old_stdout = sys.stdout\n  sys.stdout = stream\n  try:\n    yield\n  finally:\n    sys.stdout = old_stdout\n\n\n# NamedTemporaryFile is useless because on Windows the temporary file would be\n# created with O_TEMPORARY, which would not allow the file to be opened a\n# second time, even by the same process, unless the same flag is used.\n# Thus we provide a simplified version ourselves.\n#\n# Note: returns a tuple of (io.file_obj, file_path), instead of a file_obj with\n# a .name attribute\n#\n# Note: `buffering` is set to -1 despite documentation of NamedTemporaryFile\n# says None. This is probably a problem with the python documentation.\n@contextlib.contextmanager\ndef NamedTempFile(mode='w+b',\n                  buffering=-1,\n                  encoding=None,\n                  errors=None,\n                  newline=None,\n                  suffix=None,\n                  prefix=None,\n                  dirname=None,\n                  text=False):\n  \"\"\"Context manager creating a new temporary file in text mode.\"\"\"\n  (fd, fname) = tempfile.mkstemp(\n      suffix=suffix, prefix=prefix, dir=dirname, text=text)\n  f = io.open(\n      fd,\n      mode=mode,\n      buffering=buffering,\n      encoding=encoding,\n      errors=errors,\n      newline=newline)\n  yield f, fname\n  f.close()\n  os.remove(fname)\n\n\n@contextlib.contextmanager\ndef TempFileContents(dirname,\n                     contents,\n                     encoding='utf-8',\n                     newline='',\n                     suffix=None):\n  # Note: NamedTempFile properly handles unicode encoding when using mode='w'\n  with NamedTempFile(\n      dirname=dirname,\n      mode='w',\n      encoding=encoding,\n      newline=newline,\n      suffix=suffix) as (f, fname):\n    f.write(contents)\n    f.flush()\n    yield fname\n"
  },
  {
    "path": "yapftests/yapf_test.py",
    "content": "# -*- coding: utf-8 -*-\n# Copyright 2015 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Tests for yapf.yapf.\"\"\"\n\nimport io\nimport logging\nimport os\nimport shutil\nimport subprocess\nimport sys\nimport tempfile\nimport textwrap\nimport unittest\nfrom io import StringIO\n\nfrom yapf_third_party._ylib2to3.pgen2 import tokenize\n\nfrom yapf.yapflib import errors\nfrom yapf.yapflib import style\nfrom yapf.yapflib import yapf_api\n\nfrom yapftests import utils\nfrom yapftests import yapf_test_helper\n\nYAPF_BINARY = [sys.executable, '-m', 'yapf', '--no-local-style']\n\n\nclass FormatCodeTest(yapf_test_helper.YAPFTest):\n\n  def _Check(self, unformatted_code, expected_formatted_code):\n    formatted_code, _ = yapf_api.FormatCode(\n        unformatted_code, style_config='yapf')\n    self.assertCodeEqual(expected_formatted_code, formatted_code)\n\n  def testSimple(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        print('foo')\n    \"\"\")\n    self._Check(unformatted_code, unformatted_code)\n\n  def testNoEndingNewline(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          pass\"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n          pass\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n\nclass FormatFileTest(yapf_test_helper.YAPFTest):\n\n  def setUp(self):  # pylint: disable=g-missing-super-call\n    self.test_tmpdir = tempfile.mkdtemp()\n\n  def tearDown(self):  # pylint: disable=g-missing-super-call\n    shutil.rmtree(self.test_tmpdir)\n\n  def testFormatFile(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n         pass\n    \"\"\")\n    expected_formatted_code_pep8 = textwrap.dedent(\"\"\"\\\n        if True:\n            pass\n    \"\"\")\n    expected_formatted_code_yapf = textwrap.dedent(\"\"\"\\\n        if True:\n          pass\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, unformatted_code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(expected_formatted_code_pep8, formatted_code)\n\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf')\n      self.assertCodeEqual(expected_formatted_code_yapf, formatted_code)\n\n  def testDisableLinesPattern(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if a:    b\n\n        # yapf: disable\n        if f:    g\n\n        if h:    i\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if a: b\n\n        # yapf: disable\n        if f:    g\n\n        if h:    i\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, unformatted_code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(expected_formatted_code, formatted_code)\n\n  def testDisableAndReenableLinesPattern(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if a:    b\n\n        # yapf: disable\n        if f:    g\n        # yapf: enable\n\n        if h:    i\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if a: b\n\n        # yapf: disable\n        if f:    g\n        # yapf: enable\n\n        if h: i\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, unformatted_code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(expected_formatted_code, formatted_code)\n\n  def testFmtOnOff(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if a:    b\n\n        # fmt: off\n        if f:    g\n        # fmt: on\n\n        if h:    i\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if a: b\n\n        # fmt: off\n        if f:    g\n        # fmt: on\n\n        if h: i\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, unformatted_code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(expected_formatted_code, formatted_code)\n\n  def testDisablePartOfMultilineComment(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if a:    b\n\n        # This is a multiline comment that disables YAPF.\n        # yapf: disable\n        if f:    g\n        # yapf: enable\n        # This is a multiline comment that enables YAPF.\n\n        if h:    i\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if a: b\n\n        # This is a multiline comment that disables YAPF.\n        # yapf: disable\n        if f:    g\n        # yapf: enable\n        # This is a multiline comment that enables YAPF.\n\n        if h: i\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, unformatted_code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(expected_formatted_code, formatted_code)\n\n    code = textwrap.dedent(\"\"\"\\\n      def foo_function():\n          # some comment\n          # yapf: disable\n\n          foo(\n          bar,\n          baz\n          )\n\n          # yapf: enable\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(code, formatted_code)\n\n  def testEnabledDisabledSameComment(self):\n    code = textwrap.dedent(\"\"\"\\\n        # yapf: disable\n        a(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc, ddddddddddddddddddddddd, eeeeeeeeeeeeeeeeeeeeeeeeeee)\n        # yapf: enable\n        # yapf: disable\n        a(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc, ddddddddddddddddddddddd, eeeeeeeeeeeeeeeeeeeeeeeeeee)\n        # yapf: enable\n    \"\"\")  # noqa\n    with utils.TempFileContents(self.test_tmpdir, code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(code, formatted_code)\n\n  def testFormatFileLinesSelection(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if a:    b\n\n        if f:    g\n\n        if h:    i\n    \"\"\")\n    expected_formatted_code_lines1and2 = textwrap.dedent(\"\"\"\\\n        if a: b\n\n        if f:    g\n\n        if h:    i\n    \"\"\")\n    expected_formatted_code_lines3 = textwrap.dedent(\"\"\"\\\n        if a:    b\n\n        if f: g\n\n        if h:    i\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, unformatted_code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(\n          filepath, style_config='pep8', lines=[(1, 2)])\n      self.assertCodeEqual(expected_formatted_code_lines1and2, formatted_code)\n      formatted_code, _, _ = yapf_api.FormatFile(\n          filepath, style_config='pep8', lines=[(3, 3)])\n      self.assertCodeEqual(expected_formatted_code_lines3, formatted_code)\n\n  def testFormatFileDiff(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n         pass\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, unformatted_code) as filepath:\n      diff, _, _ = yapf_api.FormatFile(filepath, print_diff=True)\n      self.assertIn('+  pass', diff)\n\n  def testFormatFileInPlace(self):\n    unformatted_code = 'True==False\\n'\n    formatted_code = 'True == False\\n'\n    with utils.TempFileContents(self.test_tmpdir, unformatted_code) as filepath:\n      result, _, _ = yapf_api.FormatFile(filepath, in_place=True)\n      self.assertEqual(result, None)\n      with open(filepath) as fd:\n        self.assertCodeEqual(formatted_code, fd.read())\n\n      self.assertRaises(\n          ValueError,\n          yapf_api.FormatFile,\n          filepath,\n          in_place=True,\n          print_diff=True)\n\n  def testNoFile(self):\n    with self.assertRaises(IOError) as context:\n      yapf_api.FormatFile('not_a_file.py')\n\n    self.assertEqual(\n        str(context.exception),\n        \"[Errno 2] No such file or directory: 'not_a_file.py'\")\n\n  def testCommentsUnformatted(self):\n    code = textwrap.dedent(\"\"\"\\\n        foo = [# A list of things\n               # bork\n            'one',\n            # quark\n            'two'] # yapf: disable\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(code, formatted_code)\n\n  def testDisabledHorizontalFormattingOnNewLine(self):\n    code = textwrap.dedent(\"\"\"\\\n        # yapf: disable\n        a = [\n        1]\n        # yapf: enable\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(code, formatted_code)\n\n  def testSplittingSemicolonStatements(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          x = y + 42 ; z = n * 42\n          if True: a += 1 ; b += 1; c += 1\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n            x = y + 42\n            z = n * 42\n            if True:\n                a += 1\n                b += 1\n                c += 1\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, unformatted_code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(expected_formatted_code, formatted_code)\n\n  def testSemicolonStatementsDisabled(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n          x = y + 42 ; z = n * 42  # yapf: disable\n          if True: a += 1 ; b += 1; c += 1\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def f():\n            x = y + 42 ; z = n * 42  # yapf: disable\n            if True:\n                a += 1\n                b += 1\n                c += 1\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, unformatted_code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(expected_formatted_code, formatted_code)\n\n  def testDisabledSemiColonSeparatedStatements(self):\n    code = textwrap.dedent(\"\"\"\\\n        # yapf: disable\n        if True: a ; b\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')\n      self.assertCodeEqual(code, formatted_code)\n\n  def testDisabledMultilineStringInDictionary(self):\n    code = textwrap.dedent(\"\"\"\\\n        # yapf: disable\n\n        A = [\n            {\n                \"aaaaaaaaaaaaaaaaaaa\": '''\n        bbbbbbbbbbb: \"ccccccccccc\"\n        dddddddddddddd: 1\n        eeeeeeee: 0\n        ffffffffff: \"ggggggg\"\n        ''',\n            },\n        ]\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf')\n      self.assertCodeEqual(code, formatted_code)\n\n  def testDisabledWithPrecedingText(self):\n    code = textwrap.dedent(\"\"\"\\\n        # TODO(fix formatting): yapf: disable\n\n        A = [\n            {\n                \"aaaaaaaaaaaaaaaaaaa\": '''\n        bbbbbbbbbbb: \"ccccccccccc\"\n        dddddddddddddd: 1\n        eeeeeeee: 0\n        ffffffffff: \"ggggggg\"\n        ''',\n            },\n        ]\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf')\n      self.assertCodeEqual(code, formatted_code)\n\n  def testCRLFLineEnding(self):\n    code = 'class _():\\r\\n  pass\\r\\n'\n    with utils.TempFileContents(self.test_tmpdir, code) as filepath:\n      formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf')\n      self.assertCodeEqual(code, formatted_code)\n\n\nclass CommandLineTest(yapf_test_helper.YAPFTest):\n  \"\"\"Test how calling yapf from the command line acts.\"\"\"\n\n  @classmethod\n  def setUpClass(cls):  # pylint: disable=g-missing-super-call\n    cls.test_tmpdir = tempfile.mkdtemp()\n\n  @classmethod\n  def tearDownClass(cls):  # pylint: disable=g-missing-super-call\n    shutil.rmtree(cls.test_tmpdir)\n\n  def assertYapfReformats(self,\n                          unformatted,\n                          expected,\n                          extra_options=None,\n                          env=None):\n    \"\"\"Check that yapf reformats the given code as expected.\n\n    Invokes yapf in a subprocess, piping the unformatted code into its stdin.\n    Checks that the formatted output is as expected.\n\n    Arguments:\n      unformatted: unformatted code - input to yapf\n      expected: expected formatted code at the output of yapf\n      extra_options: iterable of extra command-line options to pass to yapf\n      env: dict of environment variables.\n    \"\"\"\n    cmdline = YAPF_BINARY + (extra_options or [])\n    p = subprocess.Popen(\n        cmdline,\n        stdout=subprocess.PIPE,\n        stdin=subprocess.PIPE,\n        stderr=subprocess.PIPE,\n        env=env)\n    reformatted_code, stderrdata = p.communicate(\n        unformatted.encode('utf-8-sig'))\n    self.assertEqual(stderrdata, b'')\n    self.assertMultiLineEqual(reformatted_code.decode('utf-8'), expected)\n\n  def testInPlaceReformatting(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          x = 37\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n            x = 37\n    \"\"\")\n    with utils.TempFileContents(\n        self.test_tmpdir, unformatted_code, suffix='.py') as filepath:\n      p = subprocess.Popen(YAPF_BINARY + ['--in-place', filepath])\n      p.wait()\n      with io.open(filepath, mode='r', newline='') as fd:\n        reformatted_code = fd.read()\n    self.assertEqual(reformatted_code, expected_formatted_code)\n\n  def testInPlaceReformattingBlank(self):\n    unformatted_code = '\\n\\n'\n    expected_formatted_code = '\\n'\n    with utils.TempFileContents(\n        self.test_tmpdir, unformatted_code, suffix='.py') as filepath:\n      p = subprocess.Popen(YAPF_BINARY + ['--in-place', filepath])\n      p.wait()\n      with io.open(filepath, mode='r', encoding='utf-8', newline='') as fd:\n        reformatted_code = fd.read()\n    self.assertEqual(reformatted_code, expected_formatted_code)\n\n  def testInPlaceReformattingWindowsNewLine(self):\n    unformatted_code = '\\r\\n\\r\\n'\n    expected_formatted_code = '\\r\\n'\n    with utils.TempFileContents(\n        self.test_tmpdir, unformatted_code, suffix='.py') as filepath:\n      p = subprocess.Popen(YAPF_BINARY + ['--in-place', filepath])\n      p.wait()\n      with io.open(filepath, mode='r', encoding='utf-8', newline='') as fd:\n        reformatted_code = fd.read()\n    self.assertEqual(reformatted_code, expected_formatted_code)\n\n  def testInPlaceReformattingNoNewLine(self):\n    unformatted_code = textwrap.dedent('def foo(): x = 37')\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n            x = 37\n    \"\"\")\n    with utils.TempFileContents(\n        self.test_tmpdir, unformatted_code, suffix='.py') as filepath:\n      p = subprocess.Popen(YAPF_BINARY + ['--in-place', filepath])\n      p.wait()\n      with io.open(filepath, mode='r', newline='') as fd:\n        reformatted_code = fd.read()\n    self.assertEqual(reformatted_code, expected_formatted_code)\n\n  def testInPlaceReformattingEmpty(self):\n    unformatted_code = ''\n    expected_formatted_code = ''\n    with utils.TempFileContents(\n        self.test_tmpdir, unformatted_code, suffix='.py') as filepath:\n      p = subprocess.Popen(YAPF_BINARY + ['--in-place', filepath])\n      p.wait()\n      with io.open(filepath, mode='r', encoding='utf-8', newline='') as fd:\n        reformatted_code = fd.read()\n    self.assertEqual(reformatted_code, expected_formatted_code)\n\n  def testPrintModified(self):\n    for unformatted_code, has_change in [('1==2', True), ('1 == 2', False)]:\n      with utils.TempFileContents(\n          self.test_tmpdir, unformatted_code, suffix='.py') as filepath:\n        output = subprocess.check_output(\n            YAPF_BINARY + ['--in-place', '--print-modified', filepath],\n            text=True)\n        check = self.assertIn if has_change else self.assertNotIn\n        check(f'Formatted {filepath}', output)\n\n  def testReadFromStdin(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          x = 37\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n            x = 37\n    \"\"\")\n    self.assertYapfReformats(unformatted_code, expected_formatted_code)\n\n  def testReadFromStdinWithEscapedStrings(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        s =   \"foo\\\\nbar\"\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        s = \"foo\\\\nbar\"\n    \"\"\")\n    self.assertYapfReformats(unformatted_code, expected_formatted_code)\n\n  def testSetYapfStyle(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo(): # trail\n            x = 37\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():  # trail\n          x = 37\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--style=yapf'])\n\n  def testSetCustomStyleBasedOnYapf(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo(): # trail\n            x = 37\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():    # trail\n          x = 37\n    \"\"\")\n    style_file = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = yapf\n        spaces_before_comment = 4\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, style_file) as stylepath:\n      self.assertYapfReformats(\n          unformatted_code,\n          expected_formatted_code,\n          extra_options=['--style={0}'.format(stylepath)])\n\n  def testSetCustomStyleSpacesBeforeComment(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a_very_long_statement_that_extends_way_beyond # Comment\n        short # This is a shorter statement\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a_very_long_statement_that_extends_way_beyond # Comment\n        short                                         # This is a shorter statement\n    \"\"\")  # noqa\n    style_file = textwrap.dedent(\"\"\"\\\n        [style]\n        spaces_before_comment = 15, 20\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, style_file) as stylepath:\n      self.assertYapfReformats(\n          unformatted_code,\n          expected_formatted_code,\n          extra_options=['--style={0}'.format(stylepath)])\n\n  def testReadSingleLineCodeFromStdin(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True: pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True: pass\n    \"\"\")\n    self.assertYapfReformats(unformatted_code, expected_formatted_code)\n\n  def testEncodingVerification(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        '''The module docstring.'''\n        # -*- coding: utf-8 -*-\n        def f():\n            x = 37\n    \"\"\")\n\n    with utils.NamedTempFile(\n        suffix='.py', dirname=self.test_tmpdir) as (out, _):\n      with utils.TempFileContents(\n          self.test_tmpdir, unformatted_code, suffix='.py') as filepath:\n        try:\n          subprocess.check_call(YAPF_BINARY + ['--diff', filepath], stdout=out)\n        except subprocess.CalledProcessError as e:\n          # Indicates the text changed.\n          self.assertEqual(e.returncode, 1)  # pylint: disable=g-assert-in-except # noqa\n\n  def testReformattingSpecificLines(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n\n        def g():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n        \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and\n                    xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n\n        def g():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n    \"\"\")  # noqa\n    # TODO(ambv): the `expected_formatted_code` here is not PEP8 compliant,\n    # raising \"E129 visually indented line with same indent as next logical\n    # line\" with flake8.\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-2'])\n\n  def testOmitFormattingLinesBeforeDisabledFunctionComment(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        import sys\n\n        # Comment\n        def some_func(x):\n            x = [\"badly\" , \"formatted\",\"line\" ]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        import sys\n\n        # Comment\n        def some_func(x):\n            x = [\"badly\", \"formatted\", \"line\"]\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '5-5'])\n\n  def testReformattingSkippingLines(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n        # yapf: disable\n        def g():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n        # yapf: enable\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and\n                    xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n\n        # yapf: disable\n        def g():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n        # yapf: enable\n    \"\"\")  # noqa\n    self.assertYapfReformats(unformatted_code, expected_formatted_code)\n\n  def testReformattingSkippingToEndOfFile(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n        # yapf: disable\n        def g():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n        def f():\n            def e():\n                while (xxxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz]) == 'aaaaaaaaaaa' and\n                       xxxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz].aaaaaaaa[0]) ==\n                       'bbbbbbb'):\n                    pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and\n                    xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n\n        # yapf: disable\n        def g():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n        def f():\n            def e():\n                while (xxxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz]) == 'aaaaaaaaaaa' and\n                       xxxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz].aaaaaaaa[0]) ==\n                       'bbbbbbb'):\n                    pass\n    \"\"\")  # noqa\n    self.assertYapfReformats(unformatted_code, expected_formatted_code)\n\n  def testReformattingSkippingSingleLine(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n        def g():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):  # yapf: disable\n                pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and\n                    xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n\n        def g():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):  # yapf: disable\n                pass\n    \"\"\")  # noqa\n    self.assertYapfReformats(unformatted_code, expected_formatted_code)\n\n  def testDisableWholeDataStructure(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        A = set([\n            'hello',\n            'world',\n        ])  # yapf: disable\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        A = set([\n            'hello',\n            'world',\n        ])  # yapf: disable\n    \"\"\")\n    self.assertYapfReformats(unformatted_code, expected_formatted_code)\n\n  def testDisableButAdjustIndentations(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class SplitPenaltyTest(unittest.TestCase):\n\n          def testUnbreakable(self):\n            self._CheckPenalties(tree, [\n            ])  # yapf: disable\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class SplitPenaltyTest(unittest.TestCase):\n\n            def testUnbreakable(self):\n                self._CheckPenalties(tree, [\n                ])  # yapf: disable\n    \"\"\")\n    self.assertYapfReformats(unformatted_code, expected_formatted_code)\n\n  def testRetainingHorizontalWhitespace(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n        def g():\n            if (xxxxxxxxxxxx.yyyyyyyy        (zzzzzzzzzzzzz  [0]) ==     'aaaaaaaaaaa' and    xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):  # yapf: disable\n                pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and\n                    xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n\n        def g():\n            if (xxxxxxxxxxxx.yyyyyyyy        (zzzzzzzzzzzzz  [0]) ==     'aaaaaaaaaaa' and    xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):  # yapf: disable\n                pass\n    \"\"\")  # noqa\n    self.assertYapfReformats(unformatted_code, expected_formatted_code)\n\n  def testRetainingVerticalWhitespace(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n        def g():\n\n\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n\n                pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def h():\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and\n                    xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n                pass\n\n        def g():\n\n\n            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):\n\n                pass\n    \"\"\")  # noqa\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-2'])\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n\n\n        if a:     b\n\n\n        if c:\n            to_much      + indent\n\n            same\n\n\n\n        #comment\n\n        #   trailing whitespace\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if a: b\n\n\n        if c:\n            to_much      + indent\n\n            same\n\n\n\n        #comment\n\n        #   trailing whitespace\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '3-3', '--lines', '13-13'])\n\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        '''\n        docstring\n\n        '''\n\n        import blah\n    \"\"\")\n\n    self.assertYapfReformats(\n        unformatted_code, unformatted_code, extra_options=['--lines', '2-2'])\n\n  def testVerticalSpacingWithCommentWithContinuationMarkers(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        # \\\\\n        # \\\\\n        # \\\\\n\n        x = {\n        }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        # \\\\\n        # \\\\\n        # \\\\\n\n        x = {\n        }\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-1'])\n\n  def testRetainingSemicolonsWhenSpecifyingLines(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a = line_to_format\n        def f():\n            x = y + 42; z = n * 42\n            if True: a += 1 ; b += 1 ; c += 1\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a = line_to_format\n        def f():\n            x = y + 42; z = n * 42\n            if True: a += 1 ; b += 1 ; c += 1\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-1'])\n\n  def testDisabledMultilineStrings(self):\n    unformatted_code = textwrap.dedent('''\\\n        foo=42\n        def f():\n            email_text += \"\"\"<html>This is a really long docstring that goes over the column limit and is multi-line.<br><br>\n        <b>Czar: </b>\"\"\"+despot[\"Nicholas\"]+\"\"\"<br>\n        <b>Minion: </b>\"\"\"+serf[\"Dmitri\"]+\"\"\"<br>\n        <b>Residence: </b>\"\"\"+palace[\"Winter\"]+\"\"\"<br>\n        </body>\n        </html>\"\"\"\n    ''')  # noqa\n    expected_formatted_code = textwrap.dedent('''\\\n        foo = 42\n        def f():\n            email_text += \"\"\"<html>This is a really long docstring that goes over the column limit and is multi-line.<br><br>\n        <b>Czar: </b>\"\"\"+despot[\"Nicholas\"]+\"\"\"<br>\n        <b>Minion: </b>\"\"\"+serf[\"Dmitri\"]+\"\"\"<br>\n        <b>Residence: </b>\"\"\"+palace[\"Winter\"]+\"\"\"<br>\n        </body>\n        </html>\"\"\"\n    ''')  # noqa\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-1'])\n\n  def testDisableWhenSpecifyingLines(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        # yapf: disable\n        A = set([\n            'hello',\n            'world',\n        ])\n        # yapf: enable\n        B = set([\n            'hello',\n            'world',\n        ])  # yapf: disable\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        # yapf: disable\n        A = set([\n            'hello',\n            'world',\n        ])\n        # yapf: enable\n        B = set([\n            'hello',\n            'world',\n        ])  # yapf: disable\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-10'])\n\n  def testDisableFormattingInDataLiteral(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def horrible():\n          oh_god()\n          why_would_you()\n          [\n             'do',\n\n              'that',\n          ]\n\n        def still_horrible():\n            oh_god()\n            why_would_you()\n            [\n                'do',\n\n                'that'\n            ]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def horrible():\n            oh_god()\n            why_would_you()\n            [\n               'do',\n\n                'that',\n            ]\n\n        def still_horrible():\n            oh_god()\n            why_would_you()\n            ['do', 'that']\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '14-15'])\n\n  def testRetainVerticalFormattingBetweenDisabledAndEnabledLines(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class A(object):\n            def aaaaaaaaaaaaa(self):\n                c = bbbbbbbbb.ccccccccc('challenge', 0, 1, 10)\n                self.assertEqual(\n                    ('ddddddddddddddddddddddddd',\n             'eeeeeeeeeeeeeeeeeeeeeeeee.%s' %\n                     c.ffffffffffff),\n             gggggggggggg.hhhhhhhhh(c, c.ffffffffffff))\n                iiiii = jjjjjjjjjjjjjj.iiiii\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class A(object):\n            def aaaaaaaaaaaaa(self):\n                c = bbbbbbbbb.ccccccccc('challenge', 0, 1, 10)\n                self.assertEqual(('ddddddddddddddddddddddddd',\n                                  'eeeeeeeeeeeeeeeeeeeeeeeee.%s' % c.ffffffffffff),\n                                 gggggggggggg.hhhhhhhhh(c, c.ffffffffffff))\n                iiiii = jjjjjjjjjjjjjj.iiiii\n    \"\"\")  # noqa\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '4-7'])\n\n  def testRetainVerticalFormattingBetweenDisabledLines(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class A(object):\n            def aaaaaaaaaaaaa(self):\n                pass\n\n\n            def bbbbbbbbbbbbb(self):  # 5\n                pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class A(object):\n            def aaaaaaaaaaaaa(self):\n                pass\n\n\n            def bbbbbbbbbbbbb(self):  # 5\n                pass\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '4-4'])\n\n  def testFormatLinesSpecifiedInMiddleOfExpression(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        class A(object):\n            def aaaaaaaaaaaaa(self):\n                c = bbbbbbbbb.ccccccccc('challenge', 0, 1, 10)\n                self.assertEqual(\n                    ('ddddddddddddddddddddddddd',\n             'eeeeeeeeeeeeeeeeeeeeeeeee.%s' %\n                     c.ffffffffffff),\n             gggggggggggg.hhhhhhhhh(c, c.ffffffffffff))\n                iiiii = jjjjjjjjjjjjjj.iiiii\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        class A(object):\n            def aaaaaaaaaaaaa(self):\n                c = bbbbbbbbb.ccccccccc('challenge', 0, 1, 10)\n                self.assertEqual(('ddddddddddddddddddddddddd',\n                                  'eeeeeeeeeeeeeeeeeeeeeeeee.%s' % c.ffffffffffff),\n                                 gggggggggggg.hhhhhhhhh(c, c.ffffffffffff))\n                iiiii = jjjjjjjjjjjjjj.iiiii\n    \"\"\")  # noqa\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '5-6'])\n\n  def testCommentFollowingMultilineString(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n            '''First line.\n            Second line.\n            '''  # comment\n            x = '''hello world'''  # second comment\n            return 42  # another comment\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n            '''First line.\n            Second line.\n            '''  # comment\n            x = '''hello world'''  # second comment\n            return 42  # another comment\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-1'])\n\n  def testDedentClosingBracket(self):\n    # no line-break on the first argument, not dedenting closing brackets\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(first_argument_on_the_same_line,\n        second_argument_makes_the_line_too_long):\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(first_argument_on_the_same_line,\n                                      second_argument_makes_the_line_too_long):\n            pass\n    \"\"\")  # noqa\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--style=pep8'])\n\n    # TODO(ambv): currently the following produces the closing bracket on a new\n    # line but indented to the opening bracket which is the worst of both\n    # worlds. Expected behaviour would be to format as --style=pep8 does in\n    # this case.\n    # self.assertYapfReformats(unformatted_code, expected_formatted_code,\n    #                          extra_options=['--style=facebook'])\n\n    # line-break before the first argument, dedenting closing brackets if set\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(\n          first_argument_on_the_same_line,\n          second_argument_makes_the_line_too_long):\n          pass\n    \"\"\")\n    # expected_formatted_pep8_code = textwrap.dedent(\"\"\"\\\n    #   def overly_long_function_name(\n    #           first_argument_on_the_same_line,\n    #           second_argument_makes_the_line_too_long):\n    #       pass\n    # \"\"\")\n    expected_formatted_fb_code = textwrap.dedent(\"\"\"\\\n        def overly_long_function_name(\n            first_argument_on_the_same_line, second_argument_makes_the_line_too_long\n        ):\n            pass\n    \"\"\")  # noqa\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_fb_code,\n        extra_options=['--style=facebook'])\n    # TODO(ambv): currently the following produces code that is not PEP8\n    # compliant, raising \"E125 continuation line with same indent as next\n    # logical line\" with flake8. Expected behaviour for PEP8 would be to use\n    # double-indentation here.\n    # self.assertYapfReformats(unformatted_code, expected_formatted_pep8_code,\n    #                          extra_options=['--style=pep8'])\n\n  def testCoalesceBrackets(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n       some_long_function_name_foo(\n           {\n               'first_argument_of_the_thing': id,\n               'second_argument_of_the_thing': \"some thing\"\n           }\n       )\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n       some_long_function_name_foo({\n           'first_argument_of_the_thing': id,\n           'second_argument_of_the_thing': \"some thing\"\n       })\n    \"\"\")\n    with utils.NamedTempFile(dirname=self.test_tmpdir, mode='w') as (f, name):\n      f.write(\n          textwrap.dedent(\"\"\"\\\n          [style]\n          column_limit=82\n          coalesce_brackets = True\n      \"\"\"))\n      f.flush()\n      self.assertYapfReformats(\n          unformatted_code,\n          expected_formatted_code,\n          extra_options=['--style={0}'.format(name)])\n\n  def testPseudoParenSpaces(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def   foo():\n          def bar():\n            return {msg_id: author for author, msg_id in reader}\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo():\n          def bar():\n            return {msg_id: author for author, msg_id in reader}\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-1', '--style', 'yapf'])\n\n  def testMultilineCommentFormattingDisabled(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        # This is a comment\n        FOO = {\n            aaaaaaaa.ZZZ: [\n                bbbbbbbbbb.Pop(),\n                # Multiline comment.\n                # Line two.\n                bbbbbbbbbb.Pop(),\n            ],\n            'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx':\n                ('yyyyy', zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz),\n            '#': lambda x: x  # do nothing\n        }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        # This is a comment\n        FOO = {\n            aaaaaaaa.ZZZ: [\n                bbbbbbbbbb.Pop(),\n                # Multiline comment.\n                # Line two.\n                bbbbbbbbbb.Pop(),\n            ],\n            'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx':\n                ('yyyyy', zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz),\n            '#': lambda x: x  # do nothing\n        }\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-1', '--style', 'yapf'])\n\n  def testTrailingCommentsWithDisabledFormatting(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        import os\n\n        SCOPES = [\n            'hello world'  # This is a comment.\n        ]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        import os\n\n        SCOPES = [\n            'hello world'  # This is a comment.\n        ]\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-1', '--style', 'yapf'])\n\n  def testUseTabs(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo_function():\n         if True:\n          pass\n    \"\"\")\n    expected_formatted_code = \"\"\"\\\ndef foo_function():\n\tif True:\n\t\tpass\n\"\"\"  # noqa: W191,E101\n    style_contents = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = yapf\n        use_tabs = true\n        indent_width = 1\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, style_contents) as stylepath:\n      self.assertYapfReformats(\n          unformatted_code,\n          expected_formatted_code,\n          extra_options=['--style={0}'.format(stylepath)])\n\n  def testUseTabsWith(self):\n    unformatted_code = \"\"\"\\\ndef f():\n  return ['hello', 'world',]\n\"\"\"\n    expected_formatted_code = \"\"\"\\\ndef f():\n\treturn [\n\t    'hello',\n\t    'world',\n\t]\n\"\"\"  # noqa: W191,E101\n    style_contents = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = yapf\n        use_tabs = true\n        indent_width = 1\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, style_contents) as stylepath:\n      self.assertYapfReformats(\n          unformatted_code,\n          expected_formatted_code,\n          extra_options=['--style={0}'.format(stylepath)])\n\n  def testUseTabsContinuationAlignStyleFixed(self):\n    unformatted_code = \"\"\"\\\ndef foo_function(arg1, arg2, arg3):\n  return ['hello', 'world',]\n\"\"\"\n    expected_formatted_code = \"\"\"\\\ndef foo_function(\n\t\targ1, arg2, arg3):\n\treturn [\n\t\t\t'hello',\n\t\t\t'world',\n\t]\n\"\"\"  # noqa: W191,E101\n    style_contents = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = yapf\n        use_tabs = true\n        column_limit=32\n        indent_width=4\n        continuation_indent_width=8\n        continuation_align_style = fixed\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, style_contents) as stylepath:\n      self.assertYapfReformats(\n          unformatted_code,\n          expected_formatted_code,\n          extra_options=['--style={0}'.format(stylepath)])\n\n  def testUseTabsContinuationAlignStyleVAlignRight(self):\n    unformatted_code = \"\"\"\\\ndef foo_function(arg1, arg2, arg3):\n  return ['hello', 'world',]\n\"\"\"\n    expected_formatted_code = \"\"\"\\\ndef foo_function(arg1, arg2,\n\t\t\t\t\targ3):\n\treturn [\n\t\t\t'hello',\n\t\t\t'world',\n\t]\n\"\"\"  # noqa: W191,E101\n    style_contents = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = yapf\n        use_tabs = true\n        column_limit = 32\n        indent_width = 4\n        continuation_indent_width = 8\n        continuation_align_style = valign-right\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, style_contents) as stylepath:\n      self.assertYapfReformats(\n          unformatted_code,\n          expected_formatted_code,\n          extra_options=['--style={0}'.format(stylepath)])\n\n  def testUseSpacesContinuationAlignStyleFixed(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo_function(arg1, arg2, arg3):\n          return ['hello', 'world',]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo_function(\n                arg1, arg2, arg3):\n            return [\n                    'hello',\n                    'world',\n            ]\n    \"\"\")\n    style_contents = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = yapf\n        column_limit = 32\n        indent_width = 4\n        continuation_indent_width = 8\n        continuation_align_style = fixed\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, style_contents) as stylepath:\n      self.assertYapfReformats(\n          unformatted_code,\n          expected_formatted_code,\n          extra_options=['--style={0}'.format(stylepath)])\n\n  def testUseSpacesContinuationAlignStyleVAlignRight(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo_function(arg1, arg2, arg3):\n          return ['hello', 'world',]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo_function(arg1, arg2,\n                            arg3):\n            return [\n                    'hello',\n                    'world',\n            ]\n    \"\"\")\n    style_contents = textwrap.dedent(\"\"\"\\\n        [style]\n        based_on_style = yapf\n        column_limit = 32\n        indent_width = 4\n        continuation_indent_width = 8\n        continuation_align_style = valign-right\n    \"\"\")\n    with utils.TempFileContents(self.test_tmpdir, style_contents) as stylepath:\n      self.assertYapfReformats(\n          unformatted_code,\n          expected_formatted_code,\n          extra_options=['--style={0}'.format(stylepath)])\n\n  def testStyleOutputRoundTrip(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def foo_function():\n          pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def foo_function():\n            pass\n    \"\"\")\n\n    with utils.NamedTempFile(dirname=self.test_tmpdir) as (stylefile,\n                                                           stylepath):\n      p = subprocess.Popen(\n          YAPF_BINARY + ['--style-help'],\n          stdout=stylefile,\n          stdin=subprocess.PIPE,\n          stderr=subprocess.PIPE)\n      _, stderrdata = p.communicate()\n      self.assertEqual(stderrdata, b'')\n      self.assertYapfReformats(\n          unformatted_code,\n          expected_formatted_code,\n          extra_options=['--style={0}'.format(stylepath)])\n\n  def testSpacingBeforeComments(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        A = 42\n\n\n        # A comment\n        def x():\n            pass\n        def _():\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        A = 42\n\n\n        # A comment\n        def x():\n            pass\n        def _():\n            pass\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-2'])\n\n  def testSpacingBeforeCommentsInDicts(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        A=42\n\n        X = {\n            # 'Valid' statuses.\n            PASSED:  # Passed\n                'PASSED',\n            FAILED:  # Failed\n                'FAILED',\n            TIMED_OUT:  # Timed out.\n                'FAILED',\n            BORKED:  # Broken.\n                'BROKEN'\n        }\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        A = 42\n\n        X = {\n            # 'Valid' statuses.\n            PASSED:  # Passed\n                'PASSED',\n            FAILED:  # Failed\n                'FAILED',\n            TIMED_OUT:  # Timed out.\n                'FAILED',\n            BORKED:  # Broken.\n                'BROKEN'\n        }\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--style', 'yapf', '--lines', '1-1'])\n\n  def testDisableWithLinesOption(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        # yapf_lines_bug.py\n        # yapf: disable\n        def outer_func():\n            def inner_func():\n                return\n            return\n        # yapf: enable\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        # yapf_lines_bug.py\n        # yapf: disable\n        def outer_func():\n            def inner_func():\n                return\n            return\n        # yapf: enable\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--lines', '1-8'])\n\n  def testDisableWithLineRanges(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        # yapf: disable\n        a = [\n            1,\n            2,\n\n            3\n        ]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        # yapf: disable\n        a = [\n            1,\n            2,\n\n            3\n        ]\n    \"\"\")\n    self.assertYapfReformats(\n        unformatted_code,\n        expected_formatted_code,\n        extra_options=['--style', 'yapf', '--lines', '1-100'])\n\n\nclass BadInputTest(yapf_test_helper.YAPFTest):\n  \"\"\"Test yapf's behaviour when passed bad input.\"\"\"\n\n  def testBadSyntax(self):\n    code = '  a = 1\\n'\n    self.assertRaises(errors.YapfError, yapf_api.FormatCode, code)\n\n  def testBadCode(self):\n    code = 'x = \"\"\"hello\\n'\n    self.assertRaises(errors.YapfError, yapf_api.FormatCode, code)\n\n\nclass DiffIndentTest(yapf_test_helper.YAPFTest):\n\n  @staticmethod\n  def _OwnStyle():\n    my_style = style.CreatePEP8Style()\n    my_style['INDENT_WIDTH'] = 3\n    my_style['CONTINUATION_INDENT_WIDTH'] = 3\n    return my_style\n\n  def _Check(self, unformatted_code, expected_formatted_code):\n    formatted_code, _ = yapf_api.FormatCode(\n        unformatted_code, style_config=style.SetGlobalStyle(self._OwnStyle()))\n    self.assertEqual(expected_formatted_code, formatted_code)\n\n  def testSimple(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        for i in range(5):\n         print('bar')\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        for i in range(5):\n           print('bar')\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n\nclass HorizontallyAlignedTrailingCommentsTest(yapf_test_helper.YAPFTest):\n\n  @staticmethod\n  def _OwnStyle():\n    my_style = style.CreatePEP8Style()\n    my_style['SPACES_BEFORE_COMMENT'] = [\n        15,\n        25,\n        35,\n    ]\n    return my_style\n\n  def _Check(self, unformatted_code, expected_formatted_code):\n    formatted_code, _ = yapf_api.FormatCode(\n        unformatted_code, style_config=style.SetGlobalStyle(self._OwnStyle()))\n    self.assertCodeEqual(expected_formatted_code, formatted_code)\n\n  def testSimple(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        foo = '1' # Aligned at first list value\n\n        foo = '2__<15>' # Aligned at second list value\n\n        foo = '3____________<25>' # Aligned at third list value\n\n        foo = '4______________________<35>' # Aligned beyond list values\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        foo = '1'     # Aligned at first list value\n\n        foo = '2__<15>'         # Aligned at second list value\n\n        foo = '3____________<25>'         # Aligned at third list value\n\n        foo = '4______________________<35>' # Aligned beyond list values\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n  def testBlock(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        func(1)     # Line 1\n        func(2) # Line 2\n        # Line 3\n        func(3)                             # Line 4\n                                            # Line 5\n                                            # Line 6\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        func(1)       # Line 1\n        func(2)       # Line 2\n                      # Line 3\n        func(3)       # Line 4\n                      # Line 5\n                      # Line 6\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n  def testBlockWithLongLine(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        func(1)     # Line 1\n        func___________________(2) # Line 2\n        # Line 3\n        func(3)                             # Line 4\n                                            # Line 5\n                                            # Line 6\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        func(1)                           # Line 1\n        func___________________(2)        # Line 2\n                                          # Line 3\n        func(3)                           # Line 4\n                                          # Line 5\n                                          # Line 6\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n  def testBlockFuncSuffix(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        func(1)     # Line 1\n        func(2) # Line 2\n        # Line 3\n        func(3)                             # Line 4\n                                        # Line 5\n                                    # Line 6\n\n        def Func():\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        func(1)       # Line 1\n        func(2)       # Line 2\n                      # Line 3\n        func(3)       # Line 4\n                      # Line 5\n                      # Line 6\n\n\n        def Func():\n            pass\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n  def testBlockCommentSuffix(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        func(1)     # Line 1\n        func(2) # Line 2\n        # Line 3\n        func(3)                             # Line 4\n                                        # Line 5 - SpliceComments makes this part of the previous block\n                                    # Line 6\n\n                                            # Aligned with prev comment block\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        func(1)       # Line 1\n        func(2)       # Line 2\n                      # Line 3\n        func(3)       # Line 4\n                      # Line 5 - SpliceComments makes this part of the previous block\n                      # Line 6\n\n                      # Aligned with prev comment block\n    \"\"\")  # noqa\n    self._Check(unformatted_code, expected_formatted_code)\n\n  def testBlockIndentedFuncSuffix(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            func(1)     # Line 1\n            func(2) # Line 2\n            # Line 3\n            func(3)                             # Line 4\n                                                # Line 5 - SpliceComments makes this a new block\n                                                # Line 6\n\n                                                # Aligned with Func\n\n            def Func():\n                pass\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            func(1)   # Line 1\n            func(2)   # Line 2\n                      # Line 3\n            func(3)   # Line 4\n\n            # Line 5 - SpliceComments makes this a new block\n            # Line 6\n\n            # Aligned with Func\n\n\n            def Func():\n                pass\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n  def testBlockIndentedCommentSuffix(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            func(1)     # Line 1\n            func(2) # Line 2\n            # Line 3\n            func(3)                             # Line 4\n                                                # Line 5\n                                                # Line 6\n\n                                                # Not aligned\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            func(1)   # Line 1\n            func(2)   # Line 2\n                      # Line 3\n            func(3)   # Line 4\n                      # Line 5\n                      # Line 6\n\n            # Not aligned\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n  def testBlockMultiIndented(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            if True:\n                if True:\n                    func(1)     # Line 1\n                    func(2) # Line 2\n                    # Line 3\n                    func(3)                             # Line 4\n                                                        # Line 5\n                                                        # Line 6\n\n                                                        # Not aligned\n    \"\"\")  # noqa\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        if True:\n            if True:\n                if True:\n                    func(1)     # Line 1\n                    func(2)     # Line 2\n                                # Line 3\n                    func(3)     # Line 4\n                                # Line 5\n                                # Line 6\n\n                    # Not aligned\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n  def testArgs(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        def MyFunc(\n            arg1,   # Desc 1\n            arg2,   # Desc 2\n            a_longer_var_name,  # Desc 3\n            arg4,\n            arg5,   # Desc 5\n            arg6,\n        ):\n            pass\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        def MyFunc(\n            arg1,               # Desc 1\n            arg2,               # Desc 2\n            a_longer_var_name,  # Desc 3\n            arg4,\n            arg5,               # Desc 5\n            arg6,\n        ):\n            pass\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n  def testDisableBlock(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        a() # comment 1\n        b() # comment 2\n\n        # yapf: disable\n        c() # comment 3\n        d()   # comment 4\n        # yapf: enable\n\n        e() # comment 5\n        f() # comment 6\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        a()           # comment 1\n        b()           # comment 2\n\n        # yapf: disable\n        c() # comment 3\n        d()   # comment 4\n        # yapf: enable\n\n        e()           # comment 5\n        f()           # comment 6\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n  def testDisabledLine(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        short # comment 1\n        do_not_touch1 # yapf: disable\n        do_not_touch2   # yapf: disable\n        a_longer_statement # comment 2\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        short                   # comment 1\n        do_not_touch1 # yapf: disable\n        do_not_touch2   # yapf: disable\n        a_longer_statement      # comment 2\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n\nclass _SpacesAroundDictListTupleTestImpl(yapf_test_helper.YAPFTest):\n\n  @staticmethod\n  def _OwnStyle():\n    my_style = style.CreatePEP8Style()\n    my_style['DISABLE_ENDING_COMMA_HEURISTIC'] = True\n    my_style['SPLIT_ALL_COMMA_SEPARATED_VALUES'] = False\n    my_style['SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED'] = False\n    return my_style\n\n  def _Check(self, unformatted_code, expected_formatted_code):\n    formatted_code, _ = yapf_api.FormatCode(\n        unformatted_code, style_config=style.SetGlobalStyle(self._OwnStyle()))\n    self.assertEqual(expected_formatted_code, formatted_code)\n\n  def setUp(self):\n    self.maxDiff = None\n\n\nclass SpacesAroundDictTest(_SpacesAroundDictListTupleTestImpl):\n\n  @classmethod\n  def _OwnStyle(cls):\n    style = super(SpacesAroundDictTest, cls)._OwnStyle()\n    style['SPACES_AROUND_DICT_DELIMITERS'] = True\n\n    return style\n\n  def testStandard(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        {1 : 2}\n        {k:v for k, v in other.items()}\n        {k for k in [1, 2, 3]}\n\n        # The following statements should not change\n        {}\n        {1 : 2} # yapf: disable\n\n        # yapf: disable\n        {1 : 2}\n        # yapf: enable\n\n        # Dict settings should not impact lists or tuples\n        [1, 2]\n        (3, 4)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        { 1: 2 }\n        { k: v for k, v in other.items() }\n        { k for k in [1, 2, 3] }\n\n        # The following statements should not change\n        {}\n        {1 : 2} # yapf: disable\n\n        # yapf: disable\n        {1 : 2}\n        # yapf: enable\n\n        # Dict settings should not impact lists or tuples\n        [1, 2]\n        (3, 4)\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n\nclass SpacesAroundListTest(_SpacesAroundDictListTupleTestImpl):\n\n  @classmethod\n  def _OwnStyle(cls):\n    style = super(SpacesAroundListTest, cls)._OwnStyle()\n    style['SPACES_AROUND_LIST_DELIMITERS'] = True\n\n    return style\n\n  def testStandard(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        [a,b,c]\n        [4,5,]\n        [6, [7, 8], 9]\n        [v for v in [1,2,3] if v & 1]\n\n        # The following statements should not change\n        index[0]\n        index[a, b]\n        []\n        [v for v in [1,2,3] if v & 1] # yapf: disable\n\n        # yapf: disable\n        [a,b,c]\n        [4,5,]\n        # yapf: enable\n\n        # List settings should not impact dicts or tuples\n        {a: b}\n        (1, 2)\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        [ a, b, c ]\n        [ 4, 5, ]\n        [ 6, [ 7, 8 ], 9 ]\n        [ v for v in [ 1, 2, 3 ] if v & 1 ]\n\n        # The following statements should not change\n        index[0]\n        index[a, b]\n        []\n        [v for v in [1,2,3] if v & 1] # yapf: disable\n\n        # yapf: disable\n        [a,b,c]\n        [4,5,]\n        # yapf: enable\n\n        # List settings should not impact dicts or tuples\n        {a: b}\n        (1, 2)\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n\nclass SpacesAroundTupleTest(_SpacesAroundDictListTupleTestImpl):\n\n  @classmethod\n  def _OwnStyle(cls):\n    style = super(SpacesAroundTupleTest, cls)._OwnStyle()\n    style['SPACES_AROUND_TUPLE_DELIMITERS'] = True\n\n    return style\n\n  def testStandard(self):\n    unformatted_code = textwrap.dedent(\"\"\"\\\n        (0, 1)\n        (2, 3)\n        (4, 5, 6,)\n        func((7, 8), 9)\n\n        # The following statements should not change\n        func(1, 2)\n        (this_func or that_func)(3, 4)\n        if (True and False): pass\n        ()\n\n        (0, 1) # yapf: disable\n\n        # yapf: disable\n        (0, 1)\n        (2, 3)\n        # yapf: enable\n\n        # Tuple settings should not impact dicts or lists\n        {a: b}\n        [3, 4]\n    \"\"\")\n    expected_formatted_code = textwrap.dedent(\"\"\"\\\n        ( 0, 1 )\n        ( 2, 3 )\n        ( 4, 5, 6, )\n        func(( 7, 8 ), 9)\n\n        # The following statements should not change\n        func(1, 2)\n        (this_func or that_func)(3, 4)\n        if (True and False): pass\n        ()\n\n        (0, 1) # yapf: disable\n\n        # yapf: disable\n        (0, 1)\n        (2, 3)\n        # yapf: enable\n\n        # Tuple settings should not impact dicts or lists\n        {a: b}\n        [3, 4]\n    \"\"\")\n    self._Check(unformatted_code, expected_formatted_code)\n\n\nif __name__ == '__main__':\n  unittest.main()\n"
  },
  {
    "path": "yapftests/yapf_test_helper.py",
    "content": "# Copyright 2016 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Support module for tests for yapf.\"\"\"\n\nimport difflib\nimport sys\nimport unittest\n\nfrom yapf.pytree import blank_line_calculator\nfrom yapf.pytree import comment_splicer\nfrom yapf.pytree import continuation_splicer\nfrom yapf.pytree import pytree_unwrapper\nfrom yapf.pytree import pytree_utils\nfrom yapf.pytree import pytree_visitor\nfrom yapf.pytree import split_penalty\nfrom yapf.pytree import subtype_assigner\nfrom yapf.yapflib import identify_container\nfrom yapf.yapflib import style\n\n\nclass YAPFTest(unittest.TestCase):\n\n  def __init__(self, *args):\n    super(YAPFTest, self).__init__(*args)\n\n  def assertCodeEqual(self, expected_code, code):\n    if code != expected_code:\n      msg = ['Code format mismatch:', 'Expected:']\n      linelen = style.Get('COLUMN_LIMIT')\n      for line in expected_code.splitlines():\n        if len(line) > linelen:\n          msg.append('!> %s' % line)\n        else:\n          msg.append(' > %s' % line)\n      msg.append('Actual:')\n      for line in code.splitlines():\n        if len(line) > linelen:\n          msg.append('!> %s' % line)\n        else:\n          msg.append(' > %s' % line)\n      msg.append('Diff:')\n      msg.extend(\n          difflib.unified_diff(\n              code.splitlines(),\n              expected_code.splitlines(),\n              fromfile='actual',\n              tofile='expected',\n              lineterm=''))\n      self.fail('\\n'.join(msg))\n\n\ndef ParseAndUnwrap(code, dumptree=False):\n  \"\"\"Produces logical lines from the given code.\n\n  Parses the code into a tree, performs comment splicing and runs the\n  unwrapper.\n\n  Arguments:\n    code: code to parse as a string\n    dumptree: if True, the parsed pytree (after comment splicing) is dumped\n              to stderr. Useful for debugging.\n\n  Returns:\n    List of logical lines.\n  \"\"\"\n  tree = pytree_utils.ParseCodeToTree(code)\n  comment_splicer.SpliceComments(tree)\n  continuation_splicer.SpliceContinuations(tree)\n  subtype_assigner.AssignSubtypes(tree)\n  identify_container.IdentifyContainers(tree)\n  split_penalty.ComputeSplitPenalties(tree)\n  blank_line_calculator.CalculateBlankLines(tree)\n\n  if dumptree:\n    pytree_visitor.DumpPyTree(tree, target_stream=sys.stderr)\n\n  llines = pytree_unwrapper.UnwrapPyTree(tree)\n  for lline in llines:\n    lline.CalculateFormattingInformation()\n\n  return llines\n"
  }
]