Change definition of __match__ (#1477)

It should never raise if an attribute is missing.

Also fix one last mention of the exception for a single positional argument.

See https://github.com/gvanrossum/patma/issues/111#issuecomment-650874268
This commit is contained in:
Guido van Rossum 2020-06-29 09:32:21 -07:00 committed by GitHub
parent 430718e6a5
commit d95ec4b557
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 12 deletions

View File

@ -580,7 +580,7 @@ The procedure is as following:
``ImpossibleMatchError`` is raised.
* If the ``__match_args__`` attribute is absent on the matched class,
but more than one positional item appears in a match,
and one or more positional item appears in a match,
``ImpossibleMatchError`` is also raised. We don't fall back on
using ``__slots__`` or ``__annotations__`` -- "In the face of ambiguity,
refuse the temptation to guess."
@ -588,17 +588,7 @@ The procedure is as following:
* If there are any match-by-keyword items the keywords are looked up
as attributes on the proxy. If the lookup succeeds the value is
matched against the corresponding sub-pattern. If the lookup fails,
two cases are distinguished:
* If an attribute is missing on the proxy and the class being matched
has no ``__match_args__`` attribute, the match
fails. This allows one to write ``case object(name=_)`` to
implement a check for the presence of a given attribute, or ``case
object(name=var)`` to check for its presence and extract its value.
* If an attribute is missing and the class has a ``__match_args__``,
the match fails if the attribute name is in
``__match_args__``, else the match raises ``ImpossibleMatchError``.
the match fails.
Such a protocol favors simplicity of implementation over flexibility and
performance. For other considered alternatives, see `extended matching`_.