Skip to content

Commit

Permalink
Merge branch 'main' into docstrings-linting
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsbatista authored Apr 23, 2024
2 parents be983ac + f3b0bd2 commit 724c6cd
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
6 changes: 6 additions & 0 deletions haystack/components/builders/prompt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def __init__(self, template: str):
component.set_input_type(self, var, Any, "")

def to_dict(self) -> Dict[str, Any]:
"""
Returns a dictionary representation of the component.
:returns:
Serialized dictionary representation of the component.
"""
return default_to_dict(self, template=self._template_string)

@component.output_types(prompt=str)
Expand Down
8 changes: 5 additions & 3 deletions haystack/components/converters/pypdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class PyPDFConverter(Protocol):
A protocol that defines a converter which takes a PdfReader object and converts it into a Document object.
"""

def convert(self, reader: "PdfReader") -> Document:
def convert(self, reader: "PdfReader") -> Document: # noqa: D102
...

def to_dict(self):
def to_dict(self): # noqa: D102
...

@classmethod
def from_dict(cls, data):
def from_dict(cls, data): # noqa: D102
...


Expand All @@ -43,10 +43,12 @@ def convert(self, reader: "PdfReader") -> Document:
return Document(content=text)

def to_dict(self):
"""Serialize the converter to a dictionary."""
return default_to_dict(self)

@classmethod
def from_dict(cls, data):
"""Deserialize the converter from a dictionary."""
return default_from_dict(cls, data)


Expand Down
3 changes: 3 additions & 0 deletions haystack/components/evaluators/document_recall.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def __str__(self):

@staticmethod
def from_str(string: str) -> "RecallMode":
"""
Convert a string to a RecallMode enum.
"""
enum_map = {e.value: e for e in RecallMode}
mode = enum_map.get(string)
if mode is None:
Expand Down
3 changes: 3 additions & 0 deletions haystack/core/component/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def run(self, **kwargs):
class ComponentMeta(type):
@staticmethod
def positional_to_kwargs(cls_type, args) -> Dict[str, Any]:
"""
Convert positional arguments to keyword arguments based on the signature of the `__init__` method.
"""
init_signature = inspect.signature(cls_type.__init__)
init_params = {name: info for name, info in init_signature.parameters.items() if name != "self"}

Expand Down
1 change: 1 addition & 0 deletions haystack/core/component/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class InputSocket:

@property
def is_mandatory(self):
"""Check if the input is mandatory."""
return self.default_value == _empty

def __post_init__(self):
Expand Down
48 changes: 46 additions & 2 deletions haystack/dataclasses/answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Answer(Protocol):
query: str
meta: Dict[str, Any]

def to_dict(self) -> Dict[str, Any]:
def to_dict(self) -> Dict[str, Any]: # noqa: D102
...

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "Answer":
def from_dict(cls, data: Dict[str, Any]) -> "Answer": # noqa: D102
...


Expand All @@ -40,6 +40,12 @@ class Span:
end: int

def to_dict(self) -> Dict[str, Any]:
"""
Serialize the object to a dictionary.
:returns:
Serialized dictionary representation of the object.
"""
document = self.document.to_dict(flatten=False) if self.document is not None else None
document_offset = asdict(self.document_offset) if self.document_offset is not None else None
context_offset = asdict(self.context_offset) if self.context_offset is not None else None
Expand All @@ -57,6 +63,14 @@ def to_dict(self) -> Dict[str, Any]:

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "ExtractedAnswer":
"""
Deserialize the object from a dictionary.
:param data:
Dictionary representation of the object.
:returns:
Deserialized object.
"""
init_params = data.get("init_parameters", {})
if (doc := init_params.get("document")) is not None:
data["init_parameters"]["document"] = Document.from_dict(doc)
Expand Down Expand Up @@ -86,6 +100,12 @@ class Cell:
column: int

def to_dict(self) -> Dict[str, Any]:
"""
Serialize the object to a dictionary.
:returns:
Serialized dictionary representation of the object.
"""
document = self.document.to_dict(flatten=False) if self.document is not None else None
context = self.context.to_json() if self.context is not None else None
document_cells = [asdict(c) for c in self.document_cells]
Expand All @@ -104,6 +124,15 @@ def to_dict(self) -> Dict[str, Any]:

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "ExtractedTableAnswer":
"""
Deserialize the object from a dictionary.
:param data:
Dictionary representation of the object.
:returns:
Deserialized object.
"""
init_params = data.get("init_parameters", {})
if (doc := init_params.get("document")) is not None:
data["init_parameters"]["document"] = Document.from_dict(doc)
Expand All @@ -127,11 +156,26 @@ class GeneratedAnswer:
meta: Dict[str, Any] = field(default_factory=dict)

def to_dict(self) -> Dict[str, Any]:
"""
Serialize the object to a dictionary.
:returns:
Serialized dictionary representation of the object.
"""
documents = [doc.to_dict(flatten=False) for doc in self.documents]
return default_to_dict(self, data=self.data, query=self.query, documents=documents, meta=self.meta)

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "GeneratedAnswer":
"""
Deserialize the object from a dictionary.
:param data:
Dictionary representation of the object.
:returns:
Deserialized object.
"""
init_params = data.get("init_parameters", {})
if (documents := init_params.get("documents")) is not None:
data["init_parameters"]["documents"] = [Document.from_dict(d) for d in documents]
Expand Down

0 comments on commit 724c6cd

Please sign in to comment.