Skip to content

Commit e7d25d3

Browse files
bpo-43977: Update pattern matching language reference docs (GH-25917) (GH-26117)
* Update patma language reference with new changes to sequence and mapping * update 3.10 whatsnew too (cherry picked from commit 53c91ac) Co-authored-by: Ken Jin <[email protected]> Co-authored-by: Ken Jin <[email protected]>
1 parent 3739371 commit e7d25d3

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

Doc/reference/compound_stmts.rst

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ and binds no name. Syntax:
822822

823823
``_`` is a :ref:`soft keyword <soft-keywords>` within any pattern,
824824
but only within patterns. It is an identifier, as usual, even within
825-
``match`` headers, ``guards``, and ``case`` blocks.
825+
``match`` subject expressions, ``guard``\ s, and ``case`` blocks.
826826

827827
In simple terms, ``_`` will always succeed.
828828

@@ -900,8 +900,8 @@ sequence pattern.
900900
The following is the logical flow for matching a sequence pattern against a
901901
subject value:
902902

903-
#. If the subject value is not an instance of a
904-
:class:`collections.abc.Sequence` the sequence pattern fails.
903+
#. If the subject value is not a sequence [#]_, the sequence pattern
904+
fails.
905905

906906
#. If the subject value is an instance of ``str``, ``bytes`` or ``bytearray``
907907
the sequence pattern fails.
@@ -943,7 +943,7 @@ subject value:
943943
In simple terms ``[P1, P2, P3,`` ... ``, P<N>]`` matches only if all the following
944944
happens:
945945

946-
* ``isinstance(<subject>, collections.abc.Sequence)``
946+
* check ``<subject>`` is a sequence
947947
* ``len(subject) == <N>``
948948
* ``P1`` matches ``<subject>[0]`` (note that this match can also bind names)
949949
* ``P2`` matches ``<subject>[1]`` (note that this match can also bind names)
@@ -975,8 +975,7 @@ runtime error and will raise :exc:`ValueError`.)
975975
The following is the logical flow for matching a mapping pattern against a
976976
subject value:
977977

978-
#. If the subject value is not an instance of :class:`collections.abc.Mapping`,
979-
the mapping pattern fails.
978+
#. If the subject value is not a mapping [#]_,the mapping pattern fails.
980979

981980
#. If every key given in the mapping pattern is present in the subject mapping,
982981
and the pattern for each key matches the corresponding item of the subject
@@ -993,7 +992,7 @@ subject value:
993992
In simple terms ``{KEY1: P1, KEY2: P2, ... }`` matches only if all the following
994993
happens:
995994

996-
* ``isinstance(<subject>, collections.abc.Mapping)``
995+
* check ``<subject>`` is a mapping
997996
* ``KEY1 in <subject>``
998997
* ``P1`` matches ``<subject>[KEY1]``
999998
* ... and so on for the corresponding KEY/pattern pair.
@@ -1526,6 +1525,35 @@ body of a coroutine function.
15261525
there is a :keyword:`finally` clause which happens to raise another
15271526
exception. That new exception causes the old one to be lost.
15281527
1528+
.. [#] In pattern matching, a sequence is defined as one of the following:
1529+
1530+
* a class that inherits from :class:`collections.abc.Sequence`
1531+
* a Python class that has been registered as :class:`collections.abc.Sequence`
1532+
* a builtin class that has its (CPython) :data:`Py_TPFLAGS_SEQUENCE` bit set
1533+
* a class that inherits from any of the above
1534+
1535+
The following standard library classes are sequences:
1536+
1537+
* :class:`array.array`
1538+
* :class:`collections.deque`
1539+
* :class:`list`
1540+
* :class:`memoryview`
1541+
* :class:`range`
1542+
* :class:`tuple`
1543+
1544+
.. note:: Subject values of type ``str``, ``bytes``, and ``bytearray``
1545+
do not match sequence patterns.
1546+
1547+
.. [#] In pattern matching, a mapping is defined as one of the following:
1548+
1549+
* a class that inherits from :class:`collections.abc.Mapping`
1550+
* a Python class that has been registered as :class:`collections.abc.Mapping`
1551+
* a builtin class that has its (CPython) :data:`Py_TPFLAGS_MAPPING` bit set
1552+
* a class that inherits from any of the above
1553+
1554+
The standard library classes :class:`dict` and :class:`types.MappingProxyType`
1555+
are mappings.
1556+
15291557
.. [#] A string literal appearing as the first statement in the function body is
15301558
transformed into the function's ``__doc__`` attribute and therefore the
15311559
function's :term:`docstring`.

Doc/whatsnew/3.10.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ Several other key features:
589589
590590
- Like unpacking assignments, tuple and list patterns have exactly the
591591
same meaning and actually match arbitrary sequences. Technically,
592-
the subject must be an instance of ``collections.abc.Sequence``.
592+
the subject must be a sequence.
593593
Therefore, an important exception is that patterns don't match iterators.
594594
Also, to prevent a common mistake, sequence patterns don't match strings.
595595

0 commit comments

Comments
 (0)