| 1 |
# Using _Black_ with other tools |
| 2 |
|
| 3 |
## Black compatible configurations |
| 4 |
|
| 5 |
All of Black's changes are harmless (or at least, they should be), but a few do conflict |
| 6 |
against other tools. It is not uncommon to be using other tools alongside _Black_ like |
| 7 |
linters and type checkers. Some of them need a bit of tweaking to resolve the conflicts. |
| 8 |
Listed below are _Black_ compatible configurations in various formats for the common |
| 9 |
tools out there. |
| 10 |
|
| 11 |
**Please note** that _Black_ only supports the TOML file format for its configuration |
| 12 |
(e.g. `pyproject.toml`). The provided examples are to only configure their corresponding |
| 13 |
tools, using **their** supported file formats. |
| 14 |
|
| 15 |
Compatible configuration files can be |
| 16 |
[found here](https://github.com/psf/black/blob/main/docs/compatible_configs/). |
| 17 |
|
| 18 |
### isort |
| 19 |
|
| 20 |
[isort](https://pypi.org/p/isort/) helps to sort and format imports in your Python code. |
| 21 |
_Black_ also formats imports, but in a different way from isort's defaults which leads |
| 22 |
to conflicting changes. |
| 23 |
|
| 24 |
#### Profile |
| 25 |
|
| 26 |
Since version 5.0.0, isort supports |
| 27 |
[profiles](https://pycqa.github.io/isort/docs/configuration/profiles.html) to allow easy |
| 28 |
interoperability with common code styles. You can set the black profile in any of the |
| 29 |
[config files](https://pycqa.github.io/isort/docs/configuration/config_files.html) |
| 30 |
supported by isort. Below, an example for `pyproject.toml`: |
| 31 |
|
| 32 |
```toml |
| 33 |
[tool.isort] |
| 34 |
profile = "black" |
| 35 |
``` |
| 36 |
|
| 37 |
#### Custom Configuration |
| 38 |
|
| 39 |
If you're using an isort version that is older than 5.0.0 or you have some custom |
| 40 |
configuration for _Black_, you can tweak your isort configuration to make it compatible |
| 41 |
with _Black_. Below, an example for `.isort.cfg`: |
| 42 |
|
| 43 |
```ini |
| 44 |
multi_line_output = 3 |
| 45 |
include_trailing_comma = True |
| 46 |
force_grid_wrap = 0 |
| 47 |
use_parentheses = True |
| 48 |
ensure_newline_before_comments = True |
| 49 |
line_length = 88 |
| 50 |
``` |
| 51 |
|
| 52 |
#### Why those options above? |
| 53 |
|
| 54 |
_Black_ wraps imports that surpass `line-length` by moving identifiers onto separate |
| 55 |
lines and by adding a trailing comma after each. A more detailed explanation of this |
| 56 |
behaviour can be |
| 57 |
[found here](../the_black_code_style/current_style.md#how-black-wraps-lines). |
| 58 |
|
| 59 |
isort's default mode of wrapping imports that extend past the `line_length` limit is |
| 60 |
"Grid". |
| 61 |
|
| 62 |
```python |
| 63 |
from third_party import (lib1, lib2, lib3, |
| 64 |
lib4, lib5, ...) |
| 65 |
``` |
| 66 |
|
| 67 |
This style is incompatible with _Black_, but isort can be configured to use a different |
| 68 |
wrapping mode called "Vertical Hanging Indent" which looks like this: |
| 69 |
|
| 70 |
```python |
| 71 |
from third_party import ( |
| 72 |
lib1, |
| 73 |
lib2, |
| 74 |
lib3, |
| 75 |
lib4, |
| 76 |
) |
| 77 |
``` |
| 78 |
|
| 79 |
This style is _Black_ compatible and can be achieved by `multi-line-output = 3`. Also, |
| 80 |
as mentioned above, when wrapping long imports _Black_ puts a trailing comma and uses |
| 81 |
parentheses. isort should follow the same behaviour and passing the options |
| 82 |
`include_trailing_comma = True` and `use_parentheses = True` configures that. |
| 83 |
|
| 84 |
The option `force_grid_wrap = 0` is just to tell isort to only wrap imports that surpass |
| 85 |
the `line_length` limit. |
| 86 |
|
| 87 |
Finally, isort should be told to wrap imports when they surpass _Black_'s default limit |
| 88 |
of 88 characters via `line_length = 88` as well as |
| 89 |
`ensure_newline_before_comments = True` to ensure spacing import sections with comments |
| 90 |
works the same as with _Black_. |
| 91 |
|
| 92 |
**Please note** `ensure_newline_before_comments = True` only works since isort >= 5 but |
| 93 |
does not break older versions so you can keep it if you are running previous versions. |
| 94 |
|
| 95 |
#### Formats |
| 96 |
|
| 97 |
<details> |
| 98 |
<summary>.isort.cfg</summary> |
| 99 |
|
| 100 |
```ini |
| 101 |
[settings] |
| 102 |
profile = black |
| 103 |
``` |
| 104 |
|
| 105 |
</details> |
| 106 |
|
| 107 |
<details> |
| 108 |
<summary>setup.cfg</summary> |
| 109 |
|
| 110 |
```ini |
| 111 |
[isort] |
| 112 |
profile = black |
| 113 |
``` |
| 114 |
|
| 115 |
</details> |
| 116 |
|
| 117 |
<details> |
| 118 |
<summary>pyproject.toml</summary> |
| 119 |
|
| 120 |
```toml |
| 121 |
[tool.isort] |
| 122 |
profile = 'black' |
| 123 |
``` |
| 124 |
|
| 125 |
</details> |
| 126 |
|
| 127 |
<details> |
| 128 |
<summary>.editorconfig</summary> |
| 129 |
|
| 130 |
```ini |
| 131 |
[*.py] |
| 132 |
profile = black |
| 133 |
``` |
| 134 |
|
| 135 |
</details> |
| 136 |
|
| 137 |
### pycodestyle |
| 138 |
|
| 139 |
[pycodestyle](https://pycodestyle.pycqa.org/) is a code linter. It warns you of syntax |
| 140 |
errors, possible bugs, stylistic errors, etc. For the most part, pycodestyle follows |
| 141 |
[PEP 8](https://peps.python.org/pep-0008/) when warning about stylistic errors. There |
| 142 |
are a few deviations that cause incompatibilities with _Black_. |
| 143 |
|
| 144 |
#### Configuration |
| 145 |
|
| 146 |
```ini |
| 147 |
max-line-length = 88 |
| 148 |
ignore = E203,E701 |
| 149 |
``` |
| 150 |
|
| 151 |
(labels/why-pycodestyle-warnings)= |
| 152 |
|
| 153 |
#### Why those options above? |
| 154 |
|
| 155 |
##### `max-line-length` |
| 156 |
|
| 157 |
As with isort, pycodestyle should be configured to allow lines up to the length limit of |
| 158 |
`88`, _Black_'s default. |
| 159 |
|
| 160 |
##### `E203` |
| 161 |
|
| 162 |
In some cases, as determined by PEP 8, _Black_ will enforce an equal amount of |
| 163 |
whitespace around slice operators. Due to this, pycodestyle will raise |
| 164 |
`E203 whitespace before ':'` warnings. Since this warning is not PEP 8 compliant, it |
| 165 |
should be disabled. |
| 166 |
|
| 167 |
##### `E701` / `E704` |
| 168 |
|
| 169 |
_Black_ will collapse implementations of classes and functions consisting solely of `..` |
| 170 |
to a single line. This matches how such examples are formatted in PEP 8. It remains true |
| 171 |
that in all other cases Black will prevent multiple statements on the same line, in |
| 172 |
accordance with PEP 8 generally discouraging this. |
| 173 |
|
| 174 |
However, `pycodestyle` does not mirror this logic and may raise |
| 175 |
`E701 multiple statements on one line (colon)` in this situation. Its |
| 176 |
disabled-by-default `E704 multiple statements on one line (def)` rule may also raise |
| 177 |
warnings and should not be enabled. |
| 178 |
|
| 179 |
##### `W503` |
| 180 |
|
| 181 |
When breaking a line, _Black_ will break it before a binary operator. This is compliant |
| 182 |
with PEP 8 as of |
| 183 |
[April 2016](https://github.com/python/peps/commit/c59c4376ad233a62ca4b3a6060c81368bd21e85b#diff-64ec08cc46db7540f18f2af46037f599). |
| 184 |
There's a disabled-by-default warning in Flake8 which goes against this PEP 8 |
| 185 |
recommendation called `W503 line break before binary operator`. It should not be enabled |
| 186 |
in your configuration. You can use its counterpart |
| 187 |
`W504 line break after binary operator` instead. |
| 188 |
|
| 189 |
#### Formats |
| 190 |
|
| 191 |
<details> |
| 192 |
<summary>setup.cfg, .pycodestyle, tox.ini</summary> |
| 193 |
|
| 194 |
```ini |
| 195 |
[pycodestyle] |
| 196 |
max-line-length = 88 |
| 197 |
ignore = E203,E701 |
| 198 |
``` |
| 199 |
|
| 200 |
</details> |
| 201 |
|
| 202 |
### Flake8 |
| 203 |
|
| 204 |
[Flake8](https://pypi.org/p/flake8/) is a wrapper around multiple linters, including |
| 205 |
pycodestyle. As such, it has many of the same issues. |
| 206 |
|
| 207 |
#### Bugbear |
| 208 |
|
| 209 |
It's recommended to use [the Bugbear plugin](https://github.com/PyCQA/flake8-bugbear) |
| 210 |
and enable |
| 211 |
[its B950 check](https://github.com/PyCQA/flake8-bugbear#opinionated-warnings#:~:text=you%20expect%20it.-,B950,-%3A%20Line%20too%20long) |
| 212 |
instead of using Flake8's E501, because it aligns with |
| 213 |
[Black's 10% rule](labels/line-length). |
| 214 |
|
| 215 |
Install Bugbear and use the following config: |
| 216 |
|
| 217 |
```ini |
| 218 |
[flake8] |
| 219 |
max-line-length = 80 |
| 220 |
extend-select = B950 |
| 221 |
extend-ignore = E203,E501,E701 |
| 222 |
``` |
| 223 |
|
| 224 |
#### Minimal Configuration |
| 225 |
|
| 226 |
In cases where you can't or don't want to install Bugbear, you can use this minimally |
| 227 |
compatible config: |
| 228 |
|
| 229 |
```ini |
| 230 |
[flake8] |
| 231 |
max-line-length = 88 |
| 232 |
extend-ignore = E203,E701 |
| 233 |
``` |
| 234 |
|
| 235 |
#### Why those options above? |
| 236 |
|
| 237 |
See [the pycodestyle section](labels/why-pycodestyle-warnings) above. |
| 238 |
|
| 239 |
#### Formats |
| 240 |
|
| 241 |
<details> |
| 242 |
<summary>.flake8, setup.cfg, tox.ini</summary> |
| 243 |
|
| 244 |
```ini |
| 245 |
[flake8] |
| 246 |
max-line-length = 88 |
| 247 |
extend-ignore = E203,E701 |
| 248 |
``` |
| 249 |
|
| 250 |
</details> |
| 251 |
|
| 252 |
### Pylint |
| 253 |
|
| 254 |
[Pylint](https://pypi.org/p/pylint/) is also a code linter like Flake8. It has many of |
| 255 |
the same checks as Flake8 and more. It particularly has more formatting checks regarding |
| 256 |
style conventions like variable naming. |
| 257 |
|
| 258 |
#### Configuration |
| 259 |
|
| 260 |
```ini |
| 261 |
max-line-length = 88 |
| 262 |
``` |
| 263 |
|
| 264 |
#### Why those options above? |
| 265 |
|
| 266 |
Pylint should be configured to only complain about lines that surpass `88` characters |
| 267 |
via `max-line-length = 88`. |
| 268 |
|
| 269 |
If using `pylint<2.6.0`, also disable `C0326` and `C0330` as these are incompatible with |
| 270 |
_Black_ formatting and have since been removed. |
| 271 |
|
| 272 |
#### Formats |
| 273 |
|
| 274 |
<details> |
| 275 |
<summary>pylintrc</summary> |
| 276 |
|
| 277 |
```ini |
| 278 |
[format] |
| 279 |
max-line-length = 88 |
| 280 |
``` |
| 281 |
|
| 282 |
</details> |
| 283 |
|
| 284 |
<details> |
| 285 |
<summary>setup.cfg</summary> |
| 286 |
|
| 287 |
```ini |
| 288 |
[pylint] |
| 289 |
max-line-length = 88 |
| 290 |
``` |
| 291 |
|
| 292 |
</details> |
| 293 |
|
| 294 |
<details> |
| 295 |
<summary>pyproject.toml</summary> |
| 296 |
|
| 297 |
```toml |
| 298 |
[tool.pylint.format] |
| 299 |
max-line-length = "88" |
| 300 |
``` |
| 301 |
|
| 302 |
</details> |