Skip to content
Open
Show file tree
Hide file tree
Changes from all 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/dataclasses/byte_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ def from_dict(cls, data: dict[str, Any]) -> "ByteStream":

:param data: A dictionary with keys 'data', 'meta', and 'mime_type'.

:returns: A ByteStream instance.
:returns: An instance of this class.
"""
return ByteStream(data=bytes(data["data"]), meta=data.get("meta", {}), mime_type=data.get("mime_type"))
return cls(data=bytes(data["data"]), meta=data.get("meta", {}), mime_type=data.get("mime_type"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fixes:
- |
``ByteStream.from_dict()`` now preserves subclasses when deserializing a
dictionary, instead of always returning a ``ByteStream`` instance.
11 changes: 11 additions & 0 deletions test/dataclasses/test_byte_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ def test_from_dict():
assert str(b2) == str(b)


def test_from_dict_preserves_subclass():
class CustomByteStream(ByteStream):
pass

original = CustomByteStream.from_string("Hello, world!", mime_type="text/plain", meta={"foo": "bar"})
restored = CustomByteStream.from_dict(original.to_dict())

assert isinstance(restored, CustomByteStream)
assert restored.to_dict() == original.to_dict()


def test_no_warning_on_init():
with warnings.catch_warnings():
warnings.simplefilter("error", Warning)
Expand Down
Loading