Skip to content

Commit d470e73

Browse files
rustyconoverclaude
andcommitted
arguments: reject default values on positional Arg
DuckDB's binder always requires positional arguments, so a default on Arg(<int>, default=...) could never fire through the SQL path — it was a misleading no-op. Raise ValueError at construction time in both the Arg(...) and Arg[T](...) entry points, pointing users at the named-arg form (Arg("name", default=...)) which DuckDB does honor. Drops the now-illegal defaults from five fixtures (versioned_data_scan, versioned_constraints_scan, filter_echo, order_echo, sample_echo); every call site already passes the argument explicitly, so the defaults were dead. Converts the corresponding positional+default unit tests in test_protocol_classes.py to the named form (which exercises the same "default fires when omitted" semantics) and adds a regression test covering both rejection paths. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 08aeaf4 commit d470e73

6 files changed

Lines changed: 79 additions & 29 deletions

File tree

‎tests/test_argument_spec.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ def test_annotated_pattern_basic(self) -> None:
533533

534534
class FunctionWithAnnotated(TableInOutFunction): # type: ignore[type-arg]
535535
count: Annotated[int, Arg(0, doc="Count")]
536-
name: Annotated[str, Arg(1, default="default")]
536+
name: Annotated[str, Arg("name", default="default")]
537537

538538
specs = extract_argument_specs(FunctionWithAnnotated)
539539

‎tests/test_protocol_classes.py‎

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,12 @@ class MyClass:
328328
obj = MyClass()
329329
assert obj.value == 42
330330

331-
def test_positional_with_default(self) -> None:
332-
"""Arg should use default when positional argument is missing."""
331+
def test_named_with_default_missing(self) -> None:
332+
"""Arg should use default when a named argument is missing."""
333333

334334
class MyClass:
335-
invocation = _MockInvocation(Arguments(positional=()))
336-
value = Arg[int](0, default=99)
335+
invocation = _MockInvocation(Arguments(named={}))
336+
value = Arg[int]("value", default=99)
337337

338338
obj = MyClass()
339339
assert obj.value == 99
@@ -430,12 +430,30 @@ def test_repr(self) -> None:
430430
arg1 = Arg[int](0)
431431
assert repr(arg1) == "Arg(0)"
432432

433-
arg2 = Arg[int](1, default=10)
434-
assert repr(arg2) == "Arg(1, default=10)"
433+
arg2 = Arg[int]("count", default=10)
434+
assert repr(arg2) == "Arg('count', default=10)"
435435

436436
arg3 = Arg[str]("name", default="test", doc="A name")
437437
assert repr(arg3) == "Arg('name', default='test', doc='A name')"
438438

439+
def test_positional_with_default_rejected(self) -> None:
440+
"""Positional Arg cannot carry a default.
441+
442+
DuckDB's binder always requires the positional value, so a default
443+
would never fire. Both Arg(...) and Arg[T](...) must reject it.
444+
"""
445+
# Both construction paths (Arg(...) and Arg[T](...)) must reject it.
446+
with pytest.raises(ValueError, match="positional arguments cannot have a default"):
447+
Arg(0, default=10)
448+
with pytest.raises(ValueError, match="positional arguments cannot have a default"):
449+
Arg[int](0, default=10)
450+
# Named with default still works.
451+
Arg("count", default=10)
452+
Arg[int]("count", default=10)
453+
# Positional without default still works.
454+
Arg(0)
455+
Arg[int](0)
456+
439457
def test_with_none_arguments(self) -> None:
440458
"""Arg should handle None named arguments dict."""
441459

@@ -446,28 +464,31 @@ class MyClass:
446464
obj = MyClass()
447465
assert obj.value == "default"
448466

