mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-12-12 15:27:06 +00:00
refactor: Rename deserialize_document_store_in_init_parameters (#8302)
* 8259 * update function name * rename and update docstring * fix linting * add a release note
This commit is contained in:
parent
7dbc51a3e7
commit
e614fa0c62
@ -6,7 +6,7 @@ from typing import Any, Dict, List
|
||||
|
||||
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_parameters
|
||||
from haystack.utils import deserialize_document_store_in_init_params_inplace
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -73,7 +73,7 @@ class CacheChecker:
|
||||
Deserialized component.
|
||||
"""
|
||||
# deserialize the document store
|
||||
data = deserialize_document_store_in_init_parameters(data)
|
||||
deserialize_document_store_in_init_params_inplace(data)
|
||||
|
||||
return default_from_dict(cls, data)
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ from typing import Any, Dict, List, Optional
|
||||
|
||||
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_parameters
|
||||
from haystack.utils import deserialize_document_store_in_init_params_inplace
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -78,7 +78,7 @@ class FilterRetriever:
|
||||
The deserialized component.
|
||||
"""
|
||||
# deserialize the document store
|
||||
data = deserialize_document_store_in_init_parameters(data)
|
||||
deserialize_document_store_in_init_params_inplace(data)
|
||||
|
||||
return default_from_dict(cls, data)
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ from typing import Any, Dict, List
|
||||
|
||||
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_parameters
|
||||
from haystack.utils import deserialize_document_store_in_init_params_inplace
|
||||
|
||||
|
||||
@component
|
||||
@ -131,7 +131,7 @@ class SentenceWindowRetriever:
|
||||
Deserialized component.
|
||||
"""
|
||||
# deserialize the document store
|
||||
data = deserialize_document_store_in_init_parameters(data)
|
||||
deserialize_document_store_in_init_params_inplace(data)
|
||||
|
||||
# deserialize the component
|
||||
return default_from_dict(cls, data)
|
||||
|
||||
@ -6,7 +6,7 @@ from typing import Any, Dict, List, Optional
|
||||
|
||||
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_parameters
|
||||
from haystack.utils import deserialize_document_store_in_init_params_inplace
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -74,7 +74,7 @@ class DocumentWriter:
|
||||
If the document store is not properly specified in the serialization data or its type cannot be imported.
|
||||
"""
|
||||
# deserialize the document store
|
||||
data = deserialize_document_store_in_init_parameters(data)
|
||||
deserialize_document_store_in_init_params_inplace(data)
|
||||
|
||||
data["init_parameters"]["policy"] = DuplicatePolicy[data["init_parameters"]["policy"]]
|
||||
|
||||
|
||||
@ -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_parameters
|
||||
from .docstore_deserialization import deserialize_document_store_in_init_params_inplace
|
||||
from .expit import expit
|
||||
from .filters import document_matches_filter
|
||||
from .jupyter import is_in_jupyter
|
||||
@ -27,5 +27,5 @@ __all__ = [
|
||||
"deserialize_callable",
|
||||
"serialize_type",
|
||||
"deserialize_type",
|
||||
"deserialize_document_store_in_init_parameters",
|
||||
"deserialize_document_store_in_init_params_inplace",
|
||||
]
|
||||
|
||||
@ -8,9 +8,9 @@ from haystack import DeserializationError
|
||||
from haystack.core.serialization import default_from_dict, import_class_by_name
|
||||
|
||||
|
||||
def deserialize_document_store_in_init_parameters(data: Dict[str, Any], key: str = "document_store") -> Dict[str, Any]:
|
||||
def deserialize_document_store_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.
|
||||
Deserializes a generic document store from the init_parameters of a serialized component in place.
|
||||
|
||||
:param data:
|
||||
The dictionary to deserialize from.
|
||||
@ -37,5 +37,3 @@ def deserialize_document_store_in_init_parameters(data: Dict[str, Any], key: str
|
||||
data["init_parameters"][key] = doc_store_class.from_dict(doc_store_data)
|
||||
else:
|
||||
data["init_parameters"][key] = default_from_dict(doc_store_class, doc_store_data)
|
||||
|
||||
return data
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
---
|
||||
enhancements:
|
||||
- |
|
||||
Refactor deserialize_document_store_in_init_parameters so that new function name
|
||||
indicates that the operation occurs in place, with no return value.
|
||||
@ -6,7 +6,7 @@ from unittest.mock import patch
|
||||
import pytest
|
||||
|
||||
from haystack.document_stores.in_memory.document_store import InMemoryDocumentStore
|
||||
from haystack.utils.docstore_deserialization import deserialize_document_store_in_init_parameters
|
||||
from haystack.utils.docstore_deserialization import deserialize_document_store_in_init_params_inplace
|
||||
from haystack.core.errors import DeserializationError
|
||||
|
||||
|
||||
@ -14,7 +14,7 @@ class FakeDocumentStore:
|
||||
pass
|
||||
|
||||
|
||||
def test_deserialize_document_store_in_init_parameters():
|
||||
def test_deserialize_document_store_in_init_params_inplace():
|
||||
data = {
|
||||
"type": "haystack.components.writers.document_writer.DocumentWriter",
|
||||
"init_parameters": {
|
||||
@ -25,8 +25,8 @@ def test_deserialize_document_store_in_init_parameters():
|
||||
},
|
||||
}
|
||||
|
||||
result = deserialize_document_store_in_init_parameters(data)
|
||||
assert isinstance(result["init_parameters"]["document_store"], InMemoryDocumentStore)
|
||||
deserialize_document_store_in_init_params_inplace(data)
|
||||
assert isinstance(data["init_parameters"]["document_store"], InMemoryDocumentStore)
|
||||
|
||||
|
||||
def test_from_dict_is_called():
|
||||
@ -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_parameters(data)
|
||||
deserialize_document_store_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_parameters(data)
|
||||
deserialize_document_store_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_parameters(data)
|
||||
deserialize_document_store_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_parameters(data)
|
||||
deserialize_document_store_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_parameters(data)
|
||||
deserialize_document_store_in_init_params_inplace(data)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user