PEP 635: Explain why mapping patterns ignore extra keys (#1721)

This commit is contained in:
Guido van Rossum 2020-11-22 09:49:51 -08:00 committed by GitHub
parent 37f5b8a0d3
commit f01b03ec55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

1
foo.py Normal file
View File

@ -0,0 +1 @@
"""Test program"""

View File

@ -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