Skip to content

Commit cff7a08

Browse files
rustyconoverclaude
andcommitted
fix: evaluate argument pattern constraints as an unanchored search
Arg/ConstParam/Param `pattern` used re.match (anchored at the start only), diverging from JSON Schema / ECMA `pattern` semantics used by the Rust (is_match) and Go (MatchString) SDKs. Switch both evaluation sites (scalar and varargs elements) to re.search; anchor explicitly with ^ and $. Docs and docstrings now state the semantics; tests cover unanchored search, ^/$ anchoring, named args and varargs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e22cecb commit cff7a08

4 files changed

Lines changed: 90 additions & 9 deletions

File tree

‎docs/argument-serialization.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,9 @@ def parse_schema(schema):
313313
The following are **not** serialized in the schema:
314314

315315
- **Default values** - handled at runtime by the `Arg` descriptor
316-
- **Validation constraints** (`ge`, `le`, `choices`, `pattern`) - Python-side validation
316+
- **Validation constraints** (`ge`, `le`, `choices`, `pattern`) - Python-side validation. `pattern` is an unanchored regex search (JSON Schema
317+
`pattern` semantics, same as every VGI SDK): `'https?://'` accepts `xhttp://a`;
318+
use `^` and `$` to anchor (`'^https?://'`).
317319
- **Documentation strings** - available via `ParameterInfo` in metadata
318320

319321
These are implementation details of the Python function runtime, not part

‎tests/test_protocol_classes.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,60 @@ class MyClass:
651651
with pytest.raises(ArgumentValidationError, match="does not match the required pattern"):
652652
_ = obj.name
653653

654+
def test_pattern_is_unanchored_search(self) -> None:
655+
"""An unanchored pattern matches anywhere in the value (JSON Schema semantics)."""
656+
657+
class MyClass:
658+
invocation = _MockInvocation(Arguments(positional=(pa.scalar("xhttp://a"),)))
659+
url = Arg[str](0, pattern=r"https?://")
660+
661+
assert MyClass().url == "xhttp://a"
662+
663+
def test_pattern_start_anchor_rejects_prefix(self) -> None:
664+
"""A ``^``-anchored pattern still rejects a value with a leading prefix."""
665+
666+
class MyClass:
667+
invocation = _MockInvocation(Arguments(positional=(pa.scalar("xhttp://a"),)))
668+
url = Arg[str](0, pattern=r"^https?://")
669+
670+
with pytest.raises(ArgumentValidationError, match="does not match the required pattern"):
671+
_ = MyClass().url
672+
673+
def test_pattern_start_anchor_accepts_match(self) -> None:
674+
"""A ``^``-anchored pattern accepts a value that starts with the match."""
675+
676+
class MyClass:
677+
invocation = _MockInvocation(Arguments(positional=(pa.scalar("https://a"),)))
678+
url = Arg[str](0, pattern=r"^https?://")
679+
680+
assert MyClass().url == "https://a"
681+
682+
def test_pattern_end_anchor_enforced(self) -> None:
683+
"""A ``$`` anchor is honoured: trailing text after the match is rejected."""
684+
685+
class MyClass:
686+
invocation = _MockInvocation(Arguments(positional=(pa.scalar("ab1"),)))
687+
code = Arg[str](0, pattern=r"^[a-z]+$")
688+
689+
with pytest.raises(ArgumentValidationError, match="does not match the required pattern"):
690+
_ = MyClass().code
691+
692+
def test_pattern_search_on_named_arg(self) -> None:
693+
"""Named arguments use the same unanchored search."""
694+
695+
class MyClass:
696+
invocation = _MockInvocation(Arguments(positional=(), named={"url": pa.scalar("xhttp://a")}))
697+
url = Arg[str]("url", pattern=r"https?://")
698+
699+
assert MyClass().url == "xhttp://a"
700+
701+
class Anchored:
702+
invocation = _MockInvocation(Arguments(positional=(), named={"url": pa.scalar("xhttp://a")}))
703+
url = Arg[str]("url", pattern=r"^https?://")
704+
705+
with pytest.raises(ArgumentValidationError, match="does not match the required pattern"):
706+
_ = Anchored().url
707+
654708
def test_pattern_validation_requires_string(self) -> None:
655709
"""Arg pattern validation should fail for non-string types."""
656710

@@ -1001,6 +1055,22 @@ class MyClass:
10011055
with pytest.raises(ArgumentValidationError, match="element 1.*does not match pattern"):
10021056
_ = obj.values
10031057

1058+
def test_varargs_pattern_is_unanchored_search(self) -> None:
1059+
"""Varargs elements use the same unanchored search as scalar args."""
1060+
1061+
class MyClass:
1062+
invocation = _MockInvocation(Arguments(positional=(pa.scalar("xhttp://a"), pa.scalar("https://b"))))
1063+
values = Arg[str](0, varargs=True, pattern=r"https?://", arrow_type=pa.string())
1064+
1065+
assert MyClass().values == ("xhttp://a", "https://b") # type: ignore[comparison-overlap]
1066+
1067+
class Anchored:
1068+
invocation = _MockInvocation(Arguments(positional=(pa.scalar("https://b"), pa.scalar("xhttp://a"))))
1069+
values = Arg[str](0, varargs=True, pattern=r"^https?://", arrow_type=pa.string())
1070+
1071+
with pytest.raises(ArgumentValidationError, match="element 1.*does not match pattern"):
1072+
_ = Anchored().values
1073+
10041074
def test_varargs_must_be_positional(self) -> None:
10051075
"""Varargs with named argument should raise ValueError at definition."""
10061076
with pytest.raises(ValueError, match="varargs=True requires a positional argument"):

