feat: Add programmatic diff access API with Redline dataclass#63
Merged
Conversation
Resolves #58 Adds a user-friendly programmatic API for accessing and filtering diff results without parsing output strings. - Structured representation of changes (delete, insert, replace) - Direct access to changed text via `source_text` and `test_text` - Token position information via `source_position` and `test_position` - No "equal" operations included (only actual changes) - `r.changes` - Returns list of Redline objects - `r.redlines` - Alias for changes (more intuitive naming) - Excludes "equal" operations automatically - `r.get_changes(operation="delete")` - Filter deletions - `r.get_changes(operation="insert")` - Filter insertions - `r.get_changes(operation="replace")` - Filter replacements - `r.get_changes()` - Get all changes - `r.stats()` - Returns Stats dataclass - Provides: total_changes, deletions, insertions, replacements⚠️ **The `r.redlines` property return type has changed:** - **Before**: Returned internal `Redline` objects (included "equal" operations) - **After**: Returns public `Redline` dataclass objects (only actual changes) If you were using `r.redlines` directly in your code: - The property now returns user-friendly `Redline` objects - "equal" operations are no longer included - Internal rendering code now uses `r._diff_ops` instead **Migration**: Most users should not be affected as `r.redlines` was primarily used internally for rendering. If you need the old behavior, the rendering methods (`output_markdown`, `output_rich`) continue to work unchanged. - Renamed internal `Redline` class to `DiffOperation` (for rendering) - Created public `Redline` class (user-facing API) - `r._diff_ops` - Internal property for rendering code - Maintains backward compatibility for existing rendering methods ```python from redlines import Redlines, Redline, Stats test = Redlines( "The quick brown fox jumps over the lazy dog.", "The quick brown fox walks past the lazy dog." ) for redline in test.redlines: print(f"{redline.operation}: {redline.source_text} -> {redline.test_text}") replacements = test.get_changes(operation="replace") stats = test.stats() print(f"Total changes: {stats.total_changes}") ``` - Added 17 comprehensive tests in test_changes_api.py - All 30 tests pass - Covers: replacements, insertions, deletions, filtering, stats, edge cases 1. Better naming: "Redline" instead of "Change" (matches domain) 2. Separate source_text & test_text fields (more useful than single 'text') 3. Excludes "equal" operations (only actual changes) 4. Added r.redlines as intuitive primary property 5. Comprehensive documentation with examples 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
fe18fd6 to
249b90b
Compare
- Add `from __future__ import annotations` to processor.py for consistency - Replace `import typing as t` with direct Literal import - Change `t.Literal` to `Literal` for cleaner code - Add `-> None` return type annotations to all test functions All changes ensure compatibility with Python 3.10-3.14 and pass mypy checks. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
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.
Summary
Adds a user-friendly programmatic API for accessing and filtering diff results without parsing output strings.
Resolves #58
New Features
🎯 Redline Dataclass
delete,insert,replace)source_textandtest_textsource_positionandtest_position📊 Properties
r.changes- Returns list of Redline objectsr.redlines- Alias for changes (more intuitive naming)🔍 Filtering API
📈 Statistics API
The
r.redlinesproperty return type has changed:Redlinedataclass objects (only actual changes)Impact: Minimal -
r.redlineswas primarily used internally. All rendering methods (output_markdown,output_rich) continue to work unchanged.API Example
Testing
test_changes_api.pyImplementation Details
Internal Refactoring
Redlineclass toDiffOperation(for rendering)Redlineclass (user-facing API)r._diff_opsinternal property for rendering codeImprovements Over Original Proposal
source_text&test_textfields (more useful than single 'text')r.redlinesas intuitive primary property🤖 Generated with Claude Code