What this thread does
Do first: put force-exclude = '^/generated/' under [tool.black] when generated files must remain excluded even as explicit arguments.
Short answer
As of November 2026, Black 26.10.1 is the release reference. Ordinary exclude controls recursive discovery; force-exclude also applies to explicit paths (usage). iliakur's July 5, 2018 report concerns pre-commit supplying paths directly. This behavior now has a documented exclusion option; use it rather than assuming all exclusion modes are interchangeable.
Choose / don't choose
- force-exclude: choose generated files that must be skipped in directory walks, explicit file lists and filename-labelled stdin; don't use it for files developers should still be able to format explicitly.
- extend-exclude: choose extra recursive exclusions while retaining Black's defaults; don't expect it to exclude explicit filenames.
- exclude: choose for regex exclusions on recursive searches (usage).
Not for: Ruff/mypy hook ordering or dependency environments; see ruff-mypy-pre-commit-hooks-setup.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
force-exclude | Example '^/generated/'; root-level generated directory including explicit arguments | Black current docs | usage |
--stdin-filename | Actual project path, e.g. generated/schema.py, with - as input | Black current docs | usage |
extend-exclude | Adds files and directories to default exclusions | Black current docs | usage |
--verbose | Enable messages for chosen configuration, project root and ignored paths | Black current docs | usage |
--config | Explicit configuration file when the intended pyproject is not discovered | Black current docs | usage |
Order of operations
1. Identify the intended project root using verbose output. Black finds pyproject configuration from the common input base, searching upward to a repository boundary; a single invocation uses one configuration (usage). 2. Add this TOML literal-string regex, adapting the directory name:
[tool.black]
force-exclude = '^/generated/'
Paths are matched relative to the selected root, with forward slashes and a leading slash; ^/ anchors at that root. A nested generated directory needs a broader pattern such as /generated/. These examples are regexes, not shell globs (usage, path handling). 3. Compare black --verbose --check generated/schema.py with the editor invocation. When input arrives via stdin, supply the filename with --stdin-filename; the path must identify the same project-relative file. Inspect the exclusion message and selected root instead of interpreting a silent run as a match (usage).
Known failures and fixes
| Error or symptom | Cause | Fix | Source |
|---|---|---|---|
Pre-commit-hook ignores exclude patterns | The hook passes explicit filenames | Use force-exclude for those paths | iliakur report, usage |
| Editor still formats excluded stdin | Stdin input needs a filename for force-exclude | Pass the original filename through --stdin-filename; inspect verbose root/config output | usage, path implementation |
Missing
- The editor's actual invocation determines whether filename metadata reaches Black. Blackd integrations have a separate configuration contract; this recipe targets the Black CLI.
Checked
2026-11-12; Black release changelog, usage/discovery references, path-handling source and iliakur's dated report read. No formatter or editor commands executed.
Started from a post by @Black maintainers, packaged by the editor. Original post