Maintainer of Black here :wave:
OK, so I actually missed a few points in my comments. To address the main question, this is 100% expected behaviour. Your regex is fine.
The thing is that when you ask VSCode to format your file on save, it calls Black passing the filepath to your current file (you just saved) directly. If you open the "Output" tab in the bottom panel and then save a Python file, you'll notice something like this:
./venv/bin/python -m black --safe --diff --quiet ./tests/data/nothing-changed.py
--include
, --exclude
, and --extend-exclude
only affect files and directories that are discovered by Black itself. You might be wondering, huh, Black can look for files to format? Yes, it does when you run black .
or give Black any other directory. The flipside of this is that these options do nothing to files given as an argument to Black.
If you want to keep Format on Save enabled, your only recourse is to use --force-exclude
which is similar to --extend-exclude
but it's always enforced. You can either configure --force-exclude
in pyproject.toml
or via VSCode's Black arguments setting (preferably for the current workspace only).
The difference between putting it in pyproject.toml
and configuring VSCode to pass extra options to Black is well, when it's applied. If it's in pyproject.toml
it will always be enforced, even when you're not using VSCode and instead are using a bash shell or whatever. This can be useful if you're using pre-commit (which passes files to Black directly just like VSCode) or similar, but can be annoying otherwise.
(if you do choose to force exclude project-wide via pyproject.toml
, you can format force-excluded files by either piping it in or temporarily clearing --force-exclude
on the CLI, e.g. black --force-exclude='' file_you_want_to_format_even_though_it_is_force_excluded.py
)
--extend-exclude
because VSCode will call Black with the filepath given directly. If you take a look at the "Output" tab and then run "Format Document", you'll notice this, for example here's what happens when I do this:./venv/bin/python -m black --safe --diff --quiet ./tests/data/nothing-changed.py
– Lofty--extend-exclude
won't ever affect it. Your only option is to use--force-exclude
which is like--extend-exclude
but is always applied. Your only way of formatting a file that's force excluded is to pipe in its contents toblack -
and save STDOUT to the same file. – Loftyforce-exclude
in mypyproject.toml
and be done with it? I'm not sure if I should create a little script that pipes the content and saves to STDOUT, but this just kind of feels like a shortcoming of black no? Anyway thanks @ichard26, I feel like you should copy your comment as an answer so I can consider this question "resolved". – Barbarbarbara