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

Skip to content

Commit 7c8e0b0

Browse files
authored
Document new parenthesized with statements (GH-24281)
1 parent c7c3b7d commit 7c8e0b0

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

Doc/reference/compound_stmts.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ This allows common :keyword:`try`...\ :keyword:`except`...\ :keyword:`finally`
411411
usage patterns to be encapsulated for convenient reuse.
412412

413413
.. productionlist:: python-grammar
414-
with_stmt: "with" `with_item` ("," `with_item`)* ":" `suite`
414+
with_stmt: "with" ( "(" `with_stmt_contents` ","? ")" | `with_stmt_contents` ) ":" `suite`
415+
with_stmt_contents: `with_item` ("," `with_item`)*
415416
with_item: `expression` ["as" `target`]
416417

417418
The execution of the :keyword:`with` statement with one "item" proceeds as follows:
@@ -488,9 +489,21 @@ is semantically equivalent to::
488489
with B() as b:
489490
SUITE
490491

492+
You can also write multi-item context managers in multiple lines if
493+
the items are surrounded by parentheses. For example::
494+
495+
with (
496+
A() as a,
497+
B() as b,
498+
):
499+
SUITE
500+
491501
.. versionchanged:: 3.1
492502
Support for multiple context expressions.
493503

504+
.. versionchanged:: 3.10
505+
Support for using grouping parentheses to break the statement in multiple lines.
506+
494507
.. seealso::
495508

496509
:pep:`343` - The "with" statement

Doc/whatsnew/3.10.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,59 @@ New Features
7272

7373
.. _whatsnew310-pep563:
7474

75+
Parenthesized context managers
76+
------------------------------
77+
78+
Using enclosing parentheses for continuation across multiple lines
79+
in context managers is now supported. This allows formatting a long
80+
collection of context managers in multiple lines in a similar way
81+
as it was previously possible with import statements. For instance,
82+
all these examples are now valid:
83+
84+
.. code-block:: python
85+
86+
with (CtxManager() as example):
87+
...
88+
89+
with (
90+
CtxManager1(),
91+
CtxManager2()
92+
):
93+
...
94+
95+
with (CtxManager1() as example,
96+
CtxManager2()):
97+
...
98+
99+
with (CtxManager1(),
100+
CtxManager2() as example):
101+
...
102+
103+
with (
104+
CtxManager1() as example1,
105+
CtxManager2() as example2
106+
):
107+
...
108+
109+
it is also possible to use a trailing comma at the end of the
110+
enclosed group:
111+
112+
.. code-block:: python
113+
114+
with (
115+
CtxManager1() as example1,
116+
CtxManager2() as example2,
117+
CtxManager3() as example3,
118+
):
119+
...
120+
121+
This new syntax uses the non LL(1) capacities of the new parser.
122+
Check :pep:`617` for more details.
123+
124+
(Contributed by Guido van Rossum, Pablo Galindo and Lysandros Nikolaou
125+
in :issue:`12782` and :issue:`40334`.)
126+
127+
75128
PEP 563: Postponed Evaluation of Annotations Becomes Default
76129
------------------------------------------------------------
77130

0 commit comments

Comments
 (0)