Feature/add repr methods (resolves #78)#326
Open
martinritchie wants to merge 4 commits intoanthropics:mainfrom
Open
Feature/add repr methods (resolves #78)#326martinritchie wants to merge 4 commits intoanthropics:mainfrom
martinritchie wants to merge 4 commits intoanthropics:mainfrom
Conversation
…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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
2b23773 to
9683520
Compare
6 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
__repr__and__str__methods to message and content block typesSummary
Implements
__repr__and__str__methods for all message and content block classes, eliminating the need for custom display helpers. Users can now use simpleprint(message)statements instead of manual block iteration, improving debuggability and reducing boilerplate code.Resolves #78
Changes
Core Implementation
__repr__and__str__to 12 types:_truncate_repr()helper function with configurable max length (50 chars)Documentation & Examples
examples/quick_start.py(3 examples)examples/streaming_mode_ipython.py(6 examples)examples/streaming_mode.py(display_message helper)Key Benefits
print(message)instead of iterating through content blocksrepr()andstr()Testing
Version