black nciinclude Option不在前委员会中使用以非标准命名添加Python文件

发布于 2025-02-09 09:50:13 字数 853 浏览 3 评论 0 原文

我目前正在成功使用Black作为预投入的工具之一。到目前为止,它仅应用于具有“ .py”扩展名的Python文件,并且默认行为已经足够了。

我现在想将黑色扩展到其他一些文件,这些文件实际上是Python文件,即使它们没有“ .py”扩展名。因此,我尝试将以下“ - congrude”参数添加到我的预制配置中:

- repo: https://github.com/psf/black
  rev: 22.3.0
  hooks:
  - id: black
    args: [
      "--preview",
      "--include", "(\\.py|MyFile)$",
    ]

myfile实际上是一个python文件。

但是,当我提交时,黑色完全忽略了myfile。

我尝试直接在命令行中使用黑色,而“ include”只要指定了一个目录而不是文件列表(或文件通配符)作为目标。如果我提供了文件列表,则Black似乎会尝试重新格式化所有 请求的文件,而不论它们是否匹配“ Include”模式。这也与省略“ include”并依赖默认包含(本质上是“ .py”文件上的匹配)时的行为一致;如果将文件列表指定为目标,那就是黑色似乎正在运行的内容,并且忽略了其自己的默认包含模式。

总而言之,主要问题是如何实现黑色格式到其他文件的所需扩展,但是我现在也对预先承担和黑色之间的交互感到非常困惑。

我的假设是,预先承诺正在向黑色提供文件列表,但是,由于文件列表似乎甚至覆盖了默认包含模式,但似乎并非如此知道只提供Python文件的知识。如果的情况,我该如何告诉预先承诺我希望黑色看到这些额外的文件?如果不是这种情况,并且预先承诺为黑色提供目录目标,为什么配置文件中的“包括”选项不起作用?

I am currently using black successfully as one of the tools in pre-commit. So far, it has only been applied to Python files with ".py" extensions, and default behaviour has sufficed.

I would now like to extend black to some other files that in reality are Python files even though they do not have a ".py" extension. So I tried to add the following "--include" argument to black in my pre-commit config:

- repo: https://github.com/psf/black
  rev: 22.3.0
  hooks:
  - id: black
    args: [
      "--preview",
      "--include", "(\\.py|MyFile)
quot;,
    ]

where MyFile is actually a Python file.

However, when I commit, MyFile is completely ignored by black.

I have tried using black directly at the command line, and the "--include" worked with MyFile as long as I specified a directory rather than a list of files (or file wildcard) as the target. If I provided a list of files, it seemed that black would try to reformat all requested files irrespective of whether they matched the "--include" pattern. This was also consistent with behaviour when the "--include" was omitted, and default inclusion relied on (which is essentially a match on ".py" files); if a list of files is specified as the target, that is what black seems to run on and it ignores its own default inclusion pattern.

In summary, the main issue is how to achieve the desired extension of black formatting to some additional files, but I am also now very confused about the interactions between pre-commit and black.

My assumption had been that pre-commit was supplying a list of files to black but, as a list of files appears to override even the default inclusion pattern, it seems that this cannot be the case, unless pre-commit has sufficient built-in knowledge to know to provide only Python files. If that is the case, how do I tell pre-commit that I want black to see these extra files? If it is not the case and pre-commit is providing directory targets to black, why doesn't the "--include" option in the config file work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久而酒知 2025-02-16 09:50:13

预先委托将文件列表提供给基于 filtering设置 types types_or dublude_types 文件 dubl )并传递它们作为位置参数

black> black 的默认设置要过滤

因此,它运行的命令本质上是:

black $args $files_matching_python_or_pyi

通过添加到 args 您不会更改文件的过滤 - 您需要告诉预先启动 在这种情况下,您想要的文件

,由于您的文件未由Black选择的 types 覆盖,因此您需要将它们覆盖回默认值 - 这是我的建议:

    -   id: black  # keep the original black running on python files
    -   id: black
        types_or: [file]  # reset `types_or` back to the default
        files: MyFile$  # use your custom file matching pattern here

免责声明:我创建了预警

pre-commit supplies a list of files to a tool based on the filtering settings (types, types_or, exclude_types, files, exclude) and passes them as positional arguments

the default setting for black is to filter using types_or: [python, pyi]

so the command it runs will essentially be:

black $args $files_matching_python_or_pyi

by adding to args you won't change the filtering of files -- you need to tell pre-commit what files you want

in this case, since your files aren't covered by the types selected by black, you'll need to override them back to the default -- here's what I recommend:

    -   id: black  # keep the original black running on python files
    -   id: black
        types_or: [file]  # reset `types_or` back to the default
        files: MyFile$  # use your custom file matching pattern here

disclaimer: I created pre-commit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文