【问题标题】:pre-commit yapf fails on file included in yapfignore在 yapfignore 中包含的文件上预提交 yapf 失败
【发布时间】:2021-09-17 07:05:28
【问题描述】:

在 repo 中设置的预提交挂钩之一是 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

每当我对包含在.yapfignore 中的文件进行更改并运行预提交挂钩时,挂钩都会失败:

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

有人知道如何避免 yapf 对 .yapfignore 中包含的文件失败吗?

【问题讨论】:

    标签: python pre-commit-hook pre-commit.com yapf


    【解决方案1】:

    好的,问题的原因是 yapf 将 .yapfignore 中的文件视为不存在的文件,因此您会得到:

    yapf: Input filenames did not match any python files
    

    如果您对 .yapfignore 文件中的任何文件运行 yapf,则会出错。为了在pre-commit 中解决这个问题,我添加了一个不在.yapfignore 中的现有python 文件作为参数,这样yapf 总是有一个文件可以运行。

    【讨论】:

      【解决方案2】:

      您的输出和您看到的行为中发生了一些事情,所以我将分别解释它们:

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

      这里的输出表明pre-commit 接受了您未暂存的更改并丢弃了它们,可能没有显示您的期望。在演示正在发生的事情时,您可能需要pre-commit run yapf --all-files

      关于实际问题。

      pre-commit 不知道您正在使用的任何工具的实现细节(在某种程度上,yapf 也不知道!)

      当一个文件在.yapfignore 中并且你将它传递给yapf 时,它就好像它不存在一样(这很奇怪!)

      $ 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
      

      您可能想要做的是利用pre-commitexclude,这样文件就永远不会传递给yapf!以我为例(你没有分享你的 .yapfignore 或一个最小的案例)

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

      现在 yapf 不会与 hello*.py 对抗!


      免责声明:我是 pre-commit 的创建者。

      【讨论】:

      • 这种方法很棒!但是当.yapfignore:/ 中有很多文件时,它并不能真正扩展。除非有办法以编程方式将 yapfignore 中的所有文件包含在pre-commitexclude 中? (顺便说一句,感谢您创建了一个很棒的工具)
      • 我个人会抛弃你的.yapfignore——尽管编写一个捕获相同内容的多行正则表达式应该不会太难:pre-commit.com/#regular-expressions
      猜你喜欢
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 2013-10-04
      • 2013-06-21
      • 2019-06-02
      • 2013-10-24
      • 1970-01-01
      • 2015-07-08
      相关资源
      最近更新 更多