Skip to content

Commit 7be6a93

Browse files
Drop custom matcher & config
We implemented this so that we could avoid a hard dependency on google/re2 due to the lack of wheels being published for Python 3.13. However, [3.13 wheels are now published][1], and our downstream cel-python dep, as of v0.4, is requiring google/re2 as a dependency. It seems like we ought to remove our matches workaround, as well as the configuration for supplying a user-defined matching func, as we generally want all users to have the same behavior for matching. (We can keep in the config value if we really think it's necessary.) [1]: google/re2#516
1 parent 4eee867 commit 7be6a93

File tree

8 files changed

+4
-182
lines changed

8 files changed

+4
-182
lines changed

protovalidate/config.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from collections.abc import Callable
1615
from dataclasses import dataclass
17-
from typing import Optional
1816

1917

2018
@dataclass
@@ -23,10 +21,6 @@ class Config:
2321
2422
Attributes:
2523
fail_fast (bool): If true, validation will stop after the first violation. Defaults to False.
26-
regex_matches_func: An optional regex matcher to use. If specified, this will be used to match
27-
on regex expressions instead of this library's `matches` logic.
2824
"""
2925

3026
fail_fast: bool = False
31-
32-
regex_matches_func: Optional[Callable[[str, str], bool]] = None

protovalidate/internal/extra_func.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@
1515
import math
1616
import re
1717
import typing
18-
from collections.abc import Callable
1918
from urllib import parse as urlparse
2019

2120
import celpy
2221
from celpy import celtypes
2322

24-
from protovalidate.config import Config
2523
from protovalidate.internal import string_format
26-
from protovalidate.internal.matches import matches as protovalidate_matches
2724
from protovalidate.internal.rules import MessageType, field_to_cel
2825

2926
# See https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
@@ -1556,31 +1553,11 @@ def __peek(self, char: str) -> bool:
15561553
return self._index < len(self._string) and self._string[self._index] == char
15571554

15581555

1559-
def get_matches_func(matcher: typing.Optional[Callable[[str, str], bool]]):
1560-
if matcher is None:
1561-
matcher = protovalidate_matches
1562-
1563-
def cel_matches(text: celtypes.Value, pattern: celtypes.Value) -> celpy.Result:
1564-
if not isinstance(text, celtypes.StringType):
1565-
msg = "invalid argument for text, expected string"
1566-
raise celpy.CELEvalError(msg)
1567-
if not isinstance(pattern, celtypes.StringType):
1568-
msg = "invalid argument for pattern, expected string"
1569-
raise celpy.CELEvalError(msg)
1570-
1571-
b = matcher(text, pattern)
1572-
return celtypes.BoolType(b)
1573-
1574-
return cel_matches
1575-
1576-
1577-
def make_extra_funcs(config: Config) -> dict[str, celpy.CELFunction]:
1556+
def make_extra_funcs() -> dict[str, celpy.CELFunction]:
15781557
string_fmt = string_format.StringFormat()
15791558
return {
15801559
# Missing standard functions
15811560
"format": string_fmt.format,
1582-
# Overridden standard functions
1583-
"matches": get_matches_func(config.regex_matches_func),
15841561
# protovalidate specific functions
15851562
"getField": cel_get_field,
15861563
"isNan": cel_is_nan,

protovalidate/internal/matches.py

Lines changed: 0 additions & 66 deletions
This file was deleted.

protovalidate/validator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Validator:
4040

4141
def __init__(self, config=None):
4242
self._cfg = config if config is not None else Config()
43-
funcs = extra_func.make_extra_funcs(self._cfg)
43+
funcs = extra_func.make_extra_funcs()
4444
self._factory = _rules.RuleFactory(funcs)
4545

4646
def validate(

test/test_config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ class TestConfig(unittest.TestCase):
2121
def test_defaults(self):
2222
cfg = Config()
2323
self.assertFalse(cfg.fail_fast)
24-
self.assertIsNone(cfg.regex_matches_func)

test/test_format.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
from gen.cel.expr import eval_pb2
2525
from gen.cel.expr.conformance.test import simple_pb2
26-
from protovalidate.config import Config
2726
from protovalidate.internal import extra_func
2827
from protovalidate.internal.cel_field_presence import InterpretedRunner
2928

@@ -109,7 +108,7 @@ def test_format_successes(self):
109108
if test.name in skipped_tests:
110109
continue
111110
ast = self._env.compile(test.expr)
112-
prog = self._env.program(ast, functions=extra_func.make_extra_funcs(Config()))
111+
prog = self._env.program(ast, functions=extra_func.make_extra_funcs())
113112

114113
bindings = build_variables(test.bindings)
115114
# Ideally we should use pytest parametrize instead of subtests, but
@@ -133,7 +132,7 @@ def test_format_errors(self):
133132
if test.name in skipped_error_tests:
134133
continue
135134
ast = self._env.compile(test.expr)
136-
prog = self._env.program(ast, functions=extra_func.make_extra_funcs(Config()))
135+
prog = self._env.program(ast, functions=extra_func.make_extra_funcs())
137136

138137
bindings = build_variables(test.bindings)
139138
# Ideally we should use pytest parametrize instead of subtests, but

test/test_matches.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

test/test_validate.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import re
1615
import unittest
1716

18-
import celpy
1917
from google.protobuf import message
2018

2119
import protovalidate
@@ -211,42 +209,6 @@ def test_fail_fast(self):
211209
violations = validator.collect_violations(msg)
212210
self._compare_violations(violations, [expected_violation])
213211

214-
def test_custom_matcher(self):
215-
r"""Tests usage of the custom regex_matches_func in the config
216-
217-
A bit of a contrived example, but this exercises the code path
218-
for specifying a custom regex matches function when writing regex rules.
219-
220-
Usage of the pattern \z is not supported in Python's re engine, only \Z is supported.
221-
However, the inverse is true with re2 (\Z is _not_ supported and \z is supported).
222-
223-
This test shows using a custom matcher that converts any re2-compliant usage of \z
224-
to \Z so that Python's re engine can execute it.
225-
"""
226-
msg = validations_pb2.InvalidRESyntax()
227-
228-
def matcher(text: str, pattern: str) -> bool:
229-
pattern = pattern.replace("z", "Z")
230-
try:
231-
m = re.search(pattern, text)
232-
except re.error as ex:
233-
msg = "match error"
234-
raise celpy.CELEvalError(msg, ex.__class__, ex.args) from ex
235-
return m is not None
236-
237-
cfg = Config(regex_matches_func=matcher)
238-
validator = protovalidate.Validator(config=cfg)
239-
240-
# Test validate
241-
try:
242-
validator.validate(msg)
243-
except Exception:
244-
self.fail("unexpected validation failure")
245-
246-
# Test collect_violations
247-
violations = validator.collect_violations(msg)
248-
self.assertEqual(len(violations), 0)
249-
250212
def _run_valid_tests(self, msg: message.Message):
251213
"""A helper function for testing successful validation on a given message
252214

0 commit comments

Comments
 (0)