Total Complexity | 7 |
Total Lines | 46 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from __future__ import annotations |
||
2 | |||
3 | import typing |
||
4 | |||
5 | from . import match_dict |
||
6 | from . import match_failure |
||
7 | |||
8 | |||
9 | class Matchable: |
||
10 | """Given a value, attempt to match against a target. |
||
11 | |||
12 | The truthiness of ``Matchable`` values varies on whether they have bindings |
||
13 | associated with them. They are truthy exactly when they have bindings. |
||
14 | |||
15 | ``Matchable`` values provide two basic forms of syntactic sugar. |
||
16 | ``m_able(target)`` is equivalent to ``m_able.match(target)``, and |
||
17 | ``m_able[k]`` will return ``m_able.matches[k]`` if the ``Matchable`` is |
||
18 | truthy, and raise a ``ValueError`` otherwise. |
||
19 | """ |
||
20 | |||
21 | value: typing.Any |
||
22 | matches: typing.Optional[match_dict.MatchDict] |
||
23 | |||
24 | def __init__(self, value: typing.Any): |
||
25 | self.value = value |
||
26 | self.matches = None |
||
27 | |||
28 | def match(self, target) -> Matchable: |
||
29 | """Match against target, generating a set of bindings.""" |
||
30 | try: |
||
31 | self.matches = match_dict.match(target, self.value) |
||
32 | except match_failure.MatchFailure: |
||
33 | self.matches = None |
||
34 | return self |
||
35 | |||
36 | def __call__(self, target) -> Matchable: |
||
37 | return self.match(target) |
||
38 | |||
39 | def __getitem__(self, key): |
||
40 | if self.matches is None: |
||
41 | raise ValueError |
||
42 | return self.matches[key] |
||
43 | |||
44 | def __bool__(self): |
||
45 | return self.matches is not None |
||
46 |