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: renaming deserialize_document_store_in_init_params_inplace #8379

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions haystack/components/caching/cache_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from haystack import Document, component, default_from_dict, default_to_dict, logging
from haystack.document_stores.types import DocumentStore
from haystack.utils import deserialize_document_store_in_init_params_inplace
from haystack.utils import deserialize_component_in_init_params_inplace

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -73,7 +73,7 @@ def from_dict(cls, data: Dict[str, Any]) -> "CacheChecker":
Deserialized component.
"""
# deserialize the document store
deserialize_document_store_in_init_params_inplace(data)
deserialize_component_in_init_params_inplace(data)

return default_from_dict(cls, data)

Expand Down
4 changes: 2 additions & 2 deletions haystack/components/retrievers/filter_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from haystack import Document, component, default_from_dict, default_to_dict, logging
from haystack.document_stores.types import DocumentStore
from haystack.utils import deserialize_document_store_in_init_params_inplace
from haystack.utils import deserialize_component_in_init_params_inplace

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -78,7 +78,7 @@ def from_dict(cls, data: Dict[str, Any]) -> "FilterRetriever":
The deserialized component.
"""
# deserialize the document store
deserialize_document_store_in_init_params_inplace(data)
deserialize_component_in_init_params_inplace(data)

return default_from_dict(cls, data)

Expand Down
4 changes: 2 additions & 2 deletions haystack/components/retrievers/sentence_window_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from haystack import Document, component, default_from_dict, default_to_dict
from haystack.document_stores.types import DocumentStore
from haystack.utils import deserialize_document_store_in_init_params_inplace
from haystack.utils import deserialize_component_in_init_params_inplace


@component
Expand Down Expand Up @@ -138,7 +138,7 @@ def from_dict(cls, data: Dict[str, Any]) -> "SentenceWindowRetriever":
Deserialized component.
"""
# deserialize the document store
deserialize_document_store_in_init_params_inplace(data)
deserialize_component_in_init_params_inplace(data)

# deserialize the component
return default_from_dict(cls, data)
Expand Down
4 changes: 2 additions & 2 deletions haystack/components/writers/document_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from haystack import Document, component, default_from_dict, default_to_dict, logging
from haystack.document_stores.types import DocumentStore, DuplicatePolicy
from haystack.utils import deserialize_document_store_in_init_params_inplace
from haystack.utils import deserialize_component_in_init_params_inplace

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -74,7 +74,7 @@ def from_dict(cls, data: Dict[str, Any]) -> "DocumentWriter":
If the document store is not properly specified in the serialization data or its type cannot be imported.
"""
# deserialize the document store
deserialize_document_store_in_init_params_inplace(data)
deserialize_component_in_init_params_inplace(data)

data["init_parameters"]["policy"] = DuplicatePolicy[data["init_parameters"]["policy"]]

Expand Down
4 changes: 2 additions & 2 deletions haystack/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .auth import Secret, deserialize_secrets_inplace
from .callable_serialization import deserialize_callable, serialize_callable
from .device import ComponentDevice, Device, DeviceMap, DeviceType
from .docstore_deserialization import deserialize_document_store_in_init_params_inplace
from .docstore_deserialization import deserialize_component_in_init_params_inplace
from .expit import expit
from .filters import document_matches_filter
from .jinja2_extensions import Jinja2TimeExtension
Expand All @@ -28,6 +28,6 @@
"deserialize_callable",
"serialize_type",
"deserialize_type",
"deserialize_document_store_in_init_params_inplace",
"deserialize_component_in_init_params_inplace",
"Jinja2TimeExtension",
]
20 changes: 10 additions & 10 deletions haystack/utils/docstore_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
from haystack.core.serialization import default_from_dict, import_class_by_name


