-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:
PyPDFToDocument
correctly serializes custom converters, deprec…
…ate `DefaultConverter` (#8430) * fix: `PyPDFToDocument` correctly serializes custom converters, deprecate `DefaultConverter` * Remove `auto` prefix from serde util function names, add unit tests
- Loading branch information
1 parent
3ae34b5
commit 0bf24c0
Showing
5 changed files
with
193 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from typing import Any, Dict | ||
|
||
from haystack.core.errors import DeserializationError, SerializationError | ||
from haystack.core.serialization import generate_qualified_class_name, import_class_by_name | ||
|
||
|
||
def serialize_class_instance(obj: Any) -> Dict[str, Any]: | ||
""" | ||
Serializes an object that has a `to_dict` method into a dictionary. | ||
:param obj: | ||
The object to be serialized. | ||
:returns: | ||
A dictionary representation of the object. | ||
:raises SerializationError: | ||
If the object does not have a `to_dict` method. | ||
""" | ||
if not hasattr(obj, "to_dict"): | ||
raise SerializationError(f"Object of class '{type(obj).__name__}' does not have a 'to_dict' method") | ||
|
||
output = obj.to_dict() | ||
return {"type": generate_qualified_class_name(type(obj)), "data": output} | ||
|
||
|
||
def deserialize_class_instance(data: Dict[str, Any]) -> Any: | ||
""" | ||
Deserializes an object from a dictionary representation generated by `auto_serialize_class_instance`. | ||
:param data: | ||
The dictionary to deserialize from. | ||
:returns: | ||
The deserialized object. | ||
:raises DeserializationError: | ||
If the serialization data is malformed, the class type cannot be imported, or the | ||
class does not have a `from_dict` method. | ||
""" | ||
if "type" not in data: | ||
raise DeserializationError("Missing 'type' in serialization data") | ||
if "data" not in data: | ||
raise DeserializationError("Missing 'data' in serialization data") | ||
|
||
try: | ||
obj_class = import_class_by_name(data["type"]) | ||
except ImportError as e: | ||
raise DeserializationError(f"Class '{data['type']}' not correctly imported") from e | ||
|
||
if not hasattr(obj_class, "from_dict"): | ||
raise DeserializationError(f"Class '{data['type']}' does not have a 'from_dict' method") | ||
|
||
return obj_class.from_dict(data["data"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
deprecations: | ||
- | | ||
The `DefaultConverter` class used by the `PyPDFToDocument` component has been deprecated. Its functionality will be merged into the component in 2.7.0. | ||
fixes: | ||
- | | ||
Fix the serialization of `PyPDFToDocument` component to prevent the default converter from being serialized unnecessarily. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from haystack.core.errors import DeserializationError, SerializationError | ||
import pytest | ||
|
||
from haystack.utils.base_serialization import serialize_class_instance, deserialize_class_instance | ||
|
||
|
||
class CustomClass: | ||
def to_dict(self): | ||
return {"key": "value", "more": False} | ||
|
||
@classmethod | ||
def from_dict(cls, data): | ||
assert data == {"key": "value", "more": False} | ||
return cls() | ||
|
||
|
||
class CustomClassNoToDict: | ||
@classmethod | ||
def from_dict(cls, data): | ||
assert data == {"key": "value", "more": False} | ||
return cls() | ||
|
||
|
||
class CustomClassNoFromDict: | ||
def to_dict(self): | ||
return {"key": "value", "more": False} | ||
|
||
|
||
def test_serialize_class_instance(): | ||
result = serialize_class_instance(CustomClass()) | ||
assert result == {"data": {"key": "value", "more": False}, "type": "test_base_serialization.CustomClass"} | ||
|
||
|
||
def test_serialize_class_instance_missing_method(): | ||
with pytest.raises(SerializationError, match="does not have a 'to_dict' method"): | ||
serialize_class_instance(CustomClassNoToDict()) | ||
|
||
|
||
def test_deserialize_class_instance(): | ||
data = {"data": {"key": "value", "more": False}, "type": "test_base_serialization.CustomClass"} | ||
|
||
result = deserialize_class_instance(data) | ||
assert isinstance(result, CustomClass) | ||
|
||
|
||
def test_deserialize_class_instance_invalid_data(): | ||
data = {"data": {"key": "value", "more": False}, "type": "test_base_serialization.CustomClass"} | ||
|
||
with pytest.raises(DeserializationError, match="Missing 'type'"): | ||
deserialize_class_instance({}) | ||
|
||
with pytest.raises(DeserializationError, match="Missing 'data'"): | ||
deserialize_class_instance({"type": "test_base_serialization.CustomClass"}) | ||
|
||
with pytest.raises( | ||
DeserializationError, match="Class 'test_base_serialization.CustomClass1' not correctly imported" | ||
): | ||
deserialize_class_instance({"type": "test_base_serialization.CustomClass1", "data": {}}) | ||
|
||
with pytest.raises( | ||
DeserializationError, | ||
match="Class 'test_base_serialization.CustomClassNoFromDict' does not have a 'from_dict' method", | ||
): | ||
deserialize_class_instance({"type": "test_base_serialization.CustomClassNoFromDict", "data": {}}) |