Skip to content

Set tool.black.exclude so that stdlib/venv is not excluded when "manually" invoking Black#15909

Merged
AlexWaygood merged 1 commit into
mainfrom
black-dont-ignore-venv
Jun 13, 2026
Merged

Set tool.black.exclude so that stdlib/venv is not excluded when "manually" invoking Black#15909
AlexWaygood merged 1 commit into
mainfrom
black-dont-ignore-venv

Conversation

@AlexWaygood

Copy link
Copy Markdown
Member

If you don't set tool.black.exclude, Black by default ignores everything listed in your .gitignore file and ignores a hardcoded list of regex patterns. This is documented here. One of those regex patterns is venv, so if you run uvx black stdlib/venv in this repo, Black currently does this:

% uvx black stdlib/venv
No Python files are present to be formatted. Nothing to do 😴

Now, that actually doesn't matter much for CI in this repo right now, because we run Black in CI via pre-commit, which passes all files to be formatted as positional arguments when it invokes Black. Explicitly passed positional files are formatted by Black even if the whole directory matches an exclude pattern:

% uvx black stdlib/venv/__init__.pyi
All done! ✨ 🍰 ✨
1 file left unchanged.

So Black has been enforced in CI anyway for typeshed because of the specific way we've been invoking it. Still, it seems weird that, right now, exactly which files are formatted depends on exactly how you're invoking Black. This PR therefore adds a tool.black.exclude setting so that this is consistent however you invoke Black. It has the same exclusions as the tool.ruff.exclude setting we already specify in our pyproject.toml file.

This problem has existed since 1f1bc6f, when typeshed replaced its tool.black.exclude setting with a tool.black.force-exclude setting! The reason why I noticed it now is that I was investigating why some of the docstrings in Ruff's codemodded version of typeshed have apparently quite bad formatting. The reason is that we format the stubs after codemodding them using uvx black --config=<typeshed pyproject.toml file>, and typeshed's pyproject.toml file doesn't specify tool.black.exclude, so in Ruff's CI we were just falling back to Black's default exclude patterns too. If necessary, I can just fix this problem in Ruff's CI, but I figured that it's quite confusing to have Black work differently in typeshed depending on how it's invoked, so it might be better to fix the problem over here.

(We use Black to format the stubs in Ruff's CI so that we minimize spurious differences with the upstream stubs; formatting the codemodded stubs with Ruff would increase the risk of that.)

@brianschubert brianschubert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems sensible to me!

@AlexWaygood AlexWaygood merged commit 99ecff2 into main Jun 13, 2026
39 checks passed
@AlexWaygood AlexWaygood deleted the black-dont-ignore-venv branch June 13, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants