-
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`
- Loading branch information
Showing
4 changed files
with
126 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 auto_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 auto_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