Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d05e15

Browse files
veeceeyclaude
andauthored
Docs: Expand preview style features documentation with examples (psf#4987)
* Docs: Expand preview style features documentation with examples Adds detailed descriptions and code examples for preview style features that previously only had single-line descriptions, addressing issue psf#4429. Changes: - Add dedicated section for `wrap_comprehension_in` feature: - Explanation of when and why comprehensions are wrapped - Examples with list comprehensions - Examples with dictionary comprehensions - Add dedicated section for `simplify_power_operator_hugging` feature: - Explanation of power operator whitespace handling - Examples with simple expressions - Examples with split expressions - Updated feature list to link to the new detailed sections This brings the preview features documentation in line with the unstable features documentation style, which already includes detailed sections and examples for each feature. Fixes psf#4429 * Add changelog entry for preview style docs expansion (psf#4987) Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix power operator example per review --------- Co-authored-by: Claude Opus 4.6 <[email protected]>
1 parent 38b6d35 commit 9d05e15

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
<!-- Major changes to documentation and policies. Small docs changes
6060
don't need a changelog entry. -->
6161

62+
- Expand preview style documentation with detailed examples for `wrap_comprehension_in`,
63+
`simplify_power_operator_hugging`, and `wrap_long_dict_values_in_parens` features
64+
(#4987)
65+
6266
## 26.1.0
6367

6468
### Highlights

docs/the_black_code_style/future_style.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,92 @@ Currently, the following features are included in the preview style:
1515

1616
- `wrap_comprehension_in`: Wrap the `in` clause of list and dictionary comprehensions
1717
across lines if it would otherwise exceed the maximum line length.
18+
([see below](labels/wrap-comprehension-in))
1819
- `simplify_power_operator_hugging`: Use a simpler implementation of the power operator
1920
"hugging" logic (removing whitespace around `**` in simple expressions), which applies
2021
also in the rare case the exponentiation is split into separate lines.
22+
([see below](labels/simplify-power-operator))
2123
- `wrap_long_dict_values_in_parens`: Add parentheses around long values in dictionaries.
2224
([see below](labels/wrap-long-dict-values))
2325

26+
(labels/wrap-comprehension-in)=
27+
28+
### Wrapping long comprehension `in` clauses
29+
30+
When a list or dictionary comprehension has a long `in` clause that would exceed the
31+
maximum line length, Black will wrap it across multiple lines for better readability.
32+
This helps keep comprehensions readable when the iterable expression is complex or
33+
lengthy.
34+
35+
For example:
36+
37+
```python
38+
# Before
39+
result = [item.process() for item in some_very_long_function_name(argument1, argument2, argument3)]
40+
```
41+
42+
will be formatted to:
43+
44+
```python
45+
# After
46+
result = [
47+
item.process()
48+
for item in some_very_long_function_name(
49+
argument1, argument2, argument3
50+
)
51+
]
52+
```
53+
54+
This also applies to dictionary comprehensions:
55+
56+
```python
57+
# Before
58+
mapping = {key: value.transform() for key, value in get_items_from_very_long_source(param1, param2)}
59+
```
60+
61+
will be formatted to:
62+
63+
```python
64+
# After
65+
mapping = {
66+
key: value.transform()
67+
for key, value in get_items_from_very_long_source(
68+
param1, param2
69+
)
70+
}
71+
```
72+
73+
(labels/simplify-power-operator)=
74+
75+
### Simplified power operator whitespace handling
76+
77+
Black's power operator "hugging" logic removes whitespace around `**` in simple
78+
expressions (e.g., `x**2` instead of `x ** 2`). This feature uses a simpler, more
79+
consistent implementation that also applies when exponentiation is split across lines.
80+
81+
For example:
82+
83+
```python
84+
# Simple expressions - whitespace is removed
85+
result = x**2 + y**3
86+
value = base**exponent
87+
```
88+
89+
When the exponentiation is split across lines (rare), the simplified logic ensures
90+
consistent formatting:
91+
92+
```python
93+
# Complex expression split across lines
94+
result = (
95+
some_very_long_base_expression
96+
**some_very_long_exponent_expression
97+
**some_very_long_third_expression
98+
)
99+
```
100+
101+
This feature primarily improves the internal consistency of Black's formatting logic
102+
rather than making dramatic visual changes to most code.
103+
24104
(labels/wrap-long-dict-values)=
25105

26106
### Improved parentheses management in dicts

0 commit comments

Comments
 (0)