449-
def test_absent_positional_slot_uses_default(self) -> None:
450-
"""A None slot indicates an absent argument, so default is used.
467+
def test_absent_named_slot_uses_default(self) -> None:
468+
"""An absent key in the named dict means the arg wasn't supplied.
451469
452-
Distinct from an explicit SQL NULL, which is a real value.
470+
The default is used. Distinct from an explicit SQL NULL, which is a
471+
real value (covered by test_explicit_null_through_arg_returns_none).
453472
"""
454473

455474
class MyClass:
456-
invocation = _MockInvocation(Arguments(positional=(None,)))
457-
value = Arg[int](0, default=99)
475+
invocation = _MockInvocation(Arguments(named={}))
476+
value = Arg[int]("value", default=99)
458477

459478
obj = MyClass()
460479
assert obj.value == 99
461480

462481
def test_explicit_null_through_arg_returns_none(self) -> None:
463-
"""Arg returns None for an explicit SQL NULL.
482+
"""Arg returns None for an explicit SQL NULL on a named arg.
464483
465484
Default is only consulted when the argument was not supplied.
466485
"""
467486

468487
class MyClass:
469-
invocation = _MockInvocation(Arguments(positional=(pa.scalar(None, type=pa.int64()),)))
470-
value = Arg[int](0, default=99)
488+
invocation = _MockInvocation(
489+
Arguments(named={"value": pa.scalar(None, type=pa.int64())})
490+
)
491+
value = Arg[int]("value", default=99)
471492

472493
obj = MyClass()
473494
assert obj.value is None
@@ -652,8 +673,8 @@ def test_validation_with_default(self) -> None:
652673
"""Default values should also be validated."""
653674

654675
class MyClass:
655-
invocation = _MockInvocation(Arguments(positional=()))
656-
value = Arg[int](0, default=50, ge=1, le=100)
676+
invocation = _MockInvocation(Arguments(named={}))
677+
value = Arg[int]("value", default=50, ge=1, le=100)
657678

658679
obj = MyClass()
659680
assert obj.value == 50
@@ -719,8 +740,8 @@ def test_error_includes_default_hint(self) -> None:
719740
"""Error should suggest using default value if available."""
720741

721742
class MyClass:
722-
invocation = _MockInvocation(Arguments(positional=(pa.scalar(-5),)))
723-
count = Arg[int](0, ge=0, default=10)
743+
invocation = _MockInvocation(Arguments(named={"count": pa.scalar(-5)}))
744+
count = Arg[int]("count", ge=0, default=10)
724745

725746
obj = MyClass()
726747
with pytest.raises(ArgumentValidationError) as exc_info:
@@ -1108,8 +1129,8 @@ def test_any_arrow_with_default(self) -> None:
11081129
from vgi.arguments import AnyArrow, AnyArrowValue
11091130

11101131
class MyClass:
1111-
invocation = _MockInvocation(Arguments(positional=()))
1112-
value: AnyArrow = Arg[AnyArrow](0, default="default") # type: ignore[assignment]
1132+
invocation = _MockInvocation(Arguments(named={}))
1133+
value: AnyArrow = Arg[AnyArrow]("value", default="default") # type: ignore[assignment]
11131134

11141135
obj = MyClass()
11151136
assert isinstance(obj.value, AnyArrowValue)

‎vgi/_test_fixtures/table/filters.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _format_one(f: object) -> str:
7272
class FilterEchoFunctionArgs:
7373
"""Arguments for FilterEchoFunction."""
7474

75-
count: Annotated[int, Arg(0, doc="Number of rows to generate", ge=0, default=10)]
75+
count: Annotated[int, Arg(0, doc="Number of rows to generate", ge=0)]
7676
batch_size: Annotated[int, Arg("batch_size", default=2048, doc="Batch size for output", ge=1)]
7777

7878

