8000 make Variadic wrap an iterable by masci · Pull Request #163 · deepset-ai/canals · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Nov 28, 2023. It is now read-only.

make Variadic wrap an iterable #163

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions canals/component/sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@ def __post_init__(self):
# We need to "unpack" the type inside the Variadic annotation,
# otherwise the pipeline connection api will try to match
# `Annotated[type, CANALS_VARIADIC_ANNOTATION]`.
# Note Variadic is expressed as an annotation of one single type,
#
# Note1: Variadic is expressed as an annotation of one single type,
# so the return value of get_args will always be a one-item tuple.
self.type = get_args(self.type)[0]
#
# Note2: a pipeline always passes a list of items when a component
# input is declared as Variadic, so the type itself always wraps
# an iterable of the declared type. For example, Variadic[int]
# is eventually an alias for Iterable[int]. Since we're interested
# in getting the inner type `int`, we call `get_args` twice: the
# first time to get `List[int]` out of `Variadic`, the second time
# to get `int` out of `List[int]`.
self.type = get_args(get_args(self.type)[0])[0]


@dataclass
Expand Down
4 changes: 2 additions & 2 deletions canals/component/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TypeVar
from typing import TypeVar, Iterable
from typing_extensions import TypeAlias, Annotated # Python 3.8 compatibility

CANALS_VARIADIC_ANNOTATION = "__canals__variadic_t"
Expand All @@ -11,4 +11,4 @@
# This type doesn't do anything else than "marking" the contained
# type so it can be used in the `InputSocket` creation where we
# check that its annotation equals to CANALS_VARIADIC_ANNOTATION
Variadic: TypeAlias = Annotated[T, CANALS_VARIADIC_ANNOTATION]
Variadic: TypeAlias = Annotated[Iterable[T], CANALS_VARIADIC_ANNOTATION]
4 changes: 2 additions & 2 deletions sample_components/sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class Sum:
@component.output_types(total=int)
def run(self, values: Variadic[int]):
"""
:param value: the value to check the remainder of.
:param value: the values to sum.
"""
return {"total": sum(values)} # type: ignore
return {"total": sum(values)}
0