Skip to content

Commit d330dee

Browse files
docs: We're not going to use backslashes for the with statement (#4564)
1 parent 3d81290 commit d330dee

File tree

1 file changed

+2
-49
lines changed

1 file changed

+2
-49
lines changed

docs/the_black_code_style/future_style.md

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Preview style
44

5+
(labels/preview-style)=
6+
57
Experimental, potentially disruptive style changes are gathered under the `--preview`
68
CLI flag. At the end of each year, these changes may be adopted into the default style,
79
as described in [The Black Code Style](index.md). Because the functionality is
@@ -262,52 +264,3 @@ s = ( # Top comment
262264
# Bottom comment
263265
)
264266
```
265-
266-
## Potential future changes
267-
268-
This section lists changes that we may want to make in the future, but that aren't
269-
implemented yet.
270-
271-
### Using backslashes for with statements
272-
273-
[Backslashes are bad and should be never be used](labels/why-no-backslashes) however
274-
there is one exception: `with` statements using multiple context managers. Before Python
275-
3.9 Python's grammar does not allow organizing parentheses around the series of context
276-
managers.
277-
278-
We don't want formatting like:
279-
280-
```py3
281-
with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
282-
... # nothing to split on - line too long
283-
```
284-
285-
So _Black_ will, when we implement this, format it like this:
286-
287-
```py3
288-
with \
289-
make_context_manager1() as cm1, \
290-
make_context_manager2() as cm2, \
291-
make_context_manager3() as cm3, \
292-
make_context_manager4() as cm4 \
293-
:
294-
... # backslashes and an ugly stranded colon
295-
```
296-
297-
Although when the target version is Python 3.9 or higher, _Black_ uses parentheses
298-
instead in `--preview` mode (see below) since they're allowed in Python 3.9 and higher.
299-
300-
An alternative to consider if the backslashes in the above formatting are undesirable is
301-
to use {external:py:obj}`contextlib.ExitStack` to combine context managers in the
302-
following way:
303-
304-
```python
305-
with contextlib.ExitStack() as exit_stack:
306-
cm1 = exit_stack.enter_context(make_context_manager1())
307-
cm2 = exit_stack.enter_context(make_context_manager2())
308-
cm3 = exit_stack.enter_context(make_context_manager3())
309-
cm4 = exit_stack.enter_context(make_context_manager4())
310-
...
311-
```
312-
313-
(labels/preview-style)=

0 commit comments

Comments
 (0)