From 901f17dfda8f72fab4fff1a9c77fe637552cb411 Mon Sep 17 00:00:00 2001 From: tstadel <60758086+tstadel@users.noreply.github.com> Date: Tue, 7 Jan 2025 18:20:07 +0100 Subject: [PATCH] apply feedback --- haystack/dataclasses/byte_stream.py | 15 ++++++++++----- test/dataclasses/test_byte_stream.py | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/haystack/dataclasses/byte_stream.py b/haystack/dataclasses/byte_stream.py index 43df1b734b..34b66add84 100644 --- a/haystack/dataclasses/byte_stream.py +++ b/haystack/dataclasses/byte_stream.py @@ -7,7 +7,7 @@ from typing import Any, Dict, Optional -@dataclass +@dataclass(repr=False) class ByteStream: """ Base data class representing a binary object in the Haystack API. @@ -64,9 +64,14 @@ def to_string(self, encoding: str = "utf-8") -> str: """ return self.data.decode(encoding) - def __str__(self) -> str: + def __repr__(self) -> str: """ - Returns a string representation of the ByteStream, truncating the data to 1KB. + Return a string representation of the ByteStream, truncating the data to 100 bytes. """ - truncated = self.data[:1024] + b"..." if len(self.data) > 1024 else self.data - return f"ByteStream(data={truncated!r}, mime_type={self.mime_type!r}, meta={self.meta!r})" + fields = [] + truncated_data = self.data[:100] + b"..." if len(self.data) > 100 else self.data + fields.append(f"data={truncated_data!r}") + fields.append(f"meta={self.meta!r}") + fields.append(f"mime_type={self.mime_type!r}") + fields_str = ", ".join(fields) + return f"{self.__class__.__name__}({fields_str})" diff --git a/test/dataclasses/test_byte_stream.py b/test/dataclasses/test_byte_stream.py index 097769d118..1858aad83c 100644 --- a/test/dataclasses/test_byte_stream.py +++ b/test/dataclasses/test_byte_stream.py @@ -74,9 +74,9 @@ def test_to_file(tmp_path, request): def test_str_truncation(): - test_str = "1234567890" * 1000 + test_str = "1234567890" * 100 b = ByteStream.from_string(test_str, mime_type="text/plain", meta={"foo": "bar"}) string_repr = str(b) - assert len(string_repr) < 1200 + assert len(string_repr) < 200 assert "text/plain" in string_repr assert "foo" in string_repr