-
Notifications
You must be signed in to change notification settings - Fork 0
feat: human-readable path formatting in NodeTypeError messages #40
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
Merged
nathanjmcdougall
merged 4 commits into
main
from
37-error-messages-expose-raw-tuple-repr-in-nodetypeerror
May 22, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c137c87
docs: add design spec for human-readable NodeTypeError path formatting
nathanjmcdougall 810bc46
feat: add format_path helper in _display.py
nathanjmcdougall 0c4b914
feat: use format_path in NodeTypeError messages
nathanjmcdougall 3348e25
fix: quote keys containing > in format_path to avoid separator ambiguity
nathanjmcdougall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Design: Human-Readable Path Formatting in NodeTypeError Messages | ||
|
|
||
| **Date:** 2026-05-22 | ||
| **Status:** Approved | ||
|
|
||
| ## Problem | ||
|
|
||
| `NodeTypeError` messages interpolate raw Python tuples into error strings: | ||
|
|
||
| ```python | ||
| msg = f"Value at {keys} is not a list" | ||
| # Produces: "Value at ('repos',) is not a list" | ||
| ``` | ||
|
|
||
| The tuple syntax (`('repos',)`) is implementation noise that leaks internal representation to users. | ||
|
|
||
| ## Solution | ||
|
|
||
| Introduce a `format_path` helper in a new `_display.py` module that converts key tuples to a human-readable path string using ` > ` as separator. | ||
|
|
||
| ### Output Examples | ||
|
|
||
| | Input | Output | | ||
| |-------|--------| | ||
| | `('repos',)` | `repos` | | ||
| | `('repos', 0, 'steps')` | `repos > 0 > steps` | | ||
| | `()` | `<root>` | | ||
|
|
||
| ### Separator Choice: ` > ` (ASCII arrow) | ||
|
|
||
| - Unambiguous: no YAML key naturally contains ` > ` | ||
| - Works in all terminals and log viewers (no UTF-8 issues) | ||
| - Easy to grep and type | ||
| - Reads naturally as "drill into" | ||
|
|
||
| ## New Module: `src/yamltrip/_display.py` | ||
|
|
||
| Single function: | ||
|
|
||
| ```python | ||
| def format_path(keys: tuple[str | int, ...]) -> str: | ||
| if not keys: | ||
| return "<root>" | ||
| return " > ".join(str(k) for k in keys) | ||
| ``` | ||
|
|
||
| ## Affected Call Sites (3) | ||
|
|
||
| All in `src/yamltrip/document.py`, all with the same pattern: | ||
|
|
||
| 1. `remove_from_list` — type-checks retrieved value is a list | ||
| 2. `ensure_in_list` — type-checks retrieved value is a list | ||
| 3. `find_index` — type-checks retrieved value is a list | ||
|
|
||
| Each changes from: | ||
| ```python | ||
| msg = f"Value at {keys} is not a list" | ||
| ``` | ||
| To: | ||
| ```python | ||
| msg = f"Value at {format_path(keys)} is not a list" | ||
| ``` | ||
|
|
||
| ## Out of Scope | ||
|
|
||
| - Rust-originating error messages (lines that use `msg = str(e)`) — these come from the Rust layer and don't contain tuple repr. | ||
| - Reformatting other exception types. | ||
|
|
||
| ## Testing | ||
|
|
||
| - Unit test for `format_path` covering: single key, multi-key, integer indices, empty tuple. | ||
| - Update any existing tests that assert on `NodeTypeError` message content. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """Display helpers for human-readable formatting.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
|
|
||
| def format_path(keys: tuple[str | int, ...]) -> str: | ||
| """Format a key tuple as a human-readable path string. | ||
|
|
||
| Examples: | ||
| >>> format_path(("repos",)) | ||
| 'repos' | ||
| >>> format_path(("repos", 0, "steps")) | ||
| 'repos > 0 > steps' | ||
| >>> format_path(("a", "b>c")) | ||
| "a > 'b>c'" | ||
| >>> format_path(()) | ||
| '<root>' | ||
| """ | ||
| if not keys: | ||
| return "<root>" | ||
|
|
||
| def _fmt(k: str | int) -> str: | ||
| s = str(k) | ||
| if ">" in s: | ||
| return f"'{s}'" | ||
| return s | ||
|
|
||
| return " > ".join(_fmt(k) for k in keys) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from yamltrip._display import format_path | ||
|
|
||
|
|
||
| class TestFormatPath: | ||
| def test_single_string_key(self): | ||
| assert format_path(("repos",)) == "repos" | ||
|
|
||
| def test_multi_key_path(self): | ||
| assert format_path(("repos", 0, "steps")) == "repos > 0 > steps" | ||
|
|
||
| def test_integer_index(self): | ||
| assert format_path(("items", 2)) == "items > 2" | ||
|
|
||
| def test_empty_tuple_returns_root(self): | ||
| assert format_path(()) == "<root>" | ||
|
|
||
| def test_key_containing_gt_is_quoted(self): | ||
| assert format_path(("a", "b>c")) == "a > 'b>c'" | ||
|
|
||
| def test_bare_gt_key_is_quoted(self): | ||
| assert format_path((">",)) == "'>'" |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.