Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix docstring for the sample_components package #7499

Merged
merged 1 commit into from
Apr 9, 2024
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
15 changes: 10 additions & 5 deletions haystack/testing/sample_components/accumulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ def _default_function(first: int, second: int) -> int:
class Accumulate:
"""
Accumulates the value flowing through the connection into an internal attribute.
The sum function can be customized.

Example of how to deal with serialization when some of the parameters
The sum function can be customized. Example of how to deal with serialization when some of the parameters
are not directly serializable.
"""

def __init__(self, function: Optional[Callable] = None):
"""
:param function: the function to use to accumulate the values.
Class constructor

:param function:
the function to use to accumulate the values.
The function must take exactly two values.
If it's a callable, it's used as it is.
If it's a string, the component will look for it in sys.modules and
Expand All @@ -36,7 +38,8 @@ def __init__(self, function: Optional[Callable] = None):
self.state = 0
self.function: Callable = _default_function if function is None else function # type: ignore

def to_dict(self) -> Dict[str, Any]: # pylint: disable=missing-function-docstring
def to_dict(self) -> Dict[str, Any]:
"""Converts the component to a dictionary"""
module = sys.modules.get(self.function.__module__)
if not module:
raise ValueError("Could not locate the import module.")
Expand All @@ -48,7 +51,8 @@ def to_dict(self) -> Dict[str, Any]: # pylint: disable=missing-function-docstri
return default_to_dict(self, function=function_name)

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "Accumulate": # pylint: disable=missing-function-docstring
def from_dict(cls, data: Dict[str, Any]) -> "Accumulate":
"""Loads the component from a dictionary"""
if "type" not in data:
raise ComponentDeserializationError("Missing 'type' in component serialization data")
if data["type"] != f"{cls.__module__}.{cls.__name__}":
Expand All @@ -70,6 +74,7 @@ def from_dict(cls, data: Dict[str, Any]) -> "Accumulate": # pylint: disable=mis
def run(self, value: int):
"""
Accumulates the value flowing through the connection into an internal attribute.

The sum function can be customized.
"""
self.state = self.function(self.state, value)
Expand Down
2 changes: 2 additions & 0 deletions haystack/testing/sample_components/greet.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Greet:

def __init__(self, message: str = "\nGreeting component says: Hi! The value is {value}\n", log_level: str = "INFO"):
"""
Class constructor

:param message: the message to log. Can use `{value}` to embed the value.
:param log_level: the level to log at.
"""
Expand Down
5 changes: 1 addition & 4 deletions haystack/testing/sample_components/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
class Hello:
@component.output_types(output=str)
def run(self, word: str):
"""
Takes a string in input and returns "Hello, <string>!"
in output.
"""
"""Takes a string in input and returns "Hello, <string>!"in output."""
return {"output": f"Hello, {word}!"}
12 changes: 6 additions & 6 deletions haystack/testing/sample_components/joiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class StringJoiner:
@component.output_types(output=str)
def run(self, input_str: Variadic[str]):
"""
Take strings from multiple input nodes and join them
into a single one returned in output. Since `input_str`
is Variadic, we know we'll receive a List[str].
Take strings from multiple input nodes and join them into a single one returned in output.

Since `input_str` is Variadic, we know we'll receive a List[str].
"""
return {"output": " ".join(input_str)}

Expand All @@ -24,9 +24,9 @@ class StringListJoiner:
@component.output_types(output=str)
def run(self, inputs: Variadic[List[str]]):
"""
Take list of strings from multiple input nodes and join them
into a single one returned in output. Since `input_str`
is Variadic, we know we'll receive a List[List[str]].
Take list of strings from multiple input nodes and join them into a single one returned in output.

Since `input_str` is Variadic, we know we'll receive a List[List[str]].
"""
retval: List[str] = []
for list_of_strings in inputs:
Expand Down
5 changes: 2 additions & 3 deletions haystack/testing/sample_components/self_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class SelfLoop:
"""
Decreases the initial value in steps of 1 until the target value is reached.

For no good reason it uses a self-loop to do so :)
"""

Expand All @@ -14,9 +15,7 @@ def __init__(self, target: int = 0):

@component.output_types(current_value=int, final_result=int)
def run(self, values: Variadic[int]):
"""
Decreases the input value in steps of 1 until the target value is reached.
"""
"""Decreases the input value in steps of 1 until the target value is reached."""
value = values[0] # type: ignore
value -= 1
if value == self.target:
Expand Down
2 changes: 2 additions & 0 deletions haystack/testing/sample_components/subtract.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Subtract:
@component.output_types(difference=int)
def run(self, first_value: int, second_value: int):
"""
Run the component.

:param first_value: name of the connection carrying the value to subtract from.
:param second_value: name of the connection carrying the value to subtract.
"""
Expand Down
5 changes: 1 addition & 4 deletions haystack/testing/sample_components/text_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@
class TextSplitter:
@component.output_types(output=List[str])
def run(self, sentence: str):
"""
Takes a sentence in input and returns its words"
in output.
"""
"""Takes a sentence in input and returns its words in output."""
return {"output": sentence.split()}
6 changes: 2 additions & 4 deletions haystack/testing/sample_components/threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
@component
class Threshold: # pylint: disable=too-few-public-methods
"""
Redirects the value, unchanged, along a different connection whether the value is above
or below the given threshold.
Redirects the value, along a different connection whether the value is above or below the given threshold.

:param threshold: the number to compare the input value against. This is also a parameter.
"""
Expand All @@ -24,8 +23,7 @@ def __init__(self, threshold: int = 10):
@component.output_types(above=int, below=int)
def run(self, value: int, threshold: Optional[int] = None):
"""
Redirects the value, unchanged, along a different connection whether the value is above
or below the given threshold.
Redirects the value, along a different connection whether the value is above or below the given threshold.

:param threshold: the number to compare the input value against. This is also a parameter.
"""
Expand Down