Skip to content

Commit a8e2615

Browse files
pablogsalwillingc
andauthored
bpo-42128: Add documentation for the new match-based AST nodes (GH-24673)
* bpo-42128: Add documentation for the new match-based AST nodes * Update Doc/library/ast.rst Co-authored-by: Carol Willing <carolcode@willingconsulting.com> * Fix trailing whitespace Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
1 parent 41934b3 commit a8e2615

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

‎Doc/library/ast.rst‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,128 @@ Control flow
12401240
type_ignores=[])
12411241

12421242

1243+
.. class:: Match(subject, cases)
1244+
1245+
A ``match`` statement. ``subject`` holds the subject of the match (the object
1246+
that is being matched against the cases) and ``cases`` contains an iterable of
1247+
:class:`match_case` nodes with the different cases.
1248+
1249+
1250+
.. class:: match_case(context_expr, optional_vars)
1251+
1252+
A single case pattern in a ``match`` statement. ``pattern`` contains the
1253+
match pattern that will be used to match the subject against. Notice that
1254+
the meaning of the :class:`AST` nodes in this attribute have a different
1255+
meaning than in other places, as they represent patterns to match against.
1256+
The ``guard`` attribute contains an expression that will be evaluated if
1257+
the pattern matches the subject. If the pattern matches and the ``guard`` condition
1258+
is truthy, the body of the case shall be executed. ``body`` contains a list
1259+
of nodes to execute if the guard is truthy.
1260+
1261+
.. doctest::
1262+
1263+
>>> print(ast.dump(ast.parse("""
1264+
... match x:
1265+
... case [x] if x>0:
1266+
... ...
1267+
... case tuple():
1268+
... ...
1269+
... """), indent=4))
1270+
Module(
1271+
body=[
1272+
Match(
1273+
subject=Name(id='x', ctx=Load()),
1274+
cases=[
1275+
match_case(
1276+
pattern=List(
1277+
elts=[
1278+
Name(id='x', ctx=Store())],
1279+
ctx=Load()),
1280+
guard=Compare(
1281+
left=Name(id='x', ctx=Load()),
1282+
ops=[
1283+
Gt()],
1284+
comparators=[
1285+
Constant(value=0)]),
1286+
body=[
1287+
Expr(
1288+
value=Constant(value=Ellipsis))]),
1289+
match_case(
1290+
pattern=Call(
1291+
func=Name(id='tuple', ctx=Load()),
1292+
args=[],
1293+
keywords=[]),
1294+
body=[
1295+
Expr(
1296+
value=Constant(value=Ellipsis))])])],
1297+
type_ignores=[])
1298+
1299+
.. class:: MatchAs(pattern, name)
1300+
1301+
A match "as-pattern". The as-pattern matches whatever pattern is on its
1302+
left-hand side, but also binds the value to a name. ``pattern`` contains
1303+
the match pattern that will be used to match the subject agsinst. The ``name``
1304+
attribute contains the name that will be binded if the pattern is successful.
1305+
1306+
.. doctest::
1307+
1308+
>>> print(ast.dump(ast.parse("""
1309+
... match x:
1310+
... case [x] as y:
1311+
... ...
1312+
... """), indent=4))
1313+
Module(
1314+
body=[
1315+
Match(
1316+
subject=Name(id='x', ctx=Load()),
1317+
cases=[
1318+
match_case(
1319+
pattern=MatchAs(
1320+
pattern=List(
1321+
elts=[
1322+
Name(id='x', ctx=Store())],
1323+
ctx=Load()),
1324+
name='y'),
1325+
body=[
1326+
Expr(
1327+
value=Constant(value=Ellipsis))])])],
1328+
type_ignores=[])
1329+
1330+
1331+
.. class:: MatchOr(patterns)
1332+
1333+
A match "or-pattern". An or-pattern matches each of its subpatterns in turn
1334+
to the subject, until one succeeds. The or-pattern is then deemed to
1335+
succeed. If none of the subpatterns succeed the or-pattern fails. The
1336+
``patterns`` attribute contains a list of match patterns nodes that will be
1337+
matched against the subject.
1338+
1339+
.. doctest::
1340+
1341+
>>> print(ast.dump(ast.parse("""
1342+
... match x:
1343+
... case [x] | (y):
1344+
... ...
1345+
... """), indent=4))
1346+
Module(
1347+
body=[
1348+
Match(
1349+
subject=Name(id='x', ctx=Load()),
1350+
cases=[
1351+
match_case(
1352+
pattern=MatchOr(
1353+
patterns=[
1354+
List(
1355+
elts=[
1356+
Name(id='x', ctx=Store())],
1357+
ctx=Load()),
1358+
Name(id='y', ctx=Store())]),
1359+
body=[
1360+
Expr(
1361+
value=Constant(value=Ellipsis))])])],
1362+
type_ignores=[])
1363+
1364+
12431365
Function and class definitions
12441366
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12451367

0 commit comments

Comments
 (0)