Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak repr of ChatMessage #7521

Merged
merged 2 commits into from
Dec 2, 2024
Merged
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
3 changes: 3 additions & 0 deletions panel/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,3 +698,6 @@ def serialize(
prefix_with_viewable_label=prefix_with_viewable_label,
prefix_with_container_label=prefix_with_container_label,
)

def __repr__(self, depth: int = 0) -> str:
return f"ChatMessage(object={self.object!r}, user={self.user!r}, reactions={self.reactions!r})"
2 changes: 1 addition & 1 deletion panel/chat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def get_obj_label(obj):
Get the label for the object; defaults to specified object name;
if unspecified, defaults to the type name.
"""
label = obj.name
label = obj.name if hasattr(obj, "name") else ""
type_name = type(obj).__name__
# If the name is just type + ID, simply use type
# e.g. Column10241 -> Column
Expand Down
9 changes: 9 additions & 0 deletions panel/tests/chat/test_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,15 @@ def test_update_chat_log_params(self, chat_feed):
assert chat_feed._chat_log.scroll_button_threshold == 10
assert chat_feed._chat_log.auto_scroll_limit == 10

def test_repr(self, chat_feed):
chat_feed.send("A")
chat_feed.send("B")
assert repr(chat_feed) == (
"ChatFeed(_placeholder=ChatMessage, sizing_mode='stretch_width')\n"
" [0] ChatMessage(object='A', user='User', reactions=[])\n"
" [1] ChatMessage(object='B', user='User', reactions=[])"
)


@pytest.mark.xdist_group("chat")
class TestChatFeedPromptUser:
Expand Down
8 changes: 8 additions & 0 deletions panel/tests/chat/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
timestamp_pane = columns[1][5][0]
assert isinstance(timestamp_pane, HTML)
dt_str = datetime.datetime.now(tz=ZoneInfo("US/Pacific")).strftime("%H:%M")
assert timestamp_pane.object == dt_str

Check failure on line 195 in panel/tests/chat/test_message.py

View workflow job for this annotation

GitHub Actions / unit:test-310:ubuntu-latest

TestChatMessage.test_update_timestamp AssertionError: assert '17:01' == '17:02' - 17:02 ? ^ + 17:01 ? ^

special_dt = datetime.datetime(2023, 6, 24, 15)
message.timestamp = special_dt
Expand Down Expand Up @@ -390,3 +390,11 @@
def test_serialize_dataframe(self):
message = ChatMessage(DataFrame(pd.DataFrame({'a': [1, 2, 3]})))
assert message.serialize() == "DataFrame= a\n0 1\n1 2\n2 3"

def test_repr(self):
message = ChatMessage(object="Hello", user="User", avatar="A", reactions=["favorite"])
assert repr(message) == "ChatMessage(object='Hello', user='User', reactions=['favorite'])"

def test_repr_dataframe(self):
message = ChatMessage(pd.DataFrame({'a': [1, 2, 3]}), avatar="D")
assert repr(message) == "ChatMessage(object= a\n0 1\n1 2\n2 3, user='User', reactions=[])"
Loading