Skip to content

Commit cf37d23

Browse files
rustyconoverclaude
andcommitted
Add validation and improve argument_spec module
- Add warning when arg_types mapping is missing an argument - Export VGI_* metadata constants in __all__ and re-export from vgi package - Extract duplicated sort_key function to module-level _argument_spec_sort_key - Add validation that positional argument indices are contiguous (0, 1, 2...) - Add tests for all new validation behaviors 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ef323ef commit cf37d23

3 files changed

Lines changed: 128 additions & 15 deletions

File tree

‎tests/test_argument_spec.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,78 @@ class ComplexFunction(TableInOutFunction):
447447
assert specs[4].is_any_type is True
448448

449449

450+
class TestArgumentSpecToSchemaValidation:
451+
"""Test validation in argument_specs_to_schema."""
452+
453+
def test_non_contiguous_indices_warns(self) -> None:
454+
"""Non-contiguous positional indices should issue a warning."""
455+
specs = [
456+
ArgumentSpec(name="first", position=0, arrow_type=pa.int64()),
457+
ArgumentSpec(name="third", position=2, arrow_type=pa.int64()), # Gap: no 1
458+
]
459+
with pytest.warns(UserWarning, match="not contiguous"):
460+
argument_specs_to_schema(specs)
461+
462+
def test_indices_not_starting_at_zero_warns(self) -> None:
463+
"""Positional indices not starting at 0 should warn."""
464+
specs = [
465+
ArgumentSpec(name="second", position=1, arrow_type=pa.int64()),
466+
ArgumentSpec(name="third", position=2, arrow_type=pa.int64()),
467+
]
468+
with pytest.warns(UserWarning, match="not contiguous"):
469+
argument_specs_to_schema(specs)
470+
471+
def test_contiguous_indices_no_warning(self) -> None:
472+
"""Contiguous positional indices should not warn."""
473+
specs = [
474+
ArgumentSpec(name="first", position=0, arrow_type=pa.int64()),
475+
ArgumentSpec(name="second", position=1, arrow_type=pa.int64()),
476+
ArgumentSpec(name="third", position=2, arrow_type=pa.int64()),
477+
]
478+
# Should not raise any warnings
479+
import warnings as w
480+
481+
with w.catch_warnings(record=True) as caught:
482+
w.simplefilter("always")
483+
argument_specs_to_schema(specs)
484+
# Filter for our specific warning
485+
contiguity_warnings = [
486+
x for x in caught if "not contiguous" in str(x.message)
487+
]
488+
assert len(contiguity_warnings) == 0
489+
490+
491+
class TestExtractArgumentSpecsValidation:
492+
"""Test validation in extract_argument_specs."""
493+
494+
def test_missing_arg_type_warns(self) -> None:
495+
"""Missing arg_types entry should issue a warning."""
496+
497+
class FunctionWithArg(TableInOutFunction):
498+
count = Arg[int](0)
499+
500+
# Provide empty arg_types - should warn
501+
with pytest.warns(UserWarning, match="Missing type for argument 'count'"):
502+
specs = extract_argument_specs(FunctionWithArg, {})
503+
504+
assert len(specs) == 1
505+
assert specs[0].arrow_type == pa.null()
506+
507+
def test_partial_arg_types_warns_for_missing(self) -> None:
508+
"""Only missing args should trigger warnings."""
509+
510+
class FunctionWithTwoArgs(TableInOutFunction):
511+
count = Arg[int](0)
512+
name = Arg[str](1)
513+
514+
# Provide type for only 'count'
515+
with pytest.warns(UserWarning, match="Missing type for argument 'name'"):
516+
specs = extract_argument_specs(FunctionWithTwoArgs, {"count": pa.int64()})
517+
518+
assert specs[0].arrow_type == pa.int64()
519+
assert specs[1].arrow_type == pa.null()
520+
521+
450522
class TestEdgeCases:
451523
"""Test edge cases and boundary conditions."""
452524

‎vgi/__init__.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ class Meta:
135135

