ipld / ipld/py-ipld-dag

`Block.get()` raises unhelpful `KeyError` when path traverses through a CID link

Open Beginner friendly
#27 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
13
Forks
9
PR merge metrics
No merged PRs in 30d

Description

When `Block.get()` encounters a CID (link) while traversing a path, it tries to use the CID as a dict/list and raises a generic `KeyError`. It should either stop at the link boundary and return the CID, or raise a more informative error indicating that the path crosses a block boundary.

### Problem

In `dag/block.py`:

```python
def get(self, path: str) -> Any:
segments = [s for s in path.split("/") if s]
current: Any = self._value
for seg in segments:
if isinstance(current, dict):
current = current[seg]
elif isinstance(current, list):
current = current[int(seg)]
else:
raise KeyError(f"Cannot traverse into {type(current).__name__} with {seg!r}")
return current
```

When a value is a CID:

```python
block = Block.encode(
value={"link": some_cid, "data": "hello"},
codec=dag_cbor.codec,
)

# This raises: KeyError: "Cannot traverse into CIDv1 with 'nested'"
block.get("link/nested")
```

The error message doesn't explain that the path crosses a block boundary and that the caller needs to load the linked block.

### Proposed Solution

Add explicit CID handling:

```python
def get(self, path: str) -> Any:
segments = [s for s in path.split("/") if s]
current: Any = self._value
for i, seg in enumerate(segments):
if is_cid(current):
remaining = "/".join(segments[i:])
raise LinkBoundaryError(
f"Path crosses block boundary at CID {current}. "
f"Remaining path: {remaining!r}. "
f"Load the linked block to continue traversal."
)
if isinstance(current, dict):
current = current[seg]
elif isinstance(current, list):
current = current[int(seg)]
else:
raise KeyError(f"Cannot traverse into {type(current).__name__} with {seg!r}")
return current
```

Add a new exception class:

```python
class LinkBoundaryError(Exception):
"""Raised when path traversal crosses a block boundary (CID link)."""
pass
```

### Related

- File: `dag/block.py`

Contributor guide

Open the contributing guide

Research direction

Read dag/block.py, starting with Block.get and the existing CID representation or detection used by the project. Confirm the expected boundary behavior, add coverage for traversal through a CID, and run the relevant Block tests; done means callers receive a clear block-boundary error instead of the generic KeyError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
distributed-systems
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.