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?