‎vgi/_test_fixtures/table/misc.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def process(
268268
class _OrderEchoArgs:
269269
"""Arguments for OrderEchoFunction."""
270270

271-
count: Annotated[int, Arg(0, doc="Number of rows to generate", ge=0, default=10)]
271+
count: Annotated[int, Arg(0, doc="Number of rows to generate", ge=0)]
272272
batch_size: Annotated[int, Arg("batch_size", default=2048, doc="Batch size for output", ge=1)]
273273

274274

@@ -391,7 +391,7 @@ def process(
391391
class _SampleEchoArgs:
392392
"""Arguments for SampleEchoFunction."""
393393

394-
count: Annotated[int, Arg(0, doc="Number of rows to generate", ge=0, default=10)]
394+
count: Annotated[int, Arg(0, doc="Number of rows to generate", ge=0)]
395395
batch_size: Annotated[int, Arg("batch_size", default=2048, doc="Batch size for output", ge=1)]
396396

397397

‎vgi/_test_fixtures/table/versioned.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def resolve_version(at_unit: str | None, at_value: str | None) -> int:
8080
class VersionedDataFunctionArgs:
8181
"""Arguments for VersionedDataFunction."""
8282

83-
version: Annotated[int, Arg(0, doc="Data version to return", default=_CURRENT_VERSION)]
83+
version: Annotated[int, Arg(0, doc="Data version to return")]
8484

8585

8686
@dataclass(kw_only=True)
@@ -183,7 +183,7 @@ def resolve_versioned_constraints_version(at_unit: str | None, at_value: str | N
183183
class _VersionedConstraintsArgs:
184184
"""Arguments for VersionedConstraintsScanFunction."""
185185

186-
version: Annotated[int, Arg(0, doc="Data version", default=_VERSIONED_CONSTRAINTS_CURRENT)]
186+
version: Annotated[int, Arg(0, doc="Data version")]
187187

188188

189189
@init_single_worker

‎vgi/arguments.py‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,17 @@ def __call__(
792792
if default is not _MISSING:
793793
raise ValueError("varargs=True cannot have a default value (requires at least 1 value)")
794794

795+
# Positional args cannot have defaults — DuckDB's binder always
796+
# requires the positional argument, so the default would never fire.
797+
# To make an argument optional, use a named argument (string position).
798+
if isinstance(position, int) and default is not _MISSING:
799+
raise ValueError(
800+
f"Arg(position={position}, default=...): positional arguments cannot "
801+
f"have a default value. DuckDB's binder always requires the positional "
802+
f"argument, so the default would never fire. To make this argument "
803+
f"optional, use a named argument: Arg(\"{{name}}\", default=...)."
804+
)
805+
795806
# Warn if type_bound is used with non-AnyArrow type
796807
# Check both _type_param (legacy API) and is_any (new Param API)
797808
if type_bound is not None and self._type_param is not AnyArrow and not is_any:
@@ -906,8 +917,12 @@ def __init__(
906917
"""Initialize an Arg descriptor with optional validation.
907918
908919
Args:
909-
position: Positional index (int) or named key (str).
910-
default: Default value if argument not provided. Omit for required.
920+
position: Positional index (int) or named key (str). Positional
921+
arguments are always required (DuckDB's binder always supplies
922+
them); to make an argument optional, pass a string key instead.
923+
default: Default value if argument not provided. Only valid for
924+
named (string-position) arguments — passing a default with an
925+
integer position raises ValueError. Omit for required.
911926
doc: Documentation string for this argument.
912927
ge: Minimum value (inclusive). Value must be >= this.
913928
le: Maximum value (inclusive). Value must be <= this.
@@ -930,7 +945,9 @@ def __init__(
930945
Used for tracking when AnyArrow was specified in the type hint.
931946
932947
Raises:
933-
ValueError: If conflicting constraints are specified (e.g., ge and gt).
948+
ValueError: If conflicting constraints are specified (e.g., ge and gt),
949+
or if a default value is supplied with an integer (positional)
950+
position.
934951
935952
"""
936953
# Validate constraint combinations
@@ -946,6 +963,18 @@ def __init__(
946963
if default is not _MISSING:
947964
raise ValueError("varargs=True cannot have a default value (requires at least 1 value)")
948965

966+
# Positional args cannot have defaults. DuckDB's binder always requires
967+
# a positional argument to be supplied; the default is never consulted
968+
# through the SQL path. To make an argument optional, declare it as a
969+
# named argument by passing a string position (e.g. Arg("count", default=10)).
970+
if isinstance(position, int) and default is not _MISSING:
971+
raise ValueError(
972+
f"Arg(position={position}, default=...): positional arguments cannot "
973+
f"have a default value. DuckDB's binder always requires the positional "
974+
f"argument, so the default would never fire. To make this argument "
975+
f"optional, use a named argument: Arg(\"{{name}}\", default=...)."
976+
)
977+
949978
self.position = position
950979
self.default = default
951980
self.doc = doc

0 commit comments

Comments
 (0)