`collect_links()` imports private `_walk_links` from `block` module
- Dominant language
- Python
- Stars
- 13
- Forks
- 9
- PR merge metrics
- No merged PRs in 30d
Description
`utils.py` imports `_walk_links` (a private function) from `block.py`. This cross-module use of a private function is fragile and should be refactored.
### Problem
In `dag/utils.py`:
```python
def collect_links(value: Any) -> list[tuple[str, CID]]:
from .block import _walk_links # ← Imports private function
return list(_walk_links(value, ""))
```
In `dag/block.py`:
```python
def _walk_links(node: Any, prefix: str) -> Iterator[tuple[str, CID]]:
"""Recursively yield (path, cid) from an IPLD data-model value."""
# ...
```
The `_walk_links` function is used by both `Block.links()` and `collect_links()`, but it's marked as private with the `_` prefix.
### Proposed Solution
Move `_walk_links` to `utils.py` and make it public:
```python
# In dag/utils.py:
def walk_links(node: Any, prefix: str = "") -> Iterator[tuple[str, CID]]:
"""Recursively yield (path, cid) from an IPLD data-model value."""
if is_cid(node):
yield (prefix, node)
elif isinstance(node, dict):
for k, v in node.items():
child = f"{prefix}/{k}" if prefix else k
yield from walk_links(v, child)
elif isinstance(node, list):
for i, v in enumerate(node):
child = f"{prefix}/{i}" if prefix else str(i)
yield from walk_links(v, child)
def collect_links(value: Any) -> list[tuple[str, CID]]:
return list(walk_links(value, ""))
```
Update `block.py` to import from `utils.py`:
```python
from .utils import walk_links as _walk_links
```
Export `walk_links` from `__init__.py`.
### Related
- Files: `dag/utils.py`, `dag/block.py`
Contributor guide
Research direction
Read the existing _walk_links implementation and collect_links in dag/utils.py, then inspect Block.links() and the import in dag/block.py. Check dag/__init__.py exports and the related call sites; done means walk_links is public, the private cross-module import is removed, and link collection behavior remains unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- distributed-systems
- Issue type
- Refactor
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 82/100