|
2 | 2 |
|
3 | 3 | ## Preview style |
4 | 4 |
|
| 5 | +(labels/preview-style)= |
| 6 | + |
5 | 7 | Experimental, potentially disruptive style changes are gathered under the `--preview` |
6 | 8 | CLI flag. At the end of each year, these changes may be adopted into the default style, |
7 | 9 | as described in [The Black Code Style](index.md). Because the functionality is |
@@ -262,52 +264,3 @@ s = ( # Top comment |
262 | 264 | # Bottom comment |
263 | 265 | ) |
264 | 266 | ``` |
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