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:
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:
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:
# 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:
from .utils import walk_links as _walk_links
Export walk_links from __init__.py.
Related
- Files:
dag/utils.py, dag/block.py
utils.pyimports_walk_links(a private function) fromblock.py. This cross-module use of a private function is fragile and should be refactored.Problem
In
dag/utils.py:In
dag/block.py:The
_walk_linksfunction is used by bothBlock.links()andcollect_links(), but it's marked as private with the_prefix.Proposed Solution
Move
_walk_linkstoutils.pyand make it public:Update
block.pyto import fromutils.py:Export
walk_linksfrom__init__.py.Related
dag/utils.py,dag/block.py