‎vgi/argument_spec.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@
8585
# vgi_choices — JSON array (the closed set of allowed values)
8686
# vgi_range — interval notation built from ge/le/gt/lt (e.g. "[0, 100]",
8787
# "(0, +inf)", "[1, 10)"); a discovery surface, not the raw bounds
88-
# vgi_pattern — raw regex the value must match (open set)
88+
# vgi_pattern — raw regex the value must match (open set); evaluated as an
89+
# unanchored search (JSON Schema `pattern`), anchor with ^ / $
8990
# Kept byte-for-byte in sync with the C++ reader in the vgi extension
9091
# (src/vgi_function_arguments_function.cpp / src/include/vgi_protocol_constants.hpp).
9192
VGI_DEFAULT_KEY = b"vgi_default"
@@ -144,8 +145,9 @@ class ArgumentSpec:
144145
range_notation: Optional interval-notation string built from the arg's
145146
numeric bounds (``vgi_range``, e.g. ``"[0, 100]"``); None when
146147
unbounded.
147-
pattern: Optional regex the value must match (``vgi_pattern``); None
148-
when there is no pattern constraint.
148+
pattern: Optional regex the value must match (``vgi_pattern``),
149+
evaluated as an unanchored search; None when there is no pattern
150+
constraint.
149151
150152
Note:
151153
For named arguments, the Python attribute name (``name``) and the SQL

‎vgi/arguments.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,9 @@ class Arg[ArgT]:
954954
gt: Value must be > this (for numeric types).
955955
lt: Value must be < this (for numeric types).
956956
choices: Value must be one of these options.
957-
pattern: Value must match this regex pattern (for strings).
957+
pattern: Regex the value must contain (for strings). Unanchored search
958+
(JSON Schema ``pattern`` semantics): ``'https?://'`` matches
959+
anywhere in the value; use ``^`` and ``$`` to anchor.
958960
959961
Note:
960962
For named arguments (string position), the Python attribute name should
@@ -1038,7 +1040,9 @@ def __init__(
10381040
gt: Minimum value (exclusive). Value must be > this.
10391041
lt: Maximum value (exclusive). Value must be < this.
10401042
choices: Allowed values. Value must be one of these.
1041-
pattern: Regex pattern for string validation.
1043+
pattern: Regex for string validation. Evaluated as an unanchored
1044+
search (``re.search``, JSON Schema ``pattern`` semantics); use
1045+
``^`` and ``$`` to anchor the whole value.
10421046
varargs: If True, collect all remaining positional arguments from this
10431047
position onwards. Returns tuple[ArgT, ...]. Requires at least 1 value.
10441048
Must be positional (not named).
@@ -1350,7 +1354,7 @@ def _validate(self, value: ArgT) -> None:
13501354
valid_range=valid_range,
13511355
default=self.default,
13521356
)
1353-
if not self._compiled_pattern.match(value):
1357+
if not self._compiled_pattern.search(value):
13541358
raise ArgumentValidationError(
13551359
f"Argument '{arg_name}' does not match the required pattern.",
13561360
arg_name=self._name,
@@ -1447,7 +1451,7 @@ def _validate_single(self, value: Any, *, index: int) -> None:
14471451
doc=self.doc if self.doc else None,
14481452
valid_range=valid_range,
14491453
)
1450-
if not self._compiled_pattern.match(value):
1454+
if not self._compiled_pattern.search(value):
14511455
raise ArgumentValidationError(
14521456
f"Argument '{arg_name}' element {index} does not match pattern.",
14531457
arg_name=self._name,
@@ -1634,7 +1638,8 @@ class Param:
16341638
le: Value must be <= this (inclusive upper bound). Advisory on `Param`.
16351639
gt: Value must be > this (exclusive lower bound). Advisory on `Param`.
16361640
lt: Value must be < this (exclusive upper bound).
1637-
pattern: Regex the value must match (for string parameters).
1641+
pattern: Regex the value must match (for string parameters). Unanchored
1642+
search; use ``^`` and ``$`` to anchor.
16381643
16391644
Example (ScalarFunction compute() - position inferred):
16401645
class AddColumns(ScalarFunction):
@@ -1698,6 +1703,8 @@ class ConstParam:
16981703
gt: Value must be > this (exclusive lower bound); enforced at bind.
16991704
lt: Value must be < this (exclusive upper bound); enforced at bind.
17001705
pattern: Regex the value must match (string params); enforced at bind.
1706+
Unanchored search (JSON Schema ``pattern``); use ``^`` and ``$`` to
1707+
anchor.
17011708
17021709
"""
17031710

0 commit comments

Comments
 (0)