Skip to content

Commit 6674b9b

Browse files
authored
fix: guard unsubscripted Variadic type in InputSocket (#11493)
1 parent d5791aa commit 6674b9b

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

haystack/core/component/types.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from types import UnionType
88
from typing import Annotated, Any, TypeAlias, TypedDict, TypeVar, get_args
99

10+
from haystack.core.errors import ComponentError
11+
1012
HAYSTACK_VARIADIC_ANNOTATION = "__haystack__variadic_t"
1113
HAYSTACK_GREEDY_VARIADIC_ANNOTATION = "__haystack__greedy_variadic_t"
1214

@@ -97,7 +99,15 @@ def __post_init__(self) -> None:
9799
# alias for Iterable[int]. Since we're interested in getting the inner type `int`, we call `get_args`
98100
# twice: the first time to get `list[int]` out of `Variadic`, the second time to get `int` out of `list[int]`.
99101
if self.is_lazy_variadic or self.is_greedy:
100-
self.type = get_args(get_args(self.type)[0])[0]
102+
outer_args = get_args(self.type)
103+
inner_type = outer_args[0]
104+
inner_args = get_args(inner_type)
105+
if not inner_args:
106+
raise ComponentError(
107+
f"Variadic input '{self.name}' must have a type argument, e.g. Variadic[int]. "
108+
f"Got bare {inner_type!r} without a type argument."
109+
)
110+
self.type = inner_args[0]
101111

102112

103113
class InputSocketTypeDescriptor(TypedDict):
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Fix ``InputSocket.__post_init__`` crashing with ``IndexError`` when a Variadic input
5+
uses a bare ``Iterable`` without a type argument (e.g. ``Annotated[Iterable, HAYSTACK_VARIADIC_ANNOTATION]``).
6+
Now raises ``ComponentError`` with a clear message instead of an unhelpful ``IndexError``.

test/core/component/test_sockets.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,17 @@ def test_contains(self):
8585
assert "input_1" in io
8686
assert "input_2" in io
8787
assert "invalid" not in io
88+
89+
90+
def test_input_socket_variadic_without_type_arg_raises_component_error():
91+
"""Bare Variadic (no type argument) should raise ComponentError, not IndexError."""
92+
from collections.abc import Iterable
93+
from typing import Annotated
94+
95+
from haystack.core.component.types import HAYSTACK_VARIADIC_ANNOTATION
96+
from haystack.core.errors import ComponentError
97+
98+
bare_variadic = Annotated[Iterable, HAYSTACK_VARIADIC_ANNOTATION]
99+
100+
with pytest.raises(ComponentError, match="must have a type argument"):
101+
InputSocket("inputs", bare_variadic)

0 commit comments

Comments
 (0)