def deserialize_document_store_in_init_params_inplace(data: Dict[str, Any], key: str = "document_store"):
def deserialize_component_in_init_params_inplace(data: Dict[str, Any], key: str = "document_store"):
"""
Deserializes a generic document store from the init_parameters of a serialized component in place.
Deserializes a component from the `init_parameters` of a serialized component in place.

:param data:
The dictionary to deserialize from.
:param key:
The key in the `data["init_parameters"]` dictionary where the document store is specified.
The key in the `data["init_parameters"]` dictionary where the component is specified.
:returns:
The dictionary, with the document store deserialized.
The dictionary, with the component deserialized.

:raises DeserializationError:
If the document store is not properly specified in the serialization data or its type cannot be imported.
If the component is not properly specified in the serialization data or its type cannot be imported.
"""
init_params = data.get("init_parameters", {})
if key not in init_params:
raise DeserializationError(f"Missing '{key}' in serialization data")
if "type" not in init_params[key]:
raise DeserializationError(f"Missing 'type' in {key} serialization data")

doc_store_data = data["init_parameters"][key]
component_data = data["init_parameters"][key]
try:
doc_store_class = import_class_by_name(doc_store_data["type"])
doc_store_class = import_class_by_name(component_data["type"])
except ImportError as e:
raise DeserializationError(f"Class '{doc_store_data['type']}' not correctly imported") from e
raise DeserializationError(f"Class '{component_data['type']}' not correctly imported") from e
if hasattr(doc_store_class, "from_dict"):
data["init_parameters"][key] = doc_store_class.from_dict(doc_store_data)
data["init_parameters"][key] = doc_store_class.from_dict(component_data)
else:
data["init_parameters"][key] = default_from_dict(doc_store_class, doc_store_data)
data["init_parameters"][key] = default_from_dict(doc_store_class, component_data)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
enhancements:
- |
The util function `deserialize_document_store_in_init_params_inplace` was renamed to deserialize_component_in_init_params_inplace
davidsbatista marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 7 additions & 7 deletions test/utils/test_docstore_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from haystack.document_stores.in_memory.document_store import InMemoryDocumentStore
from haystack.utils.docstore_deserialization import deserialize_document_store_in_init_params_inplace
from haystack.utils.docstore_deserialization import deserialize_component_in_init_params_inplace
from haystack.core.errors import DeserializationError


Expand All @@ -25,7 +25,7 @@ def test_deserialize_document_store_in_init_params_inplace():
},
}

deserialize_document_store_in_init_params_inplace(data)
deserialize_component_in_init_params_inplace(data)
assert isinstance(data["init_parameters"]["document_store"], InMemoryDocumentStore)


Expand All @@ -42,7 +42,7 @@ def test_from_dict_is_called():
}

with patch.object(InMemoryDocumentStore, "from_dict") as mock_from_dict:
deserialize_document_store_in_init_params_inplace(data)
deserialize_component_in_init_params_inplace(data)

mock_from_dict.assert_called_once_with(
{"type": "haystack.document_stores.in_memory.document_store.InMemoryDocumentStore", "init_parameters": {}}
Expand All @@ -59,7 +59,7 @@ def test_default_from_dict_is_called():
}

with patch("haystack.utils.docstore_deserialization.default_from_dict") as mock_default_from_dict:
deserialize_document_store_in_init_params_inplace(data)
deserialize_component_in_init_params_inplace(data)

mock_default_from_dict.assert_called_once_with(
FakeDocumentStore, {"type": "test_docstore_deserialization.FakeDocumentStore", "init_parameters": {}}
Expand All @@ -69,13 +69,13 @@ def test_default_from_dict_is_called():
def test_missing_document_store_key():
data = {"init_parameters": {"policy": "SKIP"}}
with pytest.raises(DeserializationError):
deserialize_document_store_in_init_params_inplace(data)
deserialize_component_in_init_params_inplace(data)


def test_missing_type_key_in_document_store():
data = {"init_parameters": {"document_store": {"init_parameters": {}}, "policy": "SKIP"}}
with pytest.raises(DeserializationError):
deserialize_document_store_in_init_params_inplace(data)
deserialize_component_in_init_params_inplace(data)


def test_invalid_class_import():
Expand All @@ -86,4 +86,4 @@ def test_invalid_class_import():
}
}
with pytest.raises(DeserializationError):
deserialize_document_store_in_init_params_inplace(data)
deserialize_component_in_init_params_inplace(data)
Loading