136136
# Re-export commonly used classes for convenient imports
137137
from vgi.argument_spec import (
138+
VGI_ARG_KEY,
139+
VGI_ARG_NAMED,
140+
VGI_TYPE_ANY,
141+
VGI_TYPE_KEY,
142+
VGI_TYPE_TABLE,
143+
VGI_VARARGS_KEY,
144+
VGI_VARARGS_TRUE,
138145
ArgumentSpec,
139146
argument_specs_to_schema,
140147
schema_to_argument_specs,
@@ -184,6 +191,14 @@ class Meta:
184191
"Arguments",
185192
"argument_specs_to_schema",
186193
"FilterFunction",
194+
# Metadata constants for parsing argument spec schemas
195+
"VGI_ARG_KEY",
196+
"VGI_ARG_NAMED",
197+
"VGI_TYPE_KEY",
198+
"VGI_TYPE_TABLE",
199+
"VGI_TYPE_ANY",
200+
"VGI_VARARGS_KEY",
201+
"VGI_VARARGS_TRUE",
187202
"FunctionExample",
188203
"FunctionStability",
189204
"TableInOutFunctionTestClient",

‎vgi/argument_spec.py‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
3434
"""
3535

36+
import warnings
3637
from collections.abc import Sequence
3738
from dataclasses import dataclass
3839
from typing import Any, get_type_hints
@@ -46,6 +47,14 @@
4647
"argument_specs_to_schema",
4748
"extract_argument_specs",
4849
"schema_to_argument_specs",
50+
# Metadata constants for parsing schemas
51+
"VGI_ARG_KEY",
52+
"VGI_ARG_NAMED",
53+
"VGI_TYPE_KEY",
54+
"VGI_TYPE_TABLE",
55+
"VGI_TYPE_ANY",
56+
"VGI_VARARGS_KEY",
57+
"VGI_VARARGS_TRUE",
4958
]
5059

5160
# =============================================================================
@@ -66,6 +75,13 @@
6675
VGI_VARARGS_TRUE = b"true"
6776

6877

78+
def _argument_spec_sort_key(spec: "ArgumentSpec") -> tuple[int, int | str]:
79+
"""Sort key: positional first (by index), then named (alphabetically)."""
80+
if isinstance(spec.position, int):
81+
return (0, spec.position)
82+
return (1, spec.position)
83+
84+
6985
# =============================================================================
7086
# ArgumentSpec Dataclass
7187
# =============================================================================
@@ -134,14 +150,21 @@ def argument_specs_to_schema(specs: Sequence[ArgumentSpec]) -> pa.Schema:
134150
# schema has fields: count (int64), format (utf8 with vgi_arg=named)
135151
136152
"""
153+
sorted_specs = sorted(specs, key=_argument_spec_sort_key)
137154

138-
# Sort: positional first (by index), then named (alphabetically)
139-
def sort_key(spec: ArgumentSpec) -> tuple[int, int | str]:
140-
if isinstance(spec.position, int):
141-
return (0, spec.position)
142-
return (1, spec.position)
143-
144-
sorted_specs = sorted(specs, key=sort_key)
155+
# Validate contiguous positional indices
156+
positional_indices = [
157+
spec.position for spec in sorted_specs if isinstance(spec.position, int)
158+
]
159+
if positional_indices:
160+
expected = list(range(len(positional_indices)))
161+
if positional_indices != expected:
162+
warnings.warn(
163+
f"Positional argument indices are not contiguous starting from 0. "
164+
f"Found: {positional_indices}, expected: {expected}. "
165+
f"This may indicate a bug.",
166+
stacklevel=2,
167+
)
145168

146169
fields: list[pa.Field[Any]] = []
147170
for spec in sorted_specs:
@@ -284,7 +307,16 @@ class MyFunction(TableInOutFunction):
284307
arg: Arg[Any] = attr_value
285308

286309
# Get Arrow type from provided mapping
287-
arrow_type = arg_types.get(attr_name, pa.null())
310+
arrow_type: pa.DataType
311+
if attr_name not in arg_types:
312+
warnings.warn(
313+
f"Missing type for argument '{attr_name}' in arg_types "
314+
f"mapping; defaulting to pa.null(). This may indicate a bug.",
315+
stacklevel=2,
316+
)
317+
arrow_type = pa.null()
318+
else:
319+
arrow_type = arg_types[attr_name]
288320

289321
# Check type hint for special types
290322
hint = hints.get(attr_name)
@@ -305,10 +337,4 @@ class MyFunction(TableInOutFunction):
305337
)
306338
)
307339

308-
# Sort: positional first (by index), then named (alphabetically)
309-
def sort_key(spec: ArgumentSpec) -> tuple[int, int | str]:
310-
if isinstance(spec.position, int):
311-
return (0, spec.position)
312-
return (1, spec.position)
313-
314-
return sorted(specs, key=sort_key)
340+
return sorted(specs, key=_argument_spec_sort_key)

0 commit comments

Comments
 (0)