Skip to content

Commit 3a6657b

Browse files
rustyconoverclaude
andcommitted
feat: Add AnyArrow sentinel type for arguments accepting any Arrow type
Add AnyArrow as a type parameter for Arg when an argument should accept any valid Arrow scalar type without enforcing a specific type. Unlike TableInput, AnyArrow arguments have actual values that are converted to Python via as_py(). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent e2abf23 commit 3a6657b

5 files changed

Lines changed: 181 additions & 2 deletions

File tree

‎tests/test_metadata.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import structlog
77

88
from vgi import (
9+
AnyArrow,
910
Arg,
1011
FunctionExample,
1112
FunctionType,
@@ -426,6 +427,44 @@ class Meta:
426427
assert data_param.is_table_input is True
427428

428429

430+
class TestAnyArrowMetadata:
431+
"""Tests for AnyArrow parameter metadata extraction."""
432+
433+
def test_any_arrow_type_extracted(self) -> None:
434+
"""AnyArrow type should be extracted as 'AnyArrow' in metadata."""
435+
436+
class AnyArrowFunction(TableInOutFunction):
437+
value: AnyArrow = Arg[AnyArrow](0, doc="Any type value") # type: ignore[assignment]
438+
data: TableInput = Arg[TableInput](1, doc="Input table") # type: ignore[assignment]
439+
440+
params = extract_parameters(AnyArrowFunction)
441+
value_param = next(p for p in params if p.name == "value")
442+
443+
assert value_param.type_name == "AnyArrow"
444+
assert value_param.is_table_input is False
445+
446+
def test_any_arrow_arrow_roundtrip(self) -> None:
447+
"""AnyArrow type survives Arrow serialization."""
448+
449+
class AnyArrowFunction(TableInOutFunction):
450+
"""Test AnyArrow serialization."""
451+
452+
class Meta:
453+
name = "any_arrow_test"
454+
455+
value: AnyArrow = Arg[AnyArrow](0, doc="Any type") # type: ignore[assignment]
456+
data: TableInput = Arg[TableInput](1, doc="Input") # type: ignore[assignment]
457+
458+
batch = functions_to_arrow([AnyArrowFunction])
459+
restored = arrow_to_functions(batch)
460+
461+
assert len(restored) == 1
462+
meta = restored[0]
463+
464+
value_param = next(p for p in meta.parameters if p.name == "value")
465+
assert value_param.type_name == "AnyArrow"
466+
467+
429468
class TestFunctionTypeInference:
430469
"""Tests for function type inference from class hierarchy."""
431470

‎tests/test_protocol_classes.py‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,3 +1387,109 @@ def test_get_varargs_negative_start_raises(self) -> None:
13871387
args = Arguments(positional=(pa.scalar(1),))
13881388
with pytest.raises(ValueError, match="start must be non-negative"):
13891389
args.get_varargs(-1)
1390+
1391+
1392+
class TestAnyArrow:
1393+
"""Tests for AnyArrow sentinel type."""
1394+
1395+
def test_any_arrow_accepts_int(self) -> None:
1396+
"""AnyArrow should accept integer values."""
1397+
from vgi.arguments import AnyArrow
1398+
1399+
class MyClass:
1400+
invocation = _MockInvocation(Arguments(positional=(pa.scalar(42),)))
1401+
value: AnyArrow = Arg[AnyArrow](0) # type: ignore[assignment]
1402+
1403+
obj = MyClass()
1404+
assert obj.value == 42
1405+
1406+
def test_any_arrow_accepts_string(self) -> None:
1407+
"""AnyArrow should accept string values."""
1408+
from vgi.arguments import AnyArrow
1409+
1410+
class MyClass:
1411+
invocation = _MockInvocation(Arguments(positional=(pa.scalar("hello"),)))
1412+
value: AnyArrow = Arg[AnyArrow](0) # type: ignore[assignment]
1413+
1414+
obj = MyClass()
1415+
assert obj.value == "hello"
1416+
1417+
def test_any_arrow_accepts_float(self) -> None:
1418+
"""AnyArrow should accept float values."""
1419+
from vgi.arguments import AnyArrow
1420+
1421+
class MyClass:
1422+
invocation = _MockInvocation(Arguments(positional=(pa.scalar(3.14),)))
1423+
value: AnyArrow = Arg[AnyArrow](0) # type: ignore[assignment]
1424+
1425+
obj = MyClass()
1426+
assert obj.value == 3.14
1427+
1428+
def test_any_arrow_accepts_bool(self) -> None:
1429+
"""AnyArrow should accept boolean values."""
1430+
from vgi.arguments import AnyArrow
1431+
1432+
class MyClass:
1433+
invocation = _MockInvocation(Arguments(positional=(pa.scalar(True),)))
1434+
value: AnyArrow = Arg[AnyArrow](0) # type: ignore[assignment]
1435+
1436+
obj = MyClass()
1437+
assert obj.value is True
1438+
1439+
def test_any_arrow_accepts_list(self) -> None:
1440+
"""AnyArrow should accept list values."""
1441+
from vgi.arguments import AnyArrow
1442+
1443+
class MyClass:
1444+
invocation = _MockInvocation(Arguments(positional=(pa.scalar([1, 2, 3]),)))
1445+
value: AnyArrow = Arg[AnyArrow](0) # type: ignore[assignment]
1446+
1447+
obj = MyClass()
1448+
assert obj.value == [1, 2, 3]
1449+
1450+
def test_any_arrow_mixed_types(self) -> None:
1451+
"""Multiple AnyArrow args can have different types."""
1452+
from vgi.arguments import AnyArrow
1453+
1454+
class MyClass:
1455+
invocation = _MockInvocation(
1456+
Arguments(
1457+
positional=(
1458+
pa.scalar(42),
1459+
pa.scalar("text"),
1460+
pa.scalar(True),
1461+
)
1462+
)
1463+
)
1464+
int_val: AnyArrow = Arg[AnyArrow](0) # type: ignore[assignment]
1465+
str_val: AnyArrow = Arg[AnyArrow](1) # type: ignore[assignment]
1466+
bool_val: AnyArrow = Arg[AnyArrow](2) # type: ignore[assignment]
1467+
1468+
obj = MyClass()
1469+
assert obj.int_val == 42
1470+
assert obj.str_val == "text"
1471+
assert obj.bool_val is True
1472+
1473+
def test_any_arrow_with_default(self) -> None:
1474+
"""AnyArrow should support default values."""
1475+
from vgi.arguments import AnyArrow
1476+
1477+
class MyClass:
1478+
invocation = _MockInvocation(Arguments(positional=()))
1479+
value: AnyArrow = Arg[AnyArrow](0, default="default") # type: ignore[assignment]
1480+
1481+
obj = MyClass()
1482+
assert obj.value == "default"
1483+
1484+
def test_any_arrow_named_argument(self) -> None:
1485+
"""AnyArrow should work with named arguments."""
1486+
from vgi.arguments import AnyArrow
1487+
1488+
class MyClass:
1489+
invocation = _MockInvocation(
1490+
Arguments(named={"data": pa.scalar({"key": "value"})})
1491+
)
1492+
data: AnyArrow = Arg[AnyArrow]("data") # type: ignore[assignment]
1493+
1494+
obj = MyClass()
1495+
assert obj.data == {"key": "value"}

‎vgi/__init__.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class Meta:
134134
"""
135135

136136
# Re-export commonly used classes for convenient imports
137-
from vgi.arguments import Arg, Arguments, ArgumentValidationError, TableInput
137+
from vgi.arguments import AnyArrow, Arg, Arguments, ArgumentValidationError, TableInput
138138
from vgi.invocation import Invocation
139139
from vgi.log import Level, Message
140140
from vgi.metadata import (
@@ -172,6 +172,7 @@ class Meta:
172172

173173
__all__ = [
174174
"AggregationFunction",
175+
"AnyArrow",
175176
"Arg",
176177
"ArgumentValidationError",
177178
"Arguments",

‎vgi/arguments.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __bool__(self) -> bool:
4949
_MISSING: Final = _MissingType()
5050

5151
__all__ = [
52+
"AnyArrow",
5253
"Arg",
5354
"ArgumentValidationError",
5455
"Arguments",
@@ -81,6 +82,34 @@ class MyFunction(TableInOutFunction):
8182
pass
8283

8384

85+
class AnyArrow:
86+
"""Sentinel type for arguments accepting any Arrow type.
87+
88+
Use this as the type parameter for Arg when an argument should accept
89+
any valid Arrow scalar type without enforcing a specific type. The actual
90+
value is still converted to Python via `as_py()`.
91+
92+
This is useful for functions that can operate on any data type, or when
93+
the type is determined at runtime based on the input schema.
94+
95+
Example:
96+
class FlexibleFunction(TableInOutFunction):
97+
# Accept any Arrow type for these parameters
98+
default_value = Arg[AnyArrow](0, doc="Default value (any type)")
99+
threshold = Arg[AnyArrow](1, doc="Threshold for comparison")
100+
101+
# SQL: SELECT * FROM flexible_function(42, 'text')
102+
# SQL: SELECT * FROM flexible_function(3.14, true)
103+
104+
Note:
105+
Unlike TableInput, AnyArrow arguments have actual Arrow values -
106+
they are just not constrained to a specific Arrow type.
107+
108+
"""
109+
110+
pass
111+
112+
84113
@dataclass(frozen=True, slots=True)
85114
class Arguments:
86115
"""Container for function arguments.

‎vgi/metadata.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def transform(self, batch):
5757

5858
import pyarrow as pa
5959

60-
from vgi.arguments import _MISSING, TableInput
60+
from vgi.arguments import _MISSING, AnyArrow, TableInput
6161

6262
if TYPE_CHECKING:
6363
from vgi.arguments import Arg
@@ -408,6 +408,10 @@ def _get_arg_type_info(cls: type, attr_name: str) -> tuple[str | None, bool]:
408408
if hint is TableInput:
409409
return ("TableInput", True)
410410

411+
# Check if it's AnyArrow (any Arrow type accepted)
412+
if hint is AnyArrow:
413+
return ("AnyArrow", False)
414+
411415
# Extract type name
412416
if hasattr(hint, "__name__"):
413417
return (hint.__name__, False)

0 commit comments

Comments
 (0)