pre-commit yapf fails on file included in yapfignore

1.2k Views Asked by At

One of the pre-commit hooks set up in the repo is yapf (.pre-commit-config.yaml):

repos:
# Note: the YAPF config is found in `.style.yapf` and `.yapfignore`
-   repo: https://github.com/pre-commit/mirrors-yapf
    rev: v0.29.0
    hooks:
    -   id: yapf

Whenever I make changes to a file that is included in .yapfignore and run the pre-commit hook, the hook fails:

pre-commit run yapf                                                                                                                                                                                         
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to ****************
yapf.....................................................................Failed
- hook id: yapf
- exit code: 1

yapf: Input filenames did not match any python files

Anyone know how to avoid yapf failing on the files included in the .yapfignore?

2

There are 2 best solutions below

0
On BEST ANSWER

Ok so the cause of the problem is that yapf treats files that are in the .yapfignore as if they're files that don't exist so you'll get a:

yapf: Input filenames did not match any python files

Error if you run yapf against any file in the .yapfignore file. To get around this problem in pre-commit, I added an existing python file not in .yapfignore as an argument so yapf always has a file to run against.

2
On

A few things are happening in your output and the behaviours you're seeing so I'll explain them separately:

$ pre-commit run yapf                                                                                                                                                                                         
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to ****************

this output here indicates that pre-commit took your un-staged changes and discarded them, likely not showing what you expect. you probably want pre-commit run yapf --all-files when demonstrating what's going on

on to the actual question.

pre-commit doesn't know about implementation details of whatever tool you're using (and to an extent, yapf doesn't either!)

when a file is in .yapfignore and you pass it to yapf it acts as if it doesn't exist (which is very strange!)

$ tail -n999 hello_wat.py .yapfignore 
==> hello_wat.py <==
x =     5+  4

==> .yapfignore <==
hello*.py

$ yapf hello_wat.py 
yapf: Input filenames did not match any python files

what you probably want to do is utilize pre-commit's exclude so that the files are never passed to yapf at all! taking my example (you didn't share your .yapfignore or a minimal case to go off of)

repos:
-   repo: https://github.com/pre-commit/mirrors-yapf
    rev: v0.29.0
    hooks:
    -   id: yapf
        exclude: ^hello\.*\.py$

now yapf won't run against hello*.py!


disclaimer: I'm the creator of pre-commit.