-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add find_index() for list-of-dicts lookup
#36
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
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6fe43cd
feat: add Document.find_index() for list-of-dicts lookup
nathanjmcdougall 7c62eaa
feat: add Editor.find_index() delegating to Document
nathanjmcdougall c536e71
docs: add spec and plan for find_index feature
nathanjmcdougall 64cc3a9
refactor: add equality semantics note to find_index docstring
nathanjmcdougall b4b35ab
test: add root-list find_index coverage
nathanjmcdougall 0c8bdd1
docs: add find_index() to README API overview
nathanjmcdougall 1b6fd16
fix: address review comments for find_index
nathanjmcdougall 0e3f03c
merge: resolve conflicts with main
nathanjmcdougall d4b5051
fix: require key presence in find_index matching
nathanjmcdougall 475311d
test: add missing-key-does-not-match-none coverage for find_index
nathanjmcdougall f458e05
docs: use list-of-dicts context for find_index README example
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,105 @@ | ||
| # Document.find_index() — Find Item in List-of-Dicts | ||
|
|
||
| **Date:** 2026-05-22 | ||
|
|
||
| ## Problem | ||
|
|
||
| YAML configs frequently use lists of dicts keyed by a distinguishing field: | ||
|
|
||
| ```yaml | ||
| repos: | ||
| - repo: https://github.com/pre-commit/pre-commit-hooks | ||
| hooks: [...] | ||
| - repo: https://github.com/astral-sh/ruff-pre-commit | ||
| hooks: [...] | ||
| ``` | ||
|
|
||
| Finding an item by field value currently requires manual iteration: | ||
|
|
||
| ```python | ||
| repos = doc["repos"] | ||
| idx = next((i for i, r in enumerate(repos) if r["repo"] == url), None) | ||
| doc = doc.replace("repos", idx, "hooks", value=new_hooks) | ||
| ``` | ||
|
|
||
| This is verbose, error-prone, and repeated across callers. | ||
|
|
||
| ## Design | ||
|
|
||
| Add a `find_index` method to `Document` and `Editor` that returns the index of the first list item matching a set of key/value constraints. | ||
|
|
||
| ### Signature | ||
|
|
||
| ```python | ||
| def find_index(self, *keys: KeyPart, where: dict[str, Any]) -> int | None: | ||
| ``` | ||
|
|
||
| ### Semantics | ||
|
|
||
| | Expression | Result | | ||
| |------------|--------| | ||
| | `doc.find_index("repos", where={"repo": url})` | Index of first item where `item["repo"] == url`, or `None` | | ||
| | `doc.find_index("repos", where={"repo": url, "rev": "v1"})` | First item matching *all* pairs (AND semantics) | | ||
| | `doc.find_index("repos", where={"repo": "nonexistent"})` | `None` | | ||
| | `doc.find_index("steps", where={"uses": "actions/checkout@v4"})` | Works for any list-of-dicts | | ||
|
|
||
| ### Behavior | ||
|
|
||
| 1. Retrieve the parsed value at `keys` | ||
| 2. If value is not a list, raise `NodeTypeError` | ||
| 3. If path doesn't exist, raise `QueryError` | ||
| 4. Iterate items left-to-right; return index of first item where `item[k] == v` for all `(k, v)` in `where` | ||
| 5. Items that are not dicts are skipped (no error) | ||
| 6. Return `None` if no item matches | ||
|
|
||
| ### Error Cases | ||
|
|
||
| | Condition | Raised | | ||
| |-----------|--------| | ||
| | Path doesn't exist | `QueryError` | | ||
| | Value at path is not a list | `NodeTypeError` | | ||
| | `where` is empty | `ValueError` | | ||
|
|
||
| ### Editor Delegation | ||
|
|
||
| ```python | ||
| class Editor: | ||
| def find_index(self, *keys: KeyPart, where: dict[str, Any]) -> int | None: | ||
| return self.document.find_index(*keys, where=where) | ||
| ``` | ||
|
|
||
| ## Change Locations | ||
|
|
||
| - `src/yamltrip/document.py` — add `find_index()` method to `Document` | ||
| - `src/yamltrip/editor.py` — add `find_index()` method to `Editor` | ||
| - `src/yamltrip/_core.pyi` — no changes (Python-only logic) | ||
| - No Rust changes required | ||
|
|
||
| ## Testing | ||
|
|
||
| New tests: | ||
|
|
||
| - `doc.find_index("repos", where={"repo": url})` → correct index | ||
| - `doc.find_index("repos", where={"repo": "missing"})` → `None` | ||
| - Multi-key where: `where={"repo": url, "rev": "v1"}` matches only when both match | ||
| - First match wins when multiple items match | ||
| - Non-dict items in list are skipped | ||
| - Path not found → `QueryError` | ||
| - Value is a scalar → `NodeTypeError` | ||
| - Value is a dict → `NodeTypeError` | ||
| - Empty `where={}` → `ValueError` | ||
| - Nested path: `doc.find_index("ci", "steps", where={"uses": "..."})` | ||
| - Integer key in path prefix works: `doc.find_index("jobs", 0, "steps", where={...})` | ||
| - Editor.find_index mirrors Document behavior | ||
|
|
||
| ## Scope Boundaries | ||
|
|
||
| **In scope:** | ||
| - `Document.find_index()` method | ||
| - `Editor.find_index()` method | ||
|
|
||
| **Out of scope:** | ||
| - `match=` callable predicate (future addition, additive) | ||
| - `find_value()` or `find()` returning the item itself (use `doc["repos", idx]`) | ||
| - `find_all_indices()` returning multiple matches | ||
| - Rust-side implementation (pure Python is sufficient; values are already parsed) |
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.