pre-commit hook yapf returns different results than running yapf in the command line

1.5k Views Asked by At

When running over a file using the command line and yapf, my tags are the following:

-i --verbose --style "google"

When using the same above as args for pre-commit, my pre-commit hook always returns "Pass".

This was tested against the same file for the same changes, so I would have expected similar results. If I exclude --style "google", my pre-commit hook will at least change the format of my file, but not to the style that I want it to.

Can someone tell me what I am doing wrong with the args?

Python File that contains an example:

def hello_world():
    print("hello world")




    if 5 == 5: print("goodbye world")

.pre-commit-config.yaml file:

  - repo: https://github.com/pre-commit/pre-commit-hooks.git
    sha: v4.0.1
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer

  - repo: https://github.com/google/yapf
    rev: v0.31.0
    hooks:
      - id: yapf
        name: "yapf"

On commit, my file will change and pre-commit has told me yapf has changed my file to the following:

def hello_world():
    print("hello world")

    if 5 == 5: print("goodbye world")

If I go back to the same python file and update my .pre-commit-config.yaml file to this:

  - repo: https://github.com/pre-commit/pre-commit-hooks.git
    sha: v4.0.1
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer

  - repo: https://github.com/google/yapf
    rev: v0.31.0
    hooks:
      - id: yapf
        name: "yapf"
        args: [--style "google" ]

Running a commit will provide Pass instead of making any changes, even the ones from before

Edit 1: The .pre-commit.config.yaml file was updated to:

  - repo: https://github.com/pre-commit/pre-commit-hooks.git
    sha: v4.0.1
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
  - repo: https://github.com/google/yapf
    rev: v0.31.0
    hooks:
      - id: yapf
        name: "yapf"
        args: [--style, google]

Running pre-commit run only shows Passed instead of reformatting. I've also tried putting in pep8, and other arbitrary words as a replacement for google. These all result in Passed. Maybe there is something on my end where the style arg is being ignored and causing all of yapf to fail?

1

There are 1 best solutions below

0
On

This post was from a while ago, but for anyone else in the future reading it, but I was able to control the yapf style in pre-commit with a .style.yapf in my parent directory, as outlined in the yapf documentation: https://github.com/google/yapf

This was the .style.yapf I used

[style]
based_on_style = google