diff --git a/haystack/components/caching/cache_checker.py b/haystack/components/caching/cache_checker.py index b67608b075..6d55c57ceb 100644 --- a/haystack/components/caching/cache_checker.py +++ b/haystack/components/caching/cache_checker.py @@ -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__) @@ -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) diff --git a/haystack/components/retrievers/filter_retriever.py b/haystack/components/retrievers/filter_retriever.py index 6f9ad78b3d..8cc230b662 100644 --- a/haystack/components/retrievers/filter_retriever.py +++ b/haystack/components/retrievers/filter_retriever.py @@ -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__) @@ -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) diff --git a/haystack/components/retrievers/sentence_window_retriever.py b/haystack/components/retrievers/sentence_window_retriever.py index 370638e643..3a179f5078 100644 --- a/haystack/components/retrievers/sentence_window_retriever.py +++ b/haystack/components/retrievers/sentence_window_retriever.py @@ -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 @@ -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) diff --git a/haystack/components/writers/document_writer.py b/haystack/components/writers/document_writer.py index 7e75759638..48eb1d00f2 100644 --- a/haystack/components/writers/document_writer.py +++ b/haystack/components/writers/document_writer.py @@ -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__) @@ -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"]] diff --git a/haystack/utils/__init__.py b/haystack/utils/__init__.py index cc46f07f43..78a281a9e9 100644 --- a/haystack/utils/__init__.py +++ b/haystack/utils/__init__.py @@ -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, raise_on_invalid_filter_syntax from .jinja2_extensions import Jinja2TimeExtension @@ -29,6 +29,6 @@ "deserialize_callable", "serialize_type", "deserialize_type", - "deserialize_document_store_in_init_params_inplace", + "deserialize_component_in_init_params_inplace", "Jinja2TimeExtension", ] diff --git a/haystack/utils/docstore_deserialization.py b/haystack/utils/docstore_deserialization.py index 41d8b2714d..b328130899 100644 --- a/haystack/utils/docstore_deserialization.py +++ b/haystack/utils/docstore_deserialization.py @@ -8,19 +8,19 @@ 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: @@ -28,12 +28,12 @@ def deserialize_document_store_in_init_params_inplace(data: Dict[str, Any], key: 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) diff --git a/releasenotes/notes/rename-deserialize-in-place-function-f92bf2e801dbd940.yaml b/releasenotes/notes/rename-deserialize-in-place-function-f92bf2e801dbd940.yaml new file mode 100644 index 0000000000..5cd248e4c5 --- /dev/null +++ b/releasenotes/notes/rename-deserialize-in-place-function-f92bf2e801dbd940.yaml @@ -0,0 +1,4 @@ +--- +enhancements: + - | + The util function `deserialize_document_store_in_init_params_inplace` was renamed to `deserialize_component_in_init_params_inplace` diff --git a/test/utils/test_docstore_deserialization.py b/test/utils/test_docstore_deserialization.py index ffce99dd2c..f8249baa45 100644 --- a/test/utils/test_docstore_deserialization.py +++ b/test/utils/test_docstore_deserialization.py @@ -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 @@ -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) @@ -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": {}} @@ -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": {}} @@ -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(): @@ -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)