From f01b03ec55960c9093f5967be5d1894d75473c0f Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 22 Nov 2020 09:49:51 -0800 Subject: [PATCH] PEP 635: Explain why mapping patterns ignore extra keys (#1721) --- foo.py | 1 + pep-0635.rst | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 foo.py diff --git a/foo.py b/foo.py new file mode 100644 index 000000000..7b01eee34 --- /dev/null +++ b/foo.py @@ -0,0 +1 @@ +"""Test program""" diff --git a/pep-0635.rst b/pep-0635.rst index 3663b4d10..cee869227 100644 --- a/pep-0635.rst +++ b/pep-0635.rst @@ -1003,8 +1003,13 @@ dictionary. Particularly common are string keys. The mapping pattern reflects the common usage of dictionary lookup: it allows the user to extract some values from a mapping by means of constant/known -keys and have the values match given subpatterns. Moreover, the mapping -pattern does not check for the presence of additional keys. Should it be +keys and have the values match given subpatterns. +Extra keys in the subject are ignored even if ``**rest`` is not present. +This is different from sequence patterns, where extra items will cause a +match to fail. But mappings are actually different from sequences: they +have natural structural sub-typing behavior, i.e., passing a dictionary +with extra keys somewhere will likely just work. +Should it be necessary to impose an upper bound on the mapping and ensure that no additional keys are present, then the usual double-star-pattern ``**rest`` can be used. The special case ``**_`` with a wildcard, however, is not