0

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)$",
    ]

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?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Bob
  • 195
  • 1
  • 11

1 Answers1

2

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

anthony sottile
  • 61,815
  • 15
  • 148
  • 207