1

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?

jtso
  • 21
  • 3
  • this isn't reproducible -- to be able to help you with your problem you need to show a minimal set of code so we can see what you're seeing. in this case you need *at least* a python file and your configuration in your question – anthony sottile Dec 08 '21 at 17:13
  • @AnthonySottile sorry about that, just added an explicit python file example along with my configuration. – jtso Dec 15 '21 at 18:39
  • you've got a typo, you want `args: [--style, google]` – anthony sottile Dec 15 '21 at 19:42
  • @AnthonySottile Hmm, unfortunately it doesn't seem to work still. Changing it to that still just gives me `Passed` – jtso Dec 16 '21 at 19:54
  • I can't reproduce, when I formatted `args` correctly it does fix the code you've provided in this post – anthony sottile Dec 16 '21 at 20:58
  • After some testing, I've found that `args: [--style, arbname]` always results in a `Passed`. I'm wondering if that means that, for whatever reason on my end, it is not recognizing `google` as a valid style? I also tried `pep8` unsuccessfully as well, even though I believe it defaults to pep8. – jtso Dec 16 '21 at 23:50

1 Answers1

0

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
Alex Barrett
  • 31
  • 1
  • 1
  • 4