vcti-predicate 2.1.0__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.1.0/src/vcti_predicate.egg-info → vcti_predicate-2.2.0}/PKG-INFO +26 -14
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/README.md +25 -13
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/pyproject.toml +1 -1
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/__init__.py +4 -0
- vcti_predicate-2.2.0/src/vcti/predicate/errors.py +134 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/evaluator.py +102 -23
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/registry.py +38 -1
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0/src/vcti_predicate.egg-info}/PKG-INFO +26 -14
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti_predicate.egg-info/SOURCES.txt +1 -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.1.0/src/vcti/predicate/errors.py +0 -79
- vcti_predicate-2.1.0/tests/test_errors.py +0 -201
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/LICENSE +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/setup.cfg +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/group.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/operator.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/__init__.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/collection.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/comparison.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/range.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/regex.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/string.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/operators/unary.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/py.typed +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti/predicate/symbol.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti_predicate.egg-info/dependency_links.txt +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti_predicate.egg-info/requires.txt +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/src/vcti_predicate.egg-info/top_level.txt +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_docs.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_evaluator.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_examples.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_group.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_negation_pairs.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_operator.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_operators_collection.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_operators_comparison.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_operators_range.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_operators_regex.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_operators_string.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_operators_unary.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_registry.py +0 -0
- {vcti_predicate-2.1.0 → vcti_predicate-2.2.0}/tests/test_symbol.py +0 -0
- {vcti_predicate-2.1.0 → 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.
|
|
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.
|
|
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.
|
|
59
|
+
"vcti-predicate>=2.2.0",
|
|
60
60
|
]
|
|
61
61
|
```
|
|
62
62
|
|
|
@@ -199,17 +199,29 @@ no modifiers.
|
|
|
199
199
|
|
|
200
200
|
## Errors
|
|
201
201
|
|
|
202
|
-
`check()` and `evaluate()` raise `
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
`
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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`.
|
|
213
225
|
|
|
214
226
|
---
|
|
215
227
|
|
|
@@ -25,14 +25,14 @@ pip install vcti-predicate
|
|
|
25
25
|
### In `requirements.txt`
|
|
26
26
|
|
|
27
27
|
```
|
|
28
|
-
vcti-predicate>=2.
|
|
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.
|
|
35
|
+
"vcti-predicate>=2.2.0",
|
|
36
36
|
]
|
|
37
37
|
```
|
|
38
38
|
|
|
@@ -175,17 +175,29 @@ no modifiers.
|
|
|
175
175
|
|
|
176
176
|
## Errors
|
|
177
177
|
|
|
178
|
-
`check()` and `evaluate()` raise `
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
`
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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`.
|
|
189
201
|
|
|
190
202
|
---
|
|
191
203
|
|
|
@@ -16,7 +16,9 @@ from importlib.metadata import version
|
|
|
16
16
|
from .errors import (
|
|
17
17
|
ArgumentTypeError,
|
|
18
18
|
ArityError,
|
|
19
|
+
InapplicableArgumentError,
|
|
19
20
|
InvalidArgumentError,
|
|
21
|
+
NotApplicableError,
|
|
20
22
|
PredicateError,
|
|
21
23
|
RegistrationError,
|
|
22
24
|
UnknownSymbolError,
|
|
@@ -40,7 +42,9 @@ __all__ = [
|
|
|
40
42
|
"OperatorRegistry",
|
|
41
43
|
"Symbol",
|
|
42
44
|
"PredicateError",
|
|
45
|
+
"NotApplicableError",
|
|
43
46
|
"UnsupportedOperandError",
|
|
47
|
+
"InapplicableArgumentError",
|
|
44
48
|
"UnknownSymbolError",
|
|
45
49
|
"ArityError",
|
|
46
50
|
"InvalidArgumentError",
|
|
@@ -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
|
+
"""
|
|
@@ -4,8 +4,13 @@
|
|
|
4
4
|
|
|
5
5
|
from typing import Any
|
|
6
6
|
|
|
7
|
-
from .errors import
|
|
8
|
-
|
|
7
|
+
from .errors import (
|
|
8
|
+
ArgumentTypeError,
|
|
9
|
+
ArityError,
|
|
10
|
+
InapplicableArgumentError,
|
|
11
|
+
InvalidArgumentError,
|
|
12
|
+
)
|
|
13
|
+
from .group import OperatorGroup, check_value_type
|
|
9
14
|
from .operator import Operator
|
|
10
15
|
from .operators import create_default_registry
|
|
11
16
|
from .registry import OperatorRegistry
|
|
@@ -49,19 +54,26 @@ def check(
|
|
|
49
54
|
Raises:
|
|
50
55
|
UnsupportedOperandError: If the symbol has no operator for the
|
|
51
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.
|
|
52
63
|
UnknownSymbolError: If the symbol, or ``operator``, is unknown.
|
|
53
64
|
ArityError: If the number of operands does not match the
|
|
54
65
|
resolved operator.
|
|
55
|
-
InvalidArgumentError: If a keyword argument is
|
|
56
|
-
modifier value is invalid (a
|
|
57
|
-
that does not compile).
|
|
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).
|
|
58
69
|
ArgumentTypeError: If a modifier or option has the wrong type
|
|
59
70
|
(e.g. ``case_sensitive="false"``).
|
|
60
71
|
TypeError: From the operator itself when it cannot compare the
|
|
61
72
|
operands, as the Python operation would (``None < 1``).
|
|
62
73
|
|
|
63
|
-
|
|
64
|
-
|
|
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
|
|
65
77
|
:mod:`vcti.predicate.errors`.
|
|
66
78
|
|
|
67
79
|
Examples:
|
|
@@ -112,19 +124,26 @@ def evaluate(
|
|
|
112
124
|
Raises:
|
|
113
125
|
UnsupportedOperandError: If the symbol has no operator for the
|
|
114
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.
|
|
115
133
|
UnknownSymbolError: If the symbol, or ``operator``, is unknown.
|
|
116
134
|
ArityError: If the number of operands does not match the
|
|
117
135
|
resolved operator.
|
|
118
|
-
InvalidArgumentError: If a keyword argument is
|
|
119
|
-
modifier value is invalid (a
|
|
120
|
-
that does not compile).
|
|
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).
|
|
121
139
|
ArgumentTypeError: If a modifier or option has the wrong type
|
|
122
140
|
(e.g. ``case_sensitive="false"``).
|
|
123
141
|
TypeError: From the operator itself when it cannot compare the
|
|
124
142
|
operands, as the Python operation would (``None < 1``).
|
|
125
143
|
|
|
126
|
-
|
|
127
|
-
|
|
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
|
|
128
147
|
:mod:`vcti.predicate.errors`.
|
|
129
148
|
"""
|
|
130
149
|
if not args:
|
|
@@ -148,7 +167,10 @@ def evaluate(
|
|
|
148
167
|
# ── Route kwargs ──
|
|
149
168
|
group_modifiers, op_options, unknown = _route_kwargs(kwargs, group, resolved)
|
|
150
169
|
if unknown:
|
|
151
|
-
|
|
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)
|
|
152
174
|
for key, value in group_modifiers.items():
|
|
153
175
|
group.check_modifier(key, value)
|
|
154
176
|
for key, value in op_options.items():
|
|
@@ -193,16 +215,73 @@ def _raise_unknown_kwargs(
|
|
|
193
215
|
op: Operator,
|
|
194
216
|
unknown: set[str],
|
|
195
217
|
group: OperatorGroup,
|
|
218
|
+
kwargs: dict[str, Any],
|
|
219
|
+
args: tuple[Any, ...],
|
|
220
|
+
schemas: list[tuple[int, dict[str, type]]],
|
|
196
221
|
) -> None:
|
|
197
|
-
"""Raise
|
|
198
|
-
|
|
199
|
-
if
|
|
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
|
+
)
|
|
200
256
|
raise InvalidArgumentError(
|
|
201
|
-
f"
|
|
202
|
-
f"
|
|
203
|
-
|
|
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"
|
|
204
285
|
)
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
f"group) which does not accept any keyword arguments."
|
|
208
|
-
)
|
|
286
|
+
assert first_error is not None
|
|
287
|
+
raise first_error
|
|
@@ -6,7 +6,7 @@ from collections.abc import Callable, Mapping
|
|
|
6
6
|
from dataclasses import replace as replace_field
|
|
7
7
|
from typing import Any
|
|
8
8
|
|
|
9
|
-
from .errors import RegistrationError, UnknownSymbolError
|
|
9
|
+
from .errors import RegistrationError, UnknownSymbolError, UnsupportedOperandError
|
|
10
10
|
from .group import OperatorGroup
|
|
11
11
|
from .operator import Operator
|
|
12
12
|
from .symbol import Symbol
|
|
@@ -218,6 +218,43 @@ class OperatorRegistry:
|
|
|
218
218
|
f"mean one operator throughout the registry: {uses}"
|
|
219
219
|
)
|
|
220
220
|
|
|
221
|
+
def _argument_schemas(self, symbol_name: str) -> list[tuple[int, dict[str, type]]]:
|
|
222
|
+
"""What each selectable operator of the symbol accepts: arity and keywords.
|
|
223
|
+
|
|
224
|
+
One ``(arity, schema)`` pair per operator that resolution can select
|
|
225
|
+
for some type, where the schema maps a keyword to its declared type
|
|
226
|
+
(the operator's group modifiers and its own options). Kept per
|
|
227
|
+
operator, not merged, because a condition is only valid if one
|
|
228
|
+
operator accepts it whole. An operator no type can reach is left
|
|
229
|
+
out: it cannot make a condition apply to anything.
|
|
230
|
+
|
|
231
|
+
Selectability is decided with witness types, which is exact for
|
|
232
|
+
:meth:`Symbol.resolve`:
|
|
233
|
+
|
|
234
|
+
- A group operator is selectable if and only if one of its group's
|
|
235
|
+
own ``types`` resolves to it. If an earlier group takes a listed
|
|
236
|
+
type, it also takes every subclass of that type, so no unlisted
|
|
237
|
+
type can reach the operator either.
|
|
238
|
+
- The default is selectable if and only if ``object`` resolves to
|
|
239
|
+
it. A group covering ``object`` takes every type.
|
|
240
|
+
|
|
241
|
+
Empty for an unknown symbol.
|
|
242
|
+
"""
|
|
243
|
+
sym = self._symbols.get(symbol_name)
|
|
244
|
+
if sym is None:
|
|
245
|
+
return []
|
|
246
|
+
witnesses = [t for group in sym.operators for t in group.types]
|
|
247
|
+
if sym.default is not None:
|
|
248
|
+
witnesses.append(object)
|
|
249
|
+
selectable: dict[int, Operator] = {}
|
|
250
|
+
for witness in witnesses:
|
|
251
|
+
try:
|
|
252
|
+
op = sym.resolve(witness)
|
|
253
|
+
except UnsupportedOperandError:
|
|
254
|
+
continue
|
|
255
|
+
selectable[id(op)] = op
|
|
256
|
+
return [(op.arity, {**op.group.modifiers, **op.options}) for op in selectable.values()]
|
|
257
|
+
|
|
221
258
|
def _usages(self, op: Operator) -> list[tuple[Symbol, _Slot]]:
|
|
222
259
|
"""Every symbol slot that dispatches to ``op``."""
|
|
223
260
|
found: list[tuple[Symbol, _Slot]] = []
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: vcti-predicate
|
|
3
|
-
Version: 2.
|
|
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.
|
|
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.
|
|
59
|
+
"vcti-predicate>=2.2.0",
|
|
60
60
|
]
|
|
61
61
|
```
|
|
62
62
|
|
|
@@ -199,17 +199,29 @@ no modifiers.
|
|
|
199
199
|
|
|
200
200
|
## Errors
|
|
201
201
|
|
|
202
|
-
`check()` and `evaluate()` raise `
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
`
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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`.
|
|
213
225
|
|
|
214
226
|
---
|
|
215
227
|
|
|
@@ -21,6 +21,7 @@ src/vcti_predicate.egg-info/SOURCES.txt
|
|
|
21
21
|
src/vcti_predicate.egg-info/dependency_links.txt
|
|
22
22
|
src/vcti_predicate.egg-info/requires.txt
|
|
23
23
|
src/vcti_predicate.egg-info/top_level.txt
|
|
24
|
+
tests/test_applicability.py
|
|
24
25
|
tests/test_docs.py
|
|
25
26
|
tests/test_errors.py
|
|
26
27
|
tests/test_evaluator.py
|