Skip to content

Conversation

@martinritchie
Copy link

Add __repr__ and __str__ methods to message and content block types

Summary

Implements __repr__ and __str__ methods for all message and content block classes, eliminating the need for custom display helpers. Users can now use simple print(message) statements instead of manual block iteration, improving debuggability and reducing boilerplate code.

Resolves #78

Changes

Core Implementation

  • Added __repr__ and __str__ to 12 types:
    • Content blocks: TextBlock, ThinkingBlock, ToolUseBlock, ToolResultBlock
    • Messages: UserMessage, AssistantMessage, SystemMessage, ResultMessage, StreamEvent
    • Permission results: PermissionResultAllow, PermissionResultDeny
  • Introduced reusable _truncate_repr() helper function with configurable max length (50 chars)
  • Message classes compose content block repr/str methods for hierarchical display

Documentation & Examples

  • Updated README.md basic usage example to demonstrate simplified message display
  • Simplified examples in:
    • examples/quick_start.py (3 examples)
    • examples/streaming_mode_ipython.py (6 examples)
    • examples/streaming_mode.py (display_message helper)

Key Benefits

  • Users can now use print(message) instead of iterating through content blocks
  • Reduced boilerplate from 5+ lines to single line in common cases
  • Technical and user-friendly representations available via repr() and str()

Testing

  • 12 new tests covering all implemented methods
  • All 129 tests pass (117 original + 12 new)
  • No regressions in existing functionality

Version

  • Bumped to 0.1.7

martinritchie and others added 5 commits November 13, 2025 11:45
…ypes

Implement convenience methods for all message and content block classes:
- TextBlock, ThinkingBlock, ToolUseBlock, ToolResultBlock
- UserMessage, AssistantMessage, SystemMessage, ResultMessage, StreamEvent
- PermissionResultAllow, PermissionResultDeny

Features:
- __repr__ provides technical representation with truncation for long content
- __str__ provides user-friendly output with proper formatting
- Single _REPR_MAX_LENGTH constant (50 chars) for consistent truncation
- Reusable _truncate_repr() helper function (DRY principle)
- Message classes compose content block repr/str methods

All repr implementations show class names and key fields for debugging.
All str implementations provide concise, human-readable output.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Add TestReprStr class with 12 tests covering all types:
- TextBlock, ThinkingBlock, ToolUseBlock, ToolResultBlock
- UserMessage (string and block variants)
- AssistantMessage, SystemMessage, ResultMessage, StreamEvent
- PermissionResultAllow, PermissionResultDeny

Each test validates:
- repr() contains class name and key identifiers
- str() provides user-friendly output
- Composition patterns work correctly

Tests are compact (2-3 lines) and focused on behavior validation.
All 129 tests pass with no regressions.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Update examples to use message __str__ methods instead of manual
block iteration and type checking:

Before:
  if isinstance(msg, AssistantMessage):
    for block in msg.content:
      if isinstance(block, TextBlock):
        print(f"Claude: {block.text}")

After:
  if isinstance(msg, AssistantMessage):
    print(f"Claude: {msg}")

Changes:
- quick_start.py: Simplify all 3 examples
- streaming_mode_ipython.py: Simplify all 6 examples
- streaming_mode.py: Simplify display_message() helper function

This demonstrates how the new __repr__ and __str__ methods eliminate
boilerplate code and make examples more readable.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Updated the basic query example in the README to demonstrate the simplified
message display using the new __str__ method instead of manually iterating
through content blocks.

Also removed unnecessary TextBlock import from the example code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@martinritchie martinritchie changed the title Feature/add repr methods Feature/add repr methods (resolved #78) Nov 13, 2025
@martinritchie martinritchie changed the title Feature/add repr methods (resolved #78) Feature/add repr methods (resolves #78) Nov 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support reprs for message types

1 participant