Skip to content

refactor: centralise patch-error classification and apply stepdown rule#47

Merged
nathanjmcdougall merged 2 commits into
mainfrom
refactor/centralise-patch-error-classification
May 25, 2026
Merged

refactor: centralise patch-error classification and apply stepdown rule#47
nathanjmcdougall merged 2 commits into
mainfrom
refactor/centralise-patch-error-classification

Conversation

@nathanjmcdougall
Copy link
Copy Markdown
Collaborator

Summary

Confines all yamlpatch error-message substrings in one place and enforces the stepdown rule throughout the Python layer.


Centralised error classification

Adds _PatchErrorKind (enum) and _classify_patch_error() (module-level helper after Document) so that append, insert, extend_list, and sync branch on a typed value instead of matching raw strings inline.

Adds TestPatchErrorStringPins in test_edge_cases.py — seven tests that trigger each error path directly against the Rust extension and assert the expected substring is present. Any upstream change to yamlpatch error messages will fail these tests immediately, making the single point of containment visible and testable.

Stepdown rule (document.py and editor.py)

Reorders definitions so callers appear above their callees at every level:

document.py — in class:

  • _apply_patches / _from_core moved to after find_index (previously right after __init__, before all public callers)
  • upsert moved to before _create_at / _is_empty_document
  • prune_remove swapped to before remove (it delegates to remove)

document.py — module level:

  • _ensure_str_keys, _flow_seq_replacements, _PatchErrorKind, _classify_patch_error moved to after the Document class (they are called by Document methods, so Document is the higher-level abstraction)

editor.py:

  • document property moved from after original to after extract — every delegating method calls self.document, so it now follows all its callers

Add `_PatchErrorKind` enum and `_classify_patch_error()` to confine all
yamlpatch error-message substrings in one place. Refactor `append`,
`insert`, `extend_list`, and `sync` to branch on the enum rather than
matching raw strings directly.

Add `TestPatchErrorStringPins` to `test_edge_cases.py` to pin each raw
yamlpatch substring against a live Rust error so any upstream change to
the error messages is caught immediately.

Apply stepdown rule throughout `document.py` and `editor.py`:
- Move `_apply_patches`/`_from_core` to after `find_index`
- Move `_create_at`/`_is_empty_document` to after `upsert`
- Swap `prune_remove`/`remove` so the delegating wrapper comes first
- Move module-level private helpers to after the `Document` class
- Move `Editor.document` property to after all delegation methods
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the Python Document patch-application error handling to centralize Rust/yamlpatch error-message substring matching into a single classifier, and reorders definitions in document.py / editor.py to follow the “stepdown rule” (callers before callees).

Changes:

  • Added _PatchErrorKind + _classify_patch_error() and updated append, insert, extend_list, and sync to branch on the enum instead of inline string matching.
  • Reordered methods/helpers in document.py and moved Editor.document property to follow all its callers.
  • Added edge-case tests that trigger Rust patch errors and assert the expected pinned substrings / classification behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tests/test_edge_cases.py Adds tests to pin Rust/yamlpatch error substrings and validate _classify_patch_error() behavior.
src/yamltrip/editor.py Reorders document property to follow its callers (stepdown rule), no functional change.
src/yamltrip/document.py Centralizes patch-error classification and refactors sequence mutation fallbacks to use it; reorders definitions for stepdown.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/yamltrip/document.py
Document.merge() was still branching on the raw string 'unexpected node'
inline. Add _PatchErrorKind.UNEXPECTED_NODE = 'unexpected node' and the
corresponding check in _classify_patch_error(), then update merge() to
use the classifier.

Also add two new TestPatchErrorStringPins tests: one pinning the raw
substring (triggered by Op.merge_into on a scalar node) and one
verifying the classifier round-trips correctly.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@nathanjmcdougall nathanjmcdougall merged commit 936454a into main May 25, 2026
19 checks passed
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.

2 participants