| 1 |
# The (future of the) Black code style |
| 2 |
|
| 3 |
## Preview style |
| 4 |
|
| 5 |
(labels/preview-style)= |
| 6 |
|
| 7 |
Experimental, potentially disruptive style changes are gathered under the `--preview` |
| 8 |
CLI flag. At the end of each year, these changes may be adopted into the default style, |
| 9 |
as described in [The Black Code Style](index.md). Because the functionality is |
| 10 |
experimental, feedback and issue reports are highly encouraged! |
| 11 |
|
| 12 |
(labels/preview-features)= |
| 13 |
|
| 14 |
Currently, the following features are included in the preview style: |
| 15 |
|
| 16 |
- `wrap_comprehension_in`: Wrap the `in` clause of list and dictionary comprehensions |
| 17 |
across lines if it would otherwise exceed the maximum line length. |
| 18 |
([see below](labels/wrap-comprehension-in)) |
| 19 |
- `simplify_power_operator_hugging`: Use a simpler implementation of the power operator |
| 20 |
"hugging" logic (removing whitespace around `**` in simple expressions), which applies |
| 21 |
also in the rare case the exponentiation is split into separate lines. |
| 22 |
([see below](labels/simplify-power-operator)) |
| 23 |
- `wrap_long_dict_values_in_parens`: Add parentheses around long values in dictionaries. |
| 24 |
([see below](labels/wrap-long-dict-values)) |
| 25 |
- `fix_if_guard_explosion_in_case_statement`: fixed exploding of the if guard in case |
| 26 |
patterns which have trailing commas in them, even if the guard expression fits in one |
| 27 |
line |
| 28 |
- `pyi_overload_group_blank_lines`: In `.pyi` stub files, improve heuristics around when |
| 29 |
blank lines should appear before, after and within decorated function groups. |
| 30 |
([see below](labels/pyi-overload-group)) |
| 31 |
- `pyi_blank_line_before_decorated_class`: In `.pyi` stub files, enforce a blank line |
| 32 |
before a decorated class definition when it follows a function definition. |
| 33 |
([see below](labels/pyi-blank-line-before-decorated-class)) |
| 34 |
|
| 35 |
(labels/wrap-comprehension-in)= |
| 36 |
|
| 37 |
### Wrapping long comprehension `in` clauses |
| 38 |
|
| 39 |
When a list or dictionary comprehension has a long `in` clause that would exceed the |
| 40 |
maximum line length, Black will wrap it across multiple lines for better readability. |
| 41 |
This helps keep comprehensions readable when the iterable expression is complex or |
| 42 |
lengthy. |
| 43 |
|
| 44 |
For example: |
| 45 |
|
| 46 |
```python |
| 47 |
# Before |
| 48 |
result = [ |
| 49 |
very_very_very_very_very_long_item |
| 50 |
for very_very_very_very_very_long_item in some_very_very_very_very_very_very_long_function_name |
| 51 |
] |
| 52 |
``` |
| 53 |
|
| 54 |
will be formatted to: |
| 55 |
|
| 56 |
```python |
| 57 |
# After |
| 58 |
result = [ |
| 59 |
very_very_very_very_very_long_item |
| 60 |
for very_very_very_very_very_long_item in ( |
| 61 |
some_very_very_very_very_very_very_long_function_name |
| 62 |
) |
| 63 |
] |
| 64 |
``` |
| 65 |
|
| 66 |
This also applies to dictionary comprehensions: |
| 67 |
|
| 68 |
```python |
| 69 |
# Before |
| 70 |
mapping = { |
| 71 |
very_long_key: very_very_very_long_item |
| 72 |
for very_long_key, very_very_very_long_item in very_very_very_very_long_function_name |
| 73 |
} |
| 74 |
``` |
| 75 |
|
| 76 |
will be formatted to: |
| 77 |
|
| 78 |
```python |
| 79 |
# After |
| 80 |
mapping = { |
| 81 |
very_long_key: very_very_very_long_item |
| 82 |
for very_long_key, very_very_very_long_item in ( |
| 83 |
very_very_very_very_long_function_name |
| 84 |
) |
| 85 |
} |
| 86 |
``` |
| 87 |
|
| 88 |
(labels/simplify-power-operator)= |
| 89 |
|
| 90 |
### Simplified power operator whitespace handling |
| 91 |
|
| 92 |
Black's power operator "hugging" logic removes whitespace around `**` in simple |
| 93 |
expressions (e.g., `x**2` instead of `x ** 2`). This feature uses a simpler, more |
| 94 |
consistent implementation that also applies when exponentiation is split across lines. |
| 95 |
|
| 96 |
For example: |
| 97 |
|
| 98 |
```python |
| 99 |
# Simple expressions - whitespace is removed |
| 100 |
result = x**2 + y**3 |
| 101 |
value = base**exponent |
| 102 |
``` |
| 103 |
|
| 104 |
When the exponentiation is split across lines (rare), the simplified logic ensures |
| 105 |
consistent formatting: |
| 106 |
|
| 107 |
```python |
| 108 |
# Complex expression split across lines |
| 109 |
result = ( |
| 110 |
some_very_long_base_expression |
| 111 |
**some_very_long_exponent_expression |
| 112 |
**some_very_long_third_expression |
| 113 |
) |
| 114 |
``` |
| 115 |
|
| 116 |
This feature primarily improves the internal consistency of Black's formatting logic |
| 117 |
rather than making dramatic visual changes to most code. |
| 118 |
|
| 119 |
(labels/wrap-long-dict-values)= |
| 120 |
|
| 121 |
### Improved parentheses management in dicts |
| 122 |
|
| 123 |
For dict literals with long values, they are now wrapped in parentheses. Unnecessary |
| 124 |
parentheses are now removed. For example: |
| 125 |
|
| 126 |
```python |
| 127 |
my_dict = { |
| 128 |
"a key in my dict": a_very_long_variable |
| 129 |
* and_a_very_long_function_call() |
| 130 |
/ 100000.0, |
| 131 |
"another key": (short_value), |
| 132 |
} |
| 133 |
``` |
| 134 |
|
| 135 |
will be changed to: |
| 136 |
|
| 137 |
```python |
| 138 |
my_dict = { |
| 139 |
"a key in my dict": ( |
| 140 |
a_very_long_variable * and_a_very_long_function_call() / 100000.0 |
| 141 |
), |
| 142 |
"another key": short_value, |
| 143 |
} |
| 144 |
``` |
| 145 |
|
| 146 |
(labels/pyi-overload-group)= |
| 147 |
|
| 148 |
### Improved overload groups in stub files |
| 149 |
|
| 150 |
In `.pyi` stub files, Black now has improved heuristics regarding when blank lines |
| 151 |
should appear before, after or within groups of decorated functions that share the same |
| 152 |
name (such as `@overload` groups). Two rules are applied when a decorated function is |
| 153 |
determined to be part of a series of >=2 decorated functions with the same name: |
| 154 |
|
| 155 |
1. **Before the decorated function**: a blank line is always inserted, unless the |
| 156 |
preceding statement is a same-name decorated function (i.e. part of an `@overload` |
| 157 |
group) or the function is the first statement in its block. |
| 158 |
2. **After the decorated function**: a blank line is always inserted, unless the |
| 159 |
following statement is a same-name decorated function. |
| 160 |
|
| 161 |
These rules apply regardless of what the adjacent statement is — whether it's another |
| 162 |
function definition, a variable annotation, or any other statement. |
| 163 |
|
| 164 |
Previously, Black could insert unwanted blank lines _within_ an overload group when one |
| 165 |
of the overloads had a docstring, and did not consistently enforce blank lines at the |
| 166 |
boundaries of overload groups: |
| 167 |
|
| 168 |
```python |
| 169 |
# Before |
| 170 |
|
| 171 |
@overload |
| 172 |
def foo(x: int) -> int: |
| 173 |
"""Docs.""" |
| 174 |
|
| 175 |
@overload # unwanted blank line within group |
| 176 |
def foo(x: str) -> str: ... |
| 177 |
def bar(x): ... # no blank line after group |
| 178 |
``` |
| 179 |
|
| 180 |
With this feature enabled, the group is kept together and clearly separated from |
| 181 |
surrounding code: |
| 182 |
|
| 183 |
```python |
| 184 |
# After (with --preview) |
| 185 |
|
| 186 |
@overload |
| 187 |
def foo(x: int) -> int: |
| 188 |
"""Docs.""" |
| 189 |
@overload |
| 190 |
def foo(x: str) -> str: ... |
| 191 |
|
| 192 |
def bar(x): ... |
| 193 |
``` |
| 194 |
|
| 195 |
(labels/pyi-blank-line-before-decorated-class)= |
| 196 |
|
| 197 |
### Blank line before decorated classes in stub files |
| 198 |
|
| 199 |
In `.pyi` stub files, Black already enforces blank lines around class definitions in |
| 200 |
many situations. However, when a class has a decorator, Black previously failed to |
| 201 |
enforce this, instead _removing_ the blank line between a function and the decorator: |
| 202 |
|
| 203 |
```diff |
| 204 |
# Before |
| 205 |
|
| 206 |
def foo(): ... |
| 207 |
- |
| 208 |
@decorator |
| 209 |
class Bar: ... |
| 210 |
|
| 211 |
def baz(): ... |
| 212 |
@decorator |
| 213 |
class Spam: ... |
| 214 |
``` |
| 215 |
|
| 216 |
With this feature enabled, a blank line is enforced, consistent with how undecorated |
| 217 |
classes are handled: |
| 218 |
|
| 219 |
```diff |
| 220 |
# After (with --preview) |
| 221 |
|
| 222 |
def foo(): ... |
| 223 |
|
| 224 |
@decorator |
| 225 |
class Bar: ... |
| 226 |
|
| 227 |
def baz(): ... |
| 228 |
+ |
| 229 |
@decorator |
| 230 |
class Spam: ... |
| 231 |
``` |
| 232 |
|
| 233 |
## Unstable style |
| 234 |
|
| 235 |
(labels/unstable-style)= |
| 236 |
|
| 237 |
In the past, the preview style included some features with known bugs, so that we were |
| 238 |
unable to move these features to the stable style. Therefore, such features are now |
| 239 |
moved to the `--unstable` style. All features in the `--preview` style are expected to |
| 240 |
make it to next year's stable style; features in the `--unstable` style will be |
| 241 |
stabilized only if issues with them are fixed. If bugs are discovered in a `--preview` |
| 242 |
feature, it is demoted to the `--unstable` style. To avoid thrash when a feature is |
| 243 |
demoted from the `--preview` to the `--unstable` style, users can use the |
| 244 |
`--enable-unstable-feature` flag to enable specific unstable features. |
| 245 |
|
| 246 |
(labels/unstable-features)= |
| 247 |
|
| 248 |
The unstable style additionally includes the following features: |
| 249 |
|
| 250 |
- `hug_parens_with_braces_and_square_brackets`: More compact formatting of nested |
| 251 |
brackets. ([see below](labels/hug-parens)) |
| 252 |
- `string_processing`: Split long string literals and related changes. |
| 253 |
([see below](labels/string-processing)) |
| 254 |
|
| 255 |
(labels/hug-parens)= |
| 256 |
|
| 257 |
### Improved multiline dictionary and list indentation for sole function parameter |
| 258 |
|
| 259 |
For better readability and less verticality, _Black_ now pairs parentheses ("(", ")") |
| 260 |
with braces ("{", "}") and square brackets ("[", "]") on the same line. For example: |
| 261 |
|
| 262 |
```python |
| 263 |
foo( |
| 264 |
[ |
| 265 |
1, |
| 266 |
2, |
| 267 |
3, |
| 268 |
] |
| 269 |
) |
| 270 |
|
| 271 |
nested_array = [ |
| 272 |
[ |
| 273 |
1, |
| 274 |
2, |
| 275 |
3, |
| 276 |
] |
| 277 |
] |
| 278 |
``` |
| 279 |
|
| 280 |
will be changed to: |
| 281 |
|
| 282 |
```python |
| 283 |
foo([ |
| 284 |
1, |
| 285 |
2, |
| 286 |
3, |
| 287 |
]) |
| 288 |
|
| 289 |
nested_array = [[ |
| 290 |
1, |
| 291 |
2, |
| 292 |
3, |
| 293 |
]] |
| 294 |
``` |
| 295 |
|
| 296 |
This also applies to list and dictionary unpacking: |
| 297 |
|
| 298 |
```python |
| 299 |
foo( |
| 300 |
*[ |
| 301 |
a_long_function_name(a_long_variable_name) |
| 302 |
for a_long_variable_name in some_generator |
| 303 |
] |
| 304 |
) |
| 305 |
``` |
| 306 |
|
| 307 |
will become: |
| 308 |
|
| 309 |
```python |
| 310 |
foo(*[ |
| 311 |
a_long_function_name(a_long_variable_name) |
| 312 |
for a_long_variable_name in some_generator |
| 313 |
]) |
| 314 |
``` |
| 315 |
|
| 316 |
You can use a magic trailing comma to avoid this compacting behavior; by default, |
| 317 |
_Black_ will not reformat the following code: |
| 318 |
|
| 319 |
```python |
| 320 |
foo( |
| 321 |
[ |
| 322 |
1, |
| 323 |
2, |
| 324 |
3, |
| 325 |
], |
| 326 |
) |
| 327 |
``` |
| 328 |
|
| 329 |
(labels/string-processing)= |
| 330 |
|
| 331 |
### Improved string processing |
| 332 |
|
| 333 |
_Black_ will split long string literals and merge short ones. Parentheses are used where |
| 334 |
appropriate. When split, parts of f-strings that don't need formatting are converted to |
| 335 |
plain strings. f-strings will not be merged if they contain internal quotes and it would |
| 336 |
change their quotation mark style. Line continuation backslashes are converted into |
| 337 |
parenthesized strings. Unnecessary parentheses are stripped. The stability and status of |
| 338 |
this feature is tracked in [this issue](https://github.com/psf/black/issues/2188). |