Skip to content

Commit cd3f3d7

Browse files
committed
refactor(client): validate direction/max_depth before proto import in traverse()
Pure string/int validation now fires before the deferred proto import. Callers get ValueError for invalid inputs even when proto stubs have not been generated, keeping validation independent of the build step. Addresses review thread #28.
1 parent f97a0fe commit cd3f3d7

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

coordinode/coordinode/client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,15 @@ async def traverse(
383383
Returns:
384384
:class:`TraverseResult` with ``nodes`` and ``edges`` lists.
385385
"""
386+
# Validate pure string/int inputs before importing proto stubs — ensures ValueError
387+
# is raised even when proto stubs have not been generated yet.
388+
_valid_directions = {"outbound", "inbound", "both"}
389+
key = direction.lower()
390+
if key not in _valid_directions:
391+
raise ValueError(f"Invalid direction {direction!r}. Must be one of: 'outbound', 'inbound', 'both'.")
392+
if max_depth < 1:
393+
raise ValueError(f"max_depth must be >= 1, got {max_depth!r}.")
394+
386395
from coordinode._proto.coordinode.v1.graph.graph_pb2 import ( # type: ignore[import]
387396
TraversalDirection,
388397
TraverseRequest,
@@ -393,11 +402,6 @@ async def traverse(
393402
"inbound": TraversalDirection.TRAVERSAL_DIRECTION_INBOUND,
394403
"both": TraversalDirection.TRAVERSAL_DIRECTION_BOTH,
395404
}
396-
key = direction.lower()
397-
if key not in _direction_map:
398-
raise ValueError(f"Invalid direction {direction!r}. Must be one of: 'outbound', 'inbound', 'both'.")
399-
if max_depth < 1:
400-
raise ValueError(f"max_depth must be >= 1, got {max_depth!r}.")
401405
direction_value = _direction_map[key]
402406

403407
req = TraverseRequest(

0 commit comments

Comments
 (0)