Search code examples
pythonpre-commit-hookpre-commit.comyapf

pre-commit yapf fails on file included in yapfignore


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?


Solution

  • 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.