-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add Document.get() and Editor.get() for non-raising value access
#27
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 5 commits into
main
from
23-root-property-should-return-none-on-empty-documents-instead-of-raising-queryerror
May 19, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4487710
feat: add Document.get() and Editor.get() for non-raising value access
nathanjmcdougall 3bb4fc3
chore: disable B018 (useless expression) in test suite
nathanjmcdougall 476b503
refactor: address review feedback on Document.get()
nathanjmcdougall ab5a6b0
docs: remove proposed implementations from get-method spec
nathanjmcdougall de13834
fix: translate ValueError to QueryError in Document.get()
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Document.get() — Non-Raising Value Access | ||
|
|
||
| **Date:** 2026-05-19 | ||
| **Issue:** #23 | ||
|
|
||
| ## Problem | ||
|
|
||
| Accessing `doc.root` or `doc[()]` on a document with no YAML value (empty string, comment-only, or `---` only) raises `QueryError`. This forces callers to wrap every access in try/except even though an empty document is a normal state. | ||
|
|
||
| The request proposes making `.root` return `None`, but that conflates "document has no value" with "document explicitly contains null" — two semantically different states. A `.get()` pattern keeps the distinction clear and gives callers control over the fallback. | ||
|
|
||
| ## Design | ||
|
|
||
| Add a `get(*keys, default=None)` method to `Document` and `Editor` that returns the parsed Python value at the given path, or `default` if the path doesn't exist. | ||
|
|
||
| ### Signature | ||
|
|
||
| ```python | ||
| def get(self, *keys: KeyPart, default: Any = None) -> Any: | ||
| ``` | ||
|
|
||
| ### Semantics | ||
|
|
||
| | Expression | Result | | ||
| |------------|--------| | ||
| | `doc.get()` | Root value, or `None` if empty doc | | ||
| | `doc.get('tool', 'ruff')` | Value at path, or `None` if missing | | ||
| | `doc.get('missing', default={})` | `{}` | | ||
| | `doc.get('existing_null_key')` | `None` (the actual YAML null) | | ||
| | `doc.root` | Unchanged — still raises `QueryError` on empty docs | | ||
|
|
||
| ### Null Ambiguity | ||
|
|
||
| `doc.get('key')` returns `None` both for "key missing" and "key has YAML null value". Callers who need to distinguish use `'key' in doc` first. This is the same trade-off as `dict.get()`. | ||
|
|
||
| ## Change Locations | ||
|
|
||
| - `src/yamltrip/document.py` — add `get()` method to `Document` | ||
| - `src/yamltrip/editor.py` — add `get()` method to `Editor` | ||
| - No Rust changes required | ||
|
|
||
| ## Testing | ||
|
|
||
| New tests: | ||
|
|
||
| - `doc.get()` on empty document → `None` | ||
| - `doc.get()` on comment-only document → `None` | ||
| - `doc.get()` on `---\n` document → `None` | ||
| - `doc.get()` on document with root value → returns the value | ||
| - `doc.get('key')` on existing key → returns value | ||
| - `doc.get('key')` on missing key → `None` | ||
| - `doc.get('key', default={})` on missing key → `{}` | ||
| - `doc.get('nested', 'path')` on existing nested path → value | ||
| - `doc.get('nested', 'missing')` on partial path → `None` | ||
| - `doc.get('null_key')` where value is YAML null → `None` | ||
| - `doc.root` still raises on empty docs (no regression) | ||
| - `Editor.get()` mirrors Document behavior | ||
|
|
||
| ## Scope Boundaries | ||
|
|
||
| **In scope:** | ||
| - `Document.get()` method | ||
| - `Editor.get()` method | ||
|
|
||
| **Out of scope:** | ||
| - Changing `.root` behavior | ||
| - Adding `.get()` to the stub file (`_core.pyi`) — this is Python-only | ||
| - Sentinel-based missing detection (use `in` operator for that) |
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
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
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.