vcti-predicate 2.0.1__tar.gz → 2.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {vcti_predicate-2.0.1/src/vcti_predicate.egg-info → vcti_predicate-2.2.0}/PKG-INFO +31 -3
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/README.md +30 -2
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/pyproject.toml +1 -1
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/__init__.py +20 -0
- vcti_predicate-2.2.0/src/vcti/predicate/errors.py +134 -0
- vcti_predicate-2.2.0/src/vcti/predicate/evaluator.py +287 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/group.py +23 -7
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/comparison.py +4 -2
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/range.py +2 -1
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/regex.py +9 -4
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/registry.py +58 -13
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/symbol.py +5 -3
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0/src/vcti_predicate.egg-info}/PKG-INFO +31 -3
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti_predicate.egg-info/SOURCES.txt +3 -0
- vcti_predicate-2.2.0/tests/test_applicability.py +152 -0
- vcti_predicate-2.2.0/tests/test_errors.py +385 -0
- vcti_predicate-2.0.1/src/vcti/predicate/evaluator.py +0 -185
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/LICENSE +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/setup.cfg +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/operator.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/__init__.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/collection.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/string.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/unary.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti/predicate/py.typed +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti_predicate.egg-info/dependency_links.txt +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti_predicate.egg-info/requires.txt +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/src/vcti_predicate.egg-info/top_level.txt +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_docs.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_evaluator.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_examples.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_group.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_negation_pairs.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_operator.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_operators_collection.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_operators_comparison.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_operators_range.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_operators_regex.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_operators_string.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_operators_unary.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_registry.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_symbol.py +0 -0
- {vcti_predicate-2.0.1 → vcti_predicate-2.2.0}/tests/test_version.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: vcti-predicate
|
|
3
|
-
Version: 2.0
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: VCollab Predicate - condition evaluation engine with extensible operators and modifiers
|
|
5
5
|
Author: Visual Collaboration Technologies Inc.
|
|
6
6
|
License-Expression: LicenseRef-Proprietary
|
|
@@ -49,14 +49,14 @@ pip install vcti-predicate
|
|
|
49
49
|
### In `requirements.txt`
|
|
50
50
|
|
|
51
51
|
```
|
|
52
|
-
vcti-predicate>=2.0
|
|
52
|
+
vcti-predicate>=2.2.0
|
|
53
53
|
```
|
|
54
54
|
|
|
55
55
|
### In `pyproject.toml` dependencies
|
|
56
56
|
|
|
57
57
|
```toml
|
|
58
58
|
dependencies = [
|
|
59
|
-
"vcti-predicate>=2.0
|
|
59
|
+
"vcti-predicate>=2.2.0",
|
|
60
60
|
]
|
|
61
61
|
```
|
|
62
62
|
|
|
@@ -197,6 +197,34 @@ no modifiers.
|
|
|
197
197
|
|
|
198
198
|
---
|
|
199
199
|
|
|
200
|
+
## Errors
|
|
201
|
+
|
|
202
|
+
`check()` and `evaluate()` raise `NotApplicableError` when the
|
|
203
|
+
condition does not apply to a value of this type; the same condition
|
|
204
|
+
works for another type. It has two subclasses:
|
|
205
|
+
|
|
206
|
+
- `UnsupportedOperandError`: the symbol has no operator for the value's
|
|
207
|
+
type (`None` with `"^="`).
|
|
208
|
+
- `InapplicableArgumentError`: a keyword another operator of the symbol
|
|
209
|
+
accepts, but not the one this type resolved to (`None` with `"=="` and
|
|
210
|
+
`case_sensitive=True`).
|
|
211
|
+
|
|
212
|
+
A condition that is wrong raises `UnknownSymbolError`, `ArityError`,
|
|
213
|
+
`InvalidArgumentError` (including a keyword no operator of the symbol
|
|
214
|
+
accepts) or `ArgumentTypeError`; none of these means "does not apply".
|
|
215
|
+
A keyword's type is checked even where it does not apply, but a range
|
|
216
|
+
checked inside the operator that accepts it is not: for `None`,
|
|
217
|
+
`tolerance=-1` raises `InapplicableArgumentError`, not
|
|
218
|
+
`InvalidArgumentError`. Keywords that no single operator accepts
|
|
219
|
+
together (`case_sensitive` with `tolerance` on `"=="`) are never
|
|
220
|
+
"does not apply" for a value that resolves to an operator; they raise
|
|
221
|
+
`InvalidArgumentError`. Every concrete class derives from
|
|
222
|
+
`PredicateError` and from `ValueError` (`ArgumentTypeError` from
|
|
223
|
+
`TypeError`), so existing handlers keep working; the bases
|
|
224
|
+
`PredicateError` and `NotApplicableError` derive only from `Exception`.
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
200
228
|
## Dependencies
|
|
201
229
|
|
|
202
230
|
None. `vcti-predicate` uses only the Python standard library.
|
|
@@ -25,14 +25,14 @@ pip install vcti-predicate
|
|
|
25
25
|
### In `requirements.txt`
|
|
26
26
|
|
|
27
27
|
```
|
|
28
|
-
vcti-predicate>=2.0
|
|
28
|
+
vcti-predicate>=2.2.0
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
### In `pyproject.toml` dependencies
|
|
32
32
|
|
|
33
33
|
```toml
|
|
34
34
|
dependencies = [
|
|
35
|
-
"vcti-predicate>=2.0
|
|
35
|
+
"vcti-predicate>=2.2.0",
|
|
36
36
|
]
|
|
37
37
|
```
|
|
38
38
|
|
|
@@ -173,6 +173,34 @@ no modifiers.
|
|
|
173
173
|
|
|
174
174
|
---
|
|
175
175
|
|
|
176
|
+
## Errors
|
|
177
|
+
|
|
178
|
+
`check()` and `evaluate()` raise `NotApplicableError` when the
|
|
179
|
+
condition does not apply to a value of this type; the same condition
|
|
180
|
+
works for another type. It has two subclasses:
|
|
181
|
+
|
|
182
|
+
- `UnsupportedOperandError`: the symbol has no operator for the value's
|
|
183
|
+
type (`None` with `"^="`).
|
|
184
|
+
- `InapplicableArgumentError`: a keyword another operator of the symbol
|
|
185
|
+
accepts, but not the one this type resolved to (`None` with `"=="` and
|
|
186
|
+
`case_sensitive=True`).
|
|
187
|
+
|
|
188
|
+
A condition that is wrong raises `UnknownSymbolError`, `ArityError`,
|
|
189
|
+
`InvalidArgumentError` (including a keyword no operator of the symbol
|
|
190
|
+
accepts) or `ArgumentTypeError`; none of these means "does not apply".
|
|
191
|
+
A keyword's type is checked even where it does not apply, but a range
|
|
192
|
+
checked inside the operator that accepts it is not: for `None`,
|
|
193
|
+
`tolerance=-1` raises `InapplicableArgumentError`, not
|
|
194
|
+
`InvalidArgumentError`. Keywords that no single operator accepts
|
|
195
|
+
together (`case_sensitive` with `tolerance` on `"=="`) are never
|
|
196
|
+
"does not apply" for a value that resolves to an operator; they raise
|
|
197
|
+
`InvalidArgumentError`. Every concrete class derives from
|
|
198
|
+
`PredicateError` and from `ValueError` (`ArgumentTypeError` from
|
|
199
|
+
`TypeError`), so existing handlers keep working; the bases
|
|
200
|
+
`PredicateError` and `NotApplicableError` derive only from `Exception`.
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
176
204
|
## Dependencies
|
|
177
205
|
|
|
178
206
|
None. `vcti-predicate` uses only the Python standard library.
|
|
@@ -13,6 +13,17 @@ isolated set for customization.
|
|
|
13
13
|
|
|
14
14
|
from importlib.metadata import version
|
|
15
15
|
|
|
16
|
+
from .errors import (
|
|
17
|
+
ArgumentTypeError,
|
|
18
|
+
ArityError,
|
|
19
|
+
InapplicableArgumentError,
|
|
20
|
+
InvalidArgumentError,
|
|
21
|
+
NotApplicableError,
|
|
22
|
+
PredicateError,
|
|
23
|
+
RegistrationError,
|
|
24
|
+
UnknownSymbolError,
|
|
25
|
+
UnsupportedOperandError,
|
|
26
|
+
)
|
|
16
27
|
from .evaluator import check, default_registry, evaluate
|
|
17
28
|
from .group import OperatorGroup
|
|
18
29
|
from .operator import Operator
|
|
@@ -30,4 +41,13 @@ __all__ = [
|
|
|
30
41
|
"OperatorGroup",
|
|
31
42
|
"OperatorRegistry",
|
|
32
43
|
"Symbol",
|
|
44
|
+
"PredicateError",
|
|
45
|
+
"NotApplicableError",
|
|
46
|
+
"UnsupportedOperandError",
|
|
47
|
+
"InapplicableArgumentError",
|
|
48
|
+
"UnknownSymbolError",
|
|
49
|
+
"ArityError",
|
|
50
|
+
"InvalidArgumentError",
|
|
51
|
+
"ArgumentTypeError",
|
|
52
|
+
"RegistrationError",
|
|
33
53
|
]
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# Copyright Visual Collaboration Technologies Inc. All Rights Reserved.
|
|
2
|
+
# See LICENSE for details.
|
|
3
|
+
"""Exceptions raised by evaluation and registration.
|
|
4
|
+
|
|
5
|
+
Every concrete class also derives from the built-in exception the package
|
|
6
|
+
raised before these existed: ``ArgumentTypeError`` from ``TypeError``, all
|
|
7
|
+
the others from ``ValueError``. So existing ``except ValueError`` and
|
|
8
|
+
``except TypeError`` handlers keep working. The two bases,
|
|
9
|
+
:class:`PredicateError` and :class:`NotApplicableError`, derive only from
|
|
10
|
+
``Exception``: catching either by name is the new, precise option. The
|
|
11
|
+
split exists so a caller can tell a condition that does not apply to a
|
|
12
|
+
value from a condition that is wrong in itself:
|
|
13
|
+
|
|
14
|
+
- :class:`NotApplicableError` means the condition does not apply to a
|
|
15
|
+
value of this type; the same condition succeeds for another type. Code
|
|
16
|
+
that treats "does not apply to this value" as "does not match" catches
|
|
17
|
+
this class, and only this class. It has two subclasses:
|
|
18
|
+
|
|
19
|
+
- :class:`UnsupportedOperandError`: the symbol has no operator for the
|
|
20
|
+
type of the left-hand operand (``None`` with ``"^="``).
|
|
21
|
+
- :class:`InapplicableArgumentError`: a keyword some operator of the
|
|
22
|
+
symbol accepts, but not the one this type resolved to (``None`` with
|
|
23
|
+
``"=="`` and ``case_sensitive=True``; an ``int`` with ``tolerance``).
|
|
24
|
+
|
|
25
|
+
- The others report a condition that is wrong: :class:`UnknownSymbolError`,
|
|
26
|
+
:class:`ArityError`, :class:`InvalidArgumentError` (a keyword no
|
|
27
|
+
operator of the symbol accepts, an invalid value, a bad pattern),
|
|
28
|
+
:class:`ArgumentTypeError`, and :class:`RegistrationError` from the
|
|
29
|
+
registry. They are raised against the operator the value's type selected,
|
|
30
|
+
so not all are independent of the value; only an unknown symbol is
|
|
31
|
+
reported the same way for every value.
|
|
32
|
+
|
|
33
|
+
A keyword's type is checked even when it does not apply, against the type
|
|
34
|
+
the accepting operator declares, so ``case_sensitive="false"`` is an
|
|
35
|
+
:class:`ArgumentTypeError` for any value that resolves to an operator.
|
|
36
|
+
(Resolution comes first, so a value with no operator at all still reports
|
|
37
|
+
:class:`UnsupportedOperandError`.) Its *range* is not: a check
|
|
38
|
+
made inside the accepting operator, such as ``tolerance >= 0``, does not
|
|
39
|
+
run when that operator does not, so ``check(None, "==", 1.0,
|
|
40
|
+
tolerance=-1)`` raises :class:`InapplicableArgumentError` while a ``float``
|
|
41
|
+
raises :class:`InvalidArgumentError`.
|
|
42
|
+
|
|
43
|
+
Resolution comes first: when the symbol has no operator for the operand's
|
|
44
|
+
type, :class:`UnsupportedOperandError` is raised before the arguments are
|
|
45
|
+
checked, so ``check(None, "^=", "a", bogus=True)`` reports the missing
|
|
46
|
+
operator, not the unknown keyword.
|
|
47
|
+
|
|
48
|
+
An operator may still raise a plain ``TypeError`` for operands it cannot
|
|
49
|
+
compare (``check(None, "<", 1)``, a non-container with ``"?="``), as the
|
|
50
|
+
underlying Python operation does. Errors in *defining* a group, operator
|
|
51
|
+
or symbol are plain ``TypeError`` / ``ValueError`` raised by its
|
|
52
|
+
constructor.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class PredicateError(Exception):
|
|
57
|
+
"""Base class of the exceptions this package raises deliberately."""
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class NotApplicableError(PredicateError):
|
|
61
|
+
"""The condition does not apply to a value of this type.
|
|
62
|
+
|
|
63
|
+
The base of the errors that mean "this condition is not for this value"
|
|
64
|
+
rather than "this condition is wrong": the same condition succeeds for a
|
|
65
|
+
left-hand operand of another type. Code that treats "does not apply" as
|
|
66
|
+
"does not match" catches this class.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class UnsupportedOperandError(NotApplicableError, ValueError):
|
|
71
|
+
"""The symbol has no operator for the operand's type.
|
|
72
|
+
|
|
73
|
+
Raised by resolution, before any argument is checked: the same
|
|
74
|
+
condition can succeed for a left-hand operand of another type.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class UnknownSymbolError(PredicateError, ValueError):
|
|
79
|
+
"""The symbol, or the operator named with ``operator=``, is not registered."""
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class ArityError(PredicateError, ValueError):
|
|
83
|
+
"""The number of operands does not match the resolved operator's arity."""
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class InvalidArgumentError(PredicateError, ValueError):
|
|
87
|
+
"""An argument is not accepted or not valid.
|
|
88
|
+
|
|
89
|
+
A keyword argument the operator and its group do not accept, a modifier
|
|
90
|
+
value outside its domain (a negative ``tolerance``), bounds that are not
|
|
91
|
+
a pair, or a regular expression that does not compile.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class InapplicableArgumentError(NotApplicableError, InvalidArgumentError):
|
|
96
|
+
"""Keywords another operator of the symbol accepts, but not the one this type resolved to.
|
|
97
|
+
|
|
98
|
+
``check(None, "==", "A", case_sensitive=True)``: ``case_sensitive`` is
|
|
99
|
+
accepted by the string operator of ``"=="``, but ``None`` resolves to
|
|
100
|
+
the fallback, which takes no modifiers. The condition must be one some
|
|
101
|
+
other type would run: an operator that a type can actually resolve to
|
|
102
|
+
must accept it whole, meaning *all* the keywords passed and the same
|
|
103
|
+
number of operands. ``case_sensitive`` together with ``tolerance`` is
|
|
104
|
+
accepted by no operator of ``"=="``; a keyword that only an operator of
|
|
105
|
+
another arity, or an operator no type reaches, accepts fits nothing
|
|
106
|
+
either. Such a condition is never a :class:`NotApplicableError` for a
|
|
107
|
+
value that resolves to an operator: it raises plain
|
|
108
|
+
:class:`InvalidArgumentError`, or :class:`ArityError` where the value's
|
|
109
|
+
operator takes a different number of operands. The values have already been
|
|
110
|
+
checked against the type the accepting operator declares, so a wrong
|
|
111
|
+
type still raises :class:`ArgumentTypeError`. A keyword that no
|
|
112
|
+
operator of the symbol accepts, such as a typo, raises plain
|
|
113
|
+
:class:`InvalidArgumentError`.
|
|
114
|
+
|
|
115
|
+
A value whose type is right but whose range is checked by the accepting
|
|
116
|
+
operator itself (a negative ``tolerance``) is not checked: that
|
|
117
|
+
operator does not run.
|
|
118
|
+
"""
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class ArgumentTypeError(PredicateError, TypeError):
|
|
122
|
+
"""A modifier, option or pattern is of the wrong type.
|
|
123
|
+
|
|
124
|
+
For example ``case_sensitive="false"``, ``multiline=1``, or a compiled
|
|
125
|
+
bytes pattern.
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class RegistrationError(PredicateError, ValueError):
|
|
130
|
+
"""A registration was refused: a name collision, or a replacement that
|
|
131
|
+
would leave an operator name meaning two implementations.
|
|
132
|
+
|
|
133
|
+
Nothing in the registry changes when this is raised.
|
|
134
|
+
"""
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# Copyright Visual Collaboration Technologies Inc. All Rights Reserved.
|
|
2
|
+
# See LICENSE for details.
|
|
3
|
+
"""Public entry point for predicate evaluation."""
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from .errors import (
|
|
8
|
+
ArgumentTypeError,
|
|
9
|
+
ArityError,
|
|
10
|
+
InapplicableArgumentError,
|
|
11
|
+
InvalidArgumentError,
|
|
12
|
+
)
|
|
13
|
+
from .group import OperatorGroup, check_value_type
|
|
14
|
+
from .operator import Operator
|
|
15
|
+
from .operators import create_default_registry
|
|
16
|
+
from .registry import OperatorRegistry
|
|
17
|
+
|
|
18
|
+
_UNSET = object()
|
|
19
|
+
|
|
20
|
+
default_registry: OperatorRegistry = create_default_registry()
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def check(
|
|
24
|
+
lhs: Any,
|
|
25
|
+
op: str,
|
|
26
|
+
rhs: Any = _UNSET,
|
|
27
|
+
*,
|
|
28
|
+
operator: str | None = None,
|
|
29
|
+
registry: OperatorRegistry = default_registry,
|
|
30
|
+
**kwargs: Any,
|
|
31
|
+
) -> bool:
|
|
32
|
+
"""Check if a condition holds.
|
|
33
|
+
|
|
34
|
+
For binary symbols, pass ``lhs``, ``op``, and ``rhs``.
|
|
35
|
+
For unary symbols, pass ``lhs`` and ``op`` only.
|
|
36
|
+
|
|
37
|
+
The symbol ``op`` is resolved to a concrete operator based on the
|
|
38
|
+
type of ``lhs``. Group modifiers and operator-specific options are
|
|
39
|
+
passed as keyword arguments.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
lhs: The value to test.
|
|
43
|
+
op: Symbol name (e.g., "==", "^=", "is_empty", "is_file").
|
|
44
|
+
rhs: Second operand. Required for binary symbols, omit for
|
|
45
|
+
unary symbols.
|
|
46
|
+
operator: Explicit operator name, bypassing type-based
|
|
47
|
+
resolution.
|
|
48
|
+
registry: Operator registry to use.
|
|
49
|
+
**kwargs: Group modifiers and/or operator-specific options.
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
True if the condition holds, False otherwise.
|
|
53
|
+
|
|
54
|
+
Raises:
|
|
55
|
+
UnsupportedOperandError: If the symbol has no operator for the
|
|
56
|
+
LHS type (resolution found no operator to run).
|
|
57
|
+
InapplicableArgumentError: If another operator of the symbol
|
|
58
|
+
takes the same number of operands and accepts all the keywords
|
|
59
|
+
passed, but the resolved one does not.
|
|
60
|
+
With
|
|
61
|
+
``UnsupportedOperandError``, a ``NotApplicableError``: the
|
|
62
|
+
condition does not apply to this value's type.
|
|
63
|
+
UnknownSymbolError: If the symbol, or ``operator``, is unknown.
|
|
64
|
+
ArityError: If the number of operands does not match the
|
|
65
|
+
resolved operator.
|
|
66
|
+
InvalidArgumentError: If a keyword argument is accepted by no
|
|
67
|
+
operator of the symbol, or a modifier value is invalid (a
|
|
68
|
+
negative ``tolerance``, a regex that does not compile).
|
|
69
|
+
ArgumentTypeError: If a modifier or option has the wrong type
|
|
70
|
+
(e.g. ``case_sensitive="false"``).
|
|
71
|
+
TypeError: From the operator itself when it cannot compare the
|
|
72
|
+
operands, as the Python operation would (``None < 1``).
|
|
73
|
+
|
|
74
|
+
``ArgumentTypeError`` is a ``TypeError`` and every other class above a
|
|
75
|
+
``ValueError``, so handlers for the built-in types still work. Catch
|
|
76
|
+
``NotApplicableError`` for "does not apply to this value". See
|
|
77
|
+
:mod:`vcti.predicate.errors`.
|
|
78
|
+
|
|
79
|
+
Examples:
|
|
80
|
+
>>> check("Hello", "==", "hello")
|
|
81
|
+
True
|
|
82
|
+
>>> check(5, ">", 3)
|
|
83
|
+
True
|
|
84
|
+
>>> check(1.0, "==", 1.001, tolerance=0.01)
|
|
85
|
+
True
|
|
86
|
+
>>> check("Hello", "==", "hello", case_sensitive=True)
|
|
87
|
+
False
|
|
88
|
+
>>> check("", "is_empty")
|
|
89
|
+
True
|
|
90
|
+
>>> check(path, "is_file")
|
|
91
|
+
True
|
|
92
|
+
"""
|
|
93
|
+
args = (lhs,) if rhs is _UNSET else (lhs, rhs)
|
|
94
|
+
return evaluate(op, *args, operator=operator, registry=registry, **kwargs)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def evaluate(
|
|
98
|
+
op: str,
|
|
99
|
+
*args: Any,
|
|
100
|
+
operator: str | None = None,
|
|
101
|
+
registry: OperatorRegistry = default_registry,
|
|
102
|
+
**kwargs: Any,
|
|
103
|
+
) -> bool:
|
|
104
|
+
"""Evaluate a predicate condition (symbol-first, extensible arity).
|
|
105
|
+
|
|
106
|
+
Generic entry point used by rule engines and programmatic callers.
|
|
107
|
+
Accepts any number of operands — arity is validated against the
|
|
108
|
+
resolved operator. For interactive use, prefer :func:`check` which
|
|
109
|
+
has value-first argument order.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
op: Symbol name (e.g., "==", "^=", "~=", "is_empty").
|
|
113
|
+
*args: Operands. The first operand is used for type-based
|
|
114
|
+
symbol resolution. The number of operands must match the
|
|
115
|
+
resolved operator's arity.
|
|
116
|
+
operator: Explicit operator name, bypassing type-based
|
|
117
|
+
resolution.
|
|
118
|
+
registry: Operator registry to use.
|
|
119
|
+
**kwargs: Group modifiers and/or operator-specific options.
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
True if the condition holds, False otherwise.
|
|
123
|
+
|
|
124
|
+
Raises:
|
|
125
|
+
UnsupportedOperandError: If the symbol has no operator for the
|
|
126
|
+
first operand's type (resolution found no operator to run).
|
|
127
|
+
InapplicableArgumentError: If another operator of the symbol
|
|
128
|
+
takes the same number of operands and accepts all the keywords
|
|
129
|
+
passed, but the resolved one does not.
|
|
130
|
+
With
|
|
131
|
+
``UnsupportedOperandError``, a ``NotApplicableError``: the
|
|
132
|
+
condition does not apply to this value's type.
|
|
133
|
+
UnknownSymbolError: If the symbol, or ``operator``, is unknown.
|
|
134
|
+
ArityError: If the number of operands does not match the
|
|
135
|
+
resolved operator.
|
|
136
|
+
InvalidArgumentError: If a keyword argument is accepted by no
|
|
137
|
+
operator of the symbol, or a modifier value is invalid (a
|
|
138
|
+
negative ``tolerance``, a regex that does not compile).
|
|
139
|
+
ArgumentTypeError: If a modifier or option has the wrong type
|
|
140
|
+
(e.g. ``case_sensitive="false"``).
|
|
141
|
+
TypeError: From the operator itself when it cannot compare the
|
|
142
|
+
operands, as the Python operation would (``None < 1``).
|
|
143
|
+
|
|
144
|
+
``ArgumentTypeError`` is a ``TypeError`` and every other class above a
|
|
145
|
+
``ValueError``, so handlers for the built-in types still work. Catch
|
|
146
|
+
``NotApplicableError`` for "does not apply to this value". See
|
|
147
|
+
:mod:`vcti.predicate.errors`.
|
|
148
|
+
"""
|
|
149
|
+
if not args:
|
|
150
|
+
raise ArityError("evaluate() requires at least one operand")
|
|
151
|
+
|
|
152
|
+
lhs = args[0]
|
|
153
|
+
|
|
154
|
+
# ── Resolve symbol → operator ──
|
|
155
|
+
resolved = registry.resolve(op, lhs, operator_name=operator)
|
|
156
|
+
group = resolved.group
|
|
157
|
+
|
|
158
|
+
# ── Validate arity ──
|
|
159
|
+
if len(args) != resolved.arity:
|
|
160
|
+
arity_label = {1: "unary", 2: "binary"}.get(resolved.arity, f"{resolved.arity}-ary")
|
|
161
|
+
raise ArityError(
|
|
162
|
+
f"Symbol '{op}' resolved to '{resolved.name}' which is "
|
|
163
|
+
f"{arity_label} — expected {resolved.arity} operand(s), "
|
|
164
|
+
f"got {len(args)}"
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
# ── Route kwargs ──
|
|
168
|
+
group_modifiers, op_options, unknown = _route_kwargs(kwargs, group, resolved)
|
|
169
|
+
if unknown:
|
|
170
|
+
# Only symbol dispatch has a set of sibling operators to compare
|
|
171
|
+
# with; an explicit operator= has none.
|
|
172
|
+
schemas = registry._argument_schemas(op) if operator is None else []
|
|
173
|
+
_raise_unknown_kwargs(op, resolved, unknown, group, kwargs, args, schemas)
|
|
174
|
+
for key, value in group_modifiers.items():
|
|
175
|
+
group.check_modifier(key, value)
|
|
176
|
+
for key, value in op_options.items():
|
|
177
|
+
resolved.check_option(key, value)
|
|
178
|
+
|
|
179
|
+
# ── Apply group defaults ──
|
|
180
|
+
active_modifiers = {**group.defaults, **group_modifiers}
|
|
181
|
+
|
|
182
|
+
# ── Normalize operands via group ──
|
|
183
|
+
norm_args = tuple(group.normalize(arg, active_modifiers) for arg in args)
|
|
184
|
+
|
|
185
|
+
# ── Build forwarded kwargs ──
|
|
186
|
+
fwd = {**active_modifiers, **op_options}
|
|
187
|
+
|
|
188
|
+
# ── Call operator ──
|
|
189
|
+
return resolved.fn(*norm_args, **fwd)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def _route_kwargs(
|
|
193
|
+
kwargs: dict[str, Any],
|
|
194
|
+
group: OperatorGroup,
|
|
195
|
+
op: Operator,
|
|
196
|
+
) -> tuple[dict[str, Any], dict[str, Any], set[str]]:
|
|
197
|
+
"""Split kwargs into group modifiers, operator options, and unknown."""
|
|
198
|
+
group_mods: dict[str, Any] = {}
|
|
199
|
+
op_opts: dict[str, Any] = {}
|
|
200
|
+
unknown: set[str] = set()
|
|
201
|
+
|
|
202
|
+
for key, value in kwargs.items():
|
|
203
|
+
if key in group.modifiers:
|
|
204
|
+
group_mods[key] = value
|
|
205
|
+
elif key in op.options:
|
|
206
|
+
op_opts[key] = value
|
|
207
|
+
else:
|
|
208
|
+
unknown.add(key)
|
|
209
|
+
|
|
210
|
+
return group_mods, op_opts, unknown
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _raise_unknown_kwargs(
|
|
214
|
+
symbol_name: str,
|
|
215
|
+
op: Operator,
|
|
216
|
+
unknown: set[str],
|
|
217
|
+
group: OperatorGroup,
|
|
218
|
+
kwargs: dict[str, Any],
|
|
219
|
+
args: tuple[Any, ...],
|
|
220
|
+
schemas: list[tuple[int, dict[str, type]]],
|
|
221
|
+
) -> None:
|
|
222
|
+
"""Raise the right error for keywords the resolved operator does not accept.
|
|
223
|
+
|
|
224
|
+
The condition only "does not apply" to this value if some other
|
|
225
|
+
operator of the symbol would accept it as written: the same number of
|
|
226
|
+
operands, every keyword together, each with a value of the declared
|
|
227
|
+
type. Checked in order:
|
|
228
|
+
|
|
229
|
+
1. A keyword no operator of the symbol accepts (a typo):
|
|
230
|
+
InvalidArgumentError.
|
|
231
|
+
2. No operator with this number of operands accepts all the keywords
|
|
232
|
+
together (``case_sensitive`` with ``tolerance`` on ``"=="``, or a
|
|
233
|
+
keyword only an operator of another arity takes):
|
|
234
|
+
InvalidArgumentError. No value could satisfy the condition.
|
|
235
|
+
3. No operator accepting the full set accepts all the values' types:
|
|
236
|
+
ArgumentTypeError.
|
|
237
|
+
4. Otherwise: InapplicableArgumentError.
|
|
238
|
+
|
|
239
|
+
Args:
|
|
240
|
+
args: The operands, whose count a candidate operator must match.
|
|
241
|
+
schemas: One ``(arity, keyword-to-type schema)`` pair per operator
|
|
242
|
+
of the symbol, from ``OperatorRegistry._argument_schemas``. Empty for an
|
|
243
|
+
explicit ``operator=``, which has no siblings.
|
|
244
|
+
"""
|
|
245
|
+
resolved_to = f"Symbol '{symbol_name}' resolved to '{op.name}' ({group.name} group)"
|
|
246
|
+
accepted_here = sorted(set(group.modifiers) | set(op.options))
|
|
247
|
+
accepted_by_any = {key for _, schema in schemas for key in schema}
|
|
248
|
+
|
|
249
|
+
unknown_to_symbol = sorted(k for k in unknown if k not in accepted_by_any)
|
|
250
|
+
if unknown_to_symbol:
|
|
251
|
+
if accepted_here:
|
|
252
|
+
raise InvalidArgumentError(
|
|
253
|
+
f"{resolved_to} which does not accept: {unknown_to_symbol}. "
|
|
254
|
+
f"Accepted: {accepted_here}"
|
|
255
|
+
)
|
|
256
|
+
raise InvalidArgumentError(
|
|
257
|
+
f"{resolved_to} which does not accept any keyword arguments; "
|
|
258
|
+
f"not accepted by any operator of the symbol: {unknown_to_symbol}"
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
passed = set(kwargs)
|
|
262
|
+
candidates = [
|
|
263
|
+
schema for arity, schema in schemas if arity == len(args) and passed <= set(schema)
|
|
264
|
+
]
|
|
265
|
+
if not candidates:
|
|
266
|
+
raise InvalidArgumentError(
|
|
267
|
+
f"No operator of symbol '{symbol_name}' taking {len(args)} operand(s) accepts "
|
|
268
|
+
f"{sorted(passed)} together, so the condition is invalid for every value"
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
first_error: ArgumentTypeError | None = None
|
|
272
|
+
for schema in candidates:
|
|
273
|
+
try:
|
|
274
|
+
for key in sorted(passed):
|
|
275
|
+
check_value_type(
|
|
276
|
+
f"Modifier '{key}' of symbol '{symbol_name}'", kwargs[key], schema[key]
|
|
277
|
+
)
|
|
278
|
+
except ArgumentTypeError as exc:
|
|
279
|
+
first_error = first_error or exc
|
|
280
|
+
continue
|
|
281
|
+
raise InapplicableArgumentError(
|
|
282
|
+
f"{resolved_to} for type '{type(args[0]).__name__}', which does not accept: "
|
|
283
|
+
f"{sorted(unknown)}. Another operator of the symbol accepts the condition's "
|
|
284
|
+
f"keywords, so the condition does not apply to this value"
|
|
285
|
+
)
|
|
286
|
+
assert first_error is not None
|
|
287
|
+
raise first_error
|
|
@@ -8,6 +8,8 @@ from pathlib import Path
|
|
|
8
8
|
from types import MappingProxyType
|
|
9
9
|
from typing import Any
|
|
10
10
|
|
|
11
|
+
from .errors import ArgumentTypeError
|
|
12
|
+
|
|
11
13
|
|
|
12
14
|
@dataclass(frozen=True, eq=False)
|
|
13
15
|
class OperatorGroup:
|
|
@@ -53,8 +55,15 @@ class OperatorGroup:
|
|
|
53
55
|
raise ValueError(
|
|
54
56
|
f"Group '{self.name}' has defaults for undeclared modifiers: {sorted(unknown)}"
|
|
55
57
|
)
|
|
58
|
+
# A wrong default is a definition error: plain TypeError, unlike a
|
|
59
|
+
# wrong value passed to check(), which is ArgumentTypeError.
|
|
56
60
|
for key, value in self.defaults.items():
|
|
57
|
-
|
|
61
|
+
check_value_type(
|
|
62
|
+
f"Default for modifier '{key}' of the {self.name} group",
|
|
63
|
+
value,
|
|
64
|
+
self.modifiers[key],
|
|
65
|
+
error=TypeError,
|
|
66
|
+
)
|
|
58
67
|
|
|
59
68
|
def __hash__(self) -> int:
|
|
60
69
|
return hash(self.name)
|
|
@@ -109,8 +118,13 @@ def check_declarations(owner: str, field_name: str, declared: Any) -> None:
|
|
|
109
118
|
)
|
|
110
119
|
|
|
111
120
|
|
|
112
|
-
def check_value_type(
|
|
113
|
-
|
|
121
|
+
def check_value_type(
|
|
122
|
+
what: str,
|
|
123
|
+
value: Any,
|
|
124
|
+
expected: type,
|
|
125
|
+
error: type[TypeError] = ArgumentTypeError,
|
|
126
|
+
) -> None:
|
|
127
|
+
"""Raise ``error`` if ``value`` is not of the ``expected`` declared type.
|
|
114
128
|
|
|
115
129
|
The rule shared by group modifiers and operator options: a ``bool`` is
|
|
116
130
|
accepted only where ``bool`` is declared (it is an ``int`` subclass, but
|
|
@@ -121,9 +135,13 @@ def check_value_type(what: str, value: Any, expected: type) -> None:
|
|
|
121
135
|
what: Names the argument in the error message.
|
|
122
136
|
value: The value passed.
|
|
123
137
|
expected: The declared type.
|
|
138
|
+
error: The exception to raise. ``ArgumentTypeError`` for a value
|
|
139
|
+
passed during evaluation; plain ``TypeError`` when a group's
|
|
140
|
+
own definition is being checked, since definition errors are
|
|
141
|
+
built-in exceptions.
|
|
124
142
|
|
|
125
143
|
Raises:
|
|
126
|
-
TypeError:
|
|
144
|
+
TypeError: ``error``, if the value's type does not match.
|
|
127
145
|
"""
|
|
128
146
|
if isinstance(value, bool) and expected is not bool:
|
|
129
147
|
ok = False
|
|
@@ -132,9 +150,7 @@ def check_value_type(what: str, value: Any, expected: type) -> None:
|
|
|
132
150
|
else:
|
|
133
151
|
ok = isinstance(value, expected)
|
|
134
152
|
if not ok:
|
|
135
|
-
raise
|
|
136
|
-
f"{what} expects {expected.__name__}, got {type(value).__name__}: {value!r}"
|
|
137
|
-
)
|
|
153
|
+
raise error(f"{what} expects {expected.__name__}, got {type(value).__name__}: {value!r}")
|
|
138
154
|
|
|
139
155
|
|
|
140
156
|
# ── Built-in groups ──
|
|
@@ -6,6 +6,7 @@ import math
|
|
|
6
6
|
import numbers
|
|
7
7
|
from typing import Any
|
|
8
8
|
|
|
9
|
+
from ..errors import InvalidArgumentError
|
|
9
10
|
from ..group import default_group, float_group, int_group, string_group
|
|
10
11
|
from ..operator import Operator
|
|
11
12
|
from ..registry import OperatorRegistry
|
|
@@ -19,7 +20,8 @@ def _float_eq(x: Any, y: Any, **opts: Any) -> bool:
|
|
|
19
20
|
unequal to everything, as in Python.
|
|
20
21
|
|
|
21
22
|
Raises:
|
|
22
|
-
|
|
23
|
+
InvalidArgumentError: If ``tolerance`` is negative, infinite or nan
|
|
24
|
+
(a ``ValueError``).
|
|
23
25
|
"""
|
|
24
26
|
tolerance = opts.get("tolerance", 0.0)
|
|
25
27
|
try:
|
|
@@ -28,7 +30,7 @@ def _float_eq(x: Any, y: Any, **opts: Any) -> bool:
|
|
|
28
30
|
# An int too large to convert to float: not a finite float tolerance.
|
|
29
31
|
finite = False
|
|
30
32
|
if not (finite and tolerance >= 0):
|
|
31
|
-
raise
|
|
33
|
+
raise InvalidArgumentError(f"tolerance must be a finite number >= 0, got {tolerance!r}")
|
|
32
34
|
# Exact equality first: it is Python's answer for any right-hand value
|
|
33
35
|
# (False for None or a string), and exact for an int too large for float.
|
|
34
36
|
if x == y:
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
from typing import Any
|
|
6
6
|
|
|
7
|
+
from ..errors import InvalidArgumentError
|
|
7
8
|
from ..group import OperatorGroup, float_group, int_group
|
|
8
9
|
from ..operator import Operator
|
|
9
10
|
from ..registry import OperatorRegistry
|
|
@@ -21,7 +22,7 @@ def _between(x: Any, bounds: Any, **opts: Any) -> bool:
|
|
|
21
22
|
try:
|
|
22
23
|
lo, hi = bounds
|
|
23
24
|
except (TypeError, ValueError):
|
|
24
|
-
raise
|
|
25
|
+
raise InvalidArgumentError(
|
|
25
26
|
f"'between' expects a 2-element sequence for bounds, got {bounds!r}"
|
|
26
27
|
) from None
|
|
27
28
|
if opts.get("inclusive", True):
|