All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
3 Foundational knowledge on using and configuring Black.
5 _Black_ is a well-behaved Unix-style command-line tool:
7 - it does nothing if no sources are passed to it;
8 - it will read from standard input and write to standard output if `-` is used as the
10 - it only outputs messages to users on standard error;
11 - exits with code 0 unless an internal error occurred (or `--check` was used).
15 To get started right away with sensible defaults:
18 black {source_file_or_directory}
21 You can run _Black_ as a package if running it as a script doesn't work:
24 python -m black {source_file_or_directory}
27 ### Command line options
29 _Black_ has quite a few knobs these days, although _Black_ is opinionated so style
30 configuration options are deliberately limited and rarely added. You can list them by
31 running `black --help`.
35 <summary>Help output</summary>
38 Usage: black [OPTIONS] [SRC]...
40 The uncompromising code formatter.
43 -c, --code TEXT Format the code passed in as a string.
44 -l, --line-length INTEGER How many characters per line to allow.
47 -t, --target-version [py27|py33|py34|py35|py36|py37|py38|py39]
48 Python versions that should be supported by
49 Black's output. [default: per-file auto-
52 --pyi Format all input files like typing stubs
53 regardless of file extension (useful when
54 piping source on standard input).
56 -S, --skip-string-normalization
57 Don't normalize string quotes or prefixes.
58 -C, --skip-magic-trailing-comma
59 Don't use trailing commas as a reason to
62 --check Don't write the files back, just return the
63 status. Return code 0 means nothing would
64 change. Return code 1 means some files
65 would be reformatted. Return code 123 means
66 there was an internal error.
68 --diff Don't write the files back, just output a
69 diff for each file on stdout.
71 --color / --no-color Show colored diff. Only applies when
74 --fast / --safe If --fast given, skip temporary sanity
75 checks. [default: --safe]
77 --include TEXT A regular expression that matches files and
78 directories that should be included on
79 recursive searches. An empty value means
80 all files are included regardless of the
81 name. Use forward slashes for directories
82 on all platforms (Windows, too). Exclusions
83 are calculated first, inclusions later.
86 --exclude TEXT A regular expression that matches files and
87 directories that should be excluded on
88 recursive searches. An empty value means no
89 paths are excluded. Use forward slashes for
90 directories on all platforms (Windows, too).
91 Exclusions are calculated first, inclusions
92 later. [default: /(\.direnv|\.eggs|\.git|\.
93 hg|\.mypy_cache|\.nox|\.tox|\.venv|venv|\.svn|_bu
94 ild|buck-out|build|dist)/]
96 --extend-exclude TEXT Like --exclude, but adds additional files
97 and directories on top of the excluded
98 ones (useful if you simply want to add to
101 --force-exclude TEXT Like --exclude, but files and directories
102 matching this regex will be excluded even
103 when they are passed explicitly as
107 --stdin-filename TEXT The name of the file when passing it through
108 stdin. Useful to make sure Black will
109 respect --force-exclude option on some
110 editors that rely on using stdin.
112 -q, --quiet Don't emit non-error messages to stderr.
113 Errors are still emitted; silence those with
116 -v, --verbose Also emit messages to stderr about files
117 that were not changed or were ignored due to
120 --version Show the version and exit.
121 --config FILE Read configuration from FILE path.
122 -h, --help Show this message and exit.
127 ## Configuration via a file
129 _Black_ is able to read project-specific default values for its command line options
130 from a `pyproject.toml` file. This is especially useful for specifying custom
131 `--include` and `--exclude`/`--force-exclude`/`--extend-exclude` patterns for your
134 **Pro-tip**: If you're asking yourself "Do I need to configure anything?" the answer is
135 "No". _Black_ is all about sensible defaults. Applying those defaults will have your
136 code in compliance with many other _Black_ formatted projects.
138 ### What on Earth is a `pyproject.toml` file?
140 [PEP 518](https://www.python.org/dev/peps/pep-0518/) defines `pyproject.toml` as a
141 configuration file to store build system requirements for Python projects. With the help
142 of tools like [Poetry](https://python-poetry.org/) or
143 [Flit](https://flit.readthedocs.io/en/latest/) it can fully replace the need for
144 `setup.py` and `setup.cfg` files.
146 ### Where _Black_ looks for the file
148 By default _Black_ looks for `pyproject.toml` starting from the common base directory of
149 all files and directories passed on the command line. If it's not there, it looks in
150 parent directories. It stops looking when it finds the file, or a `.git` directory, or a
151 `.hg` directory, or the root of the file system, whichever comes first.
153 If you're formatting standard input, _Black_ will look for configuration starting from
154 the current working directory.
156 You can use a "global" configuration, stored in a specific location in your home
157 directory. This will be used as a fallback configuration, that is, it will be used if
158 and only if _Black_ doesn't find any configuration as mentioned above. Depending on your
159 operating system, this configuration file should be stored as:
161 - Windows: `~\.black`
162 - Unix-like (Linux, MacOS, etc.): `$XDG_CONFIG_HOME/black` (`~/.config/black` if the
163 `XDG_CONFIG_HOME` environment variable is not set)
165 Note that these are paths to the TOML file itself (meaning that they shouldn't be named
166 as `pyproject.toml`), not directories where you store the configuration. Here, `~`
167 refers to the path to your home directory. On Windows, this will be something like
168 `C:\\Users\UserName`.
170 You can also explicitly specify the path to a particular file that you want with
171 `--config`. In this situation _Black_ will not look for any other file.
173 If you're running with `--verbose`, you will see a blue message if a file was found and
176 Please note `blackd` will not use `pyproject.toml` configuration.
178 ### Configuration format
180 As the file extension suggests, `pyproject.toml` is a
181 [TOML](https://github.com/toml-lang/toml) file. It contains separate sections for
182 different tools. _Black_ is using the `[tool.black]` section. The option keys are the
183 same as long names of options on the command line.
185 Note that you have to use single-quoted strings in TOML for regular expressions. It's
186 the equivalent of r-strings in Python. Multiline strings are treated as verbose regular
187 expressions by Black. Use `[ ]` to denote a significant space character.
190 <summary>Example <code>pyproject.toml</code></summary>
195 target-version = ['py37']
198 # A regex preceded with ^/ will apply only to files and directories
199 # in the root of the project.
200 ^/foo.py # exclude a file named foo.py in the root of the project (in addition to the defaults)
208 Command-line options have defaults that you can see in `--help`. A `pyproject.toml` can
209 override those defaults. Finally, options provided by the user on the command line
212 _Black_ will only ever use one `pyproject.toml` file during an entire run. It doesn't
213 look for multiple files, and doesn't compose configuration from different levels of the
218 You've probably noted that not all of the options you can pass to _Black_ have been
219 covered. Don't worry, the rest will be covered in a later section.
221 A good next step would be configuring auto-discovery so `black .` is all you need
222 instead of laborously listing every file or directory. You can get started by heading
223 over to [File collection and discovery](./file_collection_and_discovery.md).
225 Another good choice would be setting up an
226 [integration with your editor](../integrations/editors.md) of choice or with
227 [pre-commit for source version control](../integrations/source_version_control.md).