Skip to content

Commit 9651184

Browse files
committed
Rename spread to map
1 parent 519c5d6 commit 9651184

File tree

6 files changed

+56
-56
lines changed

6 files changed

+56
-56
lines changed

pydantic_graph/pydantic_graph/beta/decision.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class DecisionBranch(Generic[SourceT]):
112112
path: Path
113113
"""The execution path to follow when an input value matches this branch of a decision node.
114114
115-
This can include transforming, spreading, and broadcasting the output before sending to the next node or nodes.
115+
This can include transforming, mapping, and broadcasting the output before sending to the next node or nodes.
116116
117117
The path can also include position-aware labels which are used when generating mermaid diagrams."""
118118

@@ -211,7 +211,7 @@ def transform(
211211
path_builder=self.path_builder.transform(func),
212212
)
213213

214-
def spread(
214+
def map(
215215
self: DecisionBranchBuilder[StateT, DepsT, Iterable[T], SourceT, HandledT],
216216
*,
217217
fork_id: ForkID | None = None,
@@ -224,16 +224,16 @@ def spread(
224224
225225
Args:
226226
fork_id: Optional ID for the fork, defaults to a generated value
227-
downstream_join_id: Optional ID of a downstream join node which is involved when spreading empty iterables
227+
downstream_join_id: Optional ID of a downstream join node which is involved when mapping empty iterables
228228
229229
Returns:
230-
A new DecisionBranchBuilder where spreading is performed prior to generating the final output.
230+
A new DecisionBranchBuilder where mapping is performed prior to generating the final output.
231231
"""
232232
return DecisionBranchBuilder(
233233
decision=self.decision,
234234
source=self.source,
235235
matches=self.matches,
236-
path_builder=self.path_builder.spread(fork_id=fork_id, downstream_join_id=downstream_join_id),
236+
path_builder=self.path_builder.map(fork_id=fork_id, downstream_join_id=downstream_join_id),
237237
)
238238

239239
def label(self, label: str) -> DecisionBranchBuilder[StateT, DepsT, OutputT, SourceT, HandledT]:

pydantic_graph/pydantic_graph/beta/graph.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,23 +659,23 @@ def _handle_path(self, path: Path, inputs: Any, fork_stack: ForkStack) -> Sequen
659659
try:
660660
iter(inputs)
661661
except TypeError:
662-
raise RuntimeError(f'Cannot spread non-iterable value: {inputs!r}')
662+
raise RuntimeError(f'Cannot map non-iterable value: {inputs!r}')
663663

664664
node_run_id = NodeRunID(str(uuid.uuid4()))
665665

666-
# If the spread specifies a downstream join id, eagerly create a reducer for it
666+
# If the map specifies a downstream join id, eagerly create a reducer for it
667667
if item.downstream_join_id is not None:
668668
join_node = self.graph.nodes[item.downstream_join_id]
669669
assert isinstance(join_node, Join)
670670
self._active_reducers[(item.downstream_join_id, node_run_id)] = join_node.create_reducer(), fork_stack
671671

672-
spread_tasks: list[GraphTask] = []
672+
map_tasks: list[GraphTask] = []
673673
for thread_index, input_item in enumerate(inputs):
674674
item_tasks = self._handle_path(
675675
path.next_path, input_item, fork_stack + (ForkStackItem(item.fork_id, node_run_id, thread_index),)
676676
)
677-
spread_tasks += item_tasks
678-
return spread_tasks
677+
map_tasks += item_tasks
678+
return map_tasks
679679
elif isinstance(item, BroadcastMarker):
680680
return [GraphTask(item.fork_id, inputs, fork_stack)]
681681
elif isinstance(item, TransformMarker):

pydantic_graph/pydantic_graph/beta/graph_builder.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def add(self, *edges: EdgePath[StateT, DepsT]) -> None:
344344
"""Add one or more edge paths to the graph.
345345
346346
This method processes edge paths and automatically creates any necessary
347-
fork nodes for broadcasts and spreads.
347+
fork nodes for broadcasts and maps.
348348
349349
Args:
350350
*edges: The edge paths to add to the graph
@@ -358,12 +358,12 @@ def _handle_path(p: Path):
358358
"""
359359
for item in p.items:
360360
if isinstance(item, BroadcastMarker):
361-
new_node = Fork[Any, Any](id=item.fork_id, is_spread=False)
361+
new_node = Fork[Any, Any](id=item.fork_id, is_map=False)
362362
self._insert_node(new_node)
363363
for path in item.paths:
364364
_handle_path(Path(items=[*path.items]))
365365
elif isinstance(item, SpreadMarker):
366-
new_node = Fork[Any, Any](id=item.fork_id, is_spread=True)
366+
new_node = Fork[Any, Any](id=item.fork_id, is_map=True)
367367
self._insert_node(new_node)
368368
elif isinstance(item, DestinationMarker):
369369
pass
@@ -407,34 +407,34 @@ def add_edge(self, source: Source[T], destination: Destination[T], *, label: str
407407
builder = builder.label(label)
408408
self.add(builder.to(destination))
409409

410-
def add_spreading_edge(
410+
def add_mapping_edge(
411411
self,
412412
source: Source[Iterable[T]],
413-
spread_to: Destination[T],
413+
map_to: Destination[T],
414414
*,
415-
pre_spread_label: str | None = None,
416-
post_spread_label: str | None = None,
415+
pre_map_label: str | None = None,
416+
post_map_label: str | None = None,
417417
fork_id: ForkID | None = None,
418418
downstream_join_id: JoinID | None = None,
419419
) -> None:
420-
"""Add an edge that spreads iterable data across parallel paths.
420+
"""Add an edge that maps iterable data across parallel paths.
421421
422422
Args:
423423
source: The source node that produces iterable data
424-
spread_to: The destination node that receives individual items
425-
pre_spread_label: Optional label before the spread operation
426-
post_spread_label: Optional label after the spread operation
427-
fork_id: Optional ID for the fork node produced for this spread operation
428-
downstream_join_id: Optional ID of a join node that will always be downstream of this spread.
429-
Specifying this ensures correct handling if you try to spread an empty iterable.
424+
map_to: The destination node that receives individual items
425+
pre_map_label: Optional label before the map operation
426+
post_map_label: Optional label after the map operation
427+
fork_id: Optional ID for the fork node produced for this map operation
428+
downstream_join_id: Optional ID of a join node that will always be downstream of this map.
429+
Specifying this ensures correct handling if you try to map an empty iterable.
430430
"""
431431
builder = self.edge_from(source)
432-
if pre_spread_label is not None:
433-
builder = builder.label(pre_spread_label)
434-
builder = builder.spread(fork_id=fork_id, downstream_join_id=downstream_join_id)
435-
if post_spread_label is not None:
436-
builder = builder.label(post_spread_label)
437-
self.add(builder.to(spread_to))
432+
if pre_map_label is not None:
433+
builder = builder.label(pre_map_label)
434+
builder = builder.map(fork_id=fork_id, downstream_join_id=downstream_join_id)
435+
if post_map_label is not None:
436+
builder = builder.label(post_map_label)
437+
self.add(builder.to(map_to))
438438

439439
# TODO(P2): Support adding subgraphs ... not sure exactly what that looks like yet..
440440
# probably similar to a step, but with some tweaks
@@ -590,17 +590,17 @@ def _get_new_broadcast_id(self, from_: str | None = None) -> str:
590590
index += 1
591591
return node_id
592592

593-
def _get_new_spread_id(self, from_: str | None = None, to: str | None = None) -> str:
594-
"""Generate a unique ID for a new spread fork.
593+
def _get_new_map_id(self, from_: str | None = None, to: str | None = None) -> str:
594+
"""Generate a unique ID for a new map fork.
595595
596596
Args:
597597
from_: Optional source identifier to include in the ID
598598
to: Optional destination identifier to include in the ID
599599
600600
Returns:
601-
A unique spread fork ID
601+
A unique map fork ID
602602
"""
603-
prefix = 'spread'
603+
prefix = 'map'
604604
if from_ is not None:
605605
prefix += f'_from_{from_}'
606606
if to is not None:
@@ -744,13 +744,13 @@ def _normalize_forks(
744744
paths_to_handle.extend(edges_from_source)
745745

746746
node = nodes[source_id]
747-
if isinstance(node, Fork) and not node.is_spread:
747+
if isinstance(node, Fork) and not node.is_map:
748748
new_edges[source_id] = edges_from_source
749749
continue # broadcast fork; nothing to do
750750
if len(edges_from_source) == 1:
751751
new_edges[source_id] = edges_from_source
752752
continue
753-
new_fork = Fork[Any, Any](id=ForkID(NodeID(f'{node.id}_broadcast_fork')), is_spread=False)
753+
new_fork = Fork[Any, Any](id=ForkID(NodeID(f'{node.id}_broadcast_fork')), is_map=False)
754754
new_nodes[new_fork.id] = new_fork
755755
new_edges[source_id] = [Path(items=[BroadcastMarker(fork_id=new_fork.id, paths=edges_from_source)])]
756756
new_edges[new_fork.id] = edges_from_source
@@ -764,7 +764,7 @@ def _normalize_forks(
764764
if isinstance(item, BroadcastMarker):
765765
assert item.fork_id in new_nodes
766766
# if item.fork_id not in new_nodes:
767-
# new_nodes[new_fork.id] = Fork[Any, Any](id=item.fork_id, is_spread=False)
767+
# new_nodes[new_fork.id] = Fork[Any, Any](id=item.fork_id, is_map=False)
768768
new_edges[item.fork_id] = [*item.paths]
769769
paths_to_handle.extend(item.paths)
770770

pydantic_graph/pydantic_graph/beta/mermaid.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
- `'BT'`: Bottom to top
2828
"""
2929

30-
NodeKind = Literal['broadcast', 'spread', 'join', 'start', 'end', 'step', 'decision', 'base_node']
30+
NodeKind = Literal['broadcast', 'map', 'join', 'start', 'end', 'step', 'decision', 'base_node']
3131

3232

3333
@dataclass
@@ -59,7 +59,7 @@ def _collect_edges(path: Path, last_source_id: NodeID) -> None:
5959
for item in path.items:
6060
if isinstance(item, SpreadMarker):
6161
edges_by_source[last_source_id].append(MermaidEdge(last_source_id, item.fork_id, working_label))
62-
return # spread markers correspond to nodes already in the graph; downstream gets handled separately
62+
return # map markers correspond to nodes already in the graph; downstream gets handled separately
6363
elif isinstance(item, BroadcastMarker):
6464
edges_by_source[last_source_id].append(MermaidEdge(last_source_id, item.fork_id, working_label))
6565
return # broadcast markers correspond to nodes already in the graph; downstream gets handled separately
@@ -82,7 +82,7 @@ def _collect_edges(path: Path, last_source_id: NodeID) -> None:
8282
elif isinstance(node, Join):
8383
kind = 'join'
8484
elif isinstance(node, Fork):
85-
kind = 'spread' if node.is_spread else 'broadcast'
85+
kind = 'map' if node.is_map else 'broadcast'
8686
elif isinstance(node, Decision):
8787
kind = 'decision'
8888
note = node.note
@@ -143,7 +143,7 @@ def render(
143143
node_lines.append(line)
144144
elif node.kind == 'join':
145145
node_lines = [f' state {node.id} <<join>>']
146-
elif node.kind == 'broadcast' or node.kind == 'spread':
146+
elif node.kind == 'broadcast' or node.kind == 'map':
147147
node_lines = [f' state {node.id} <<fork>>']
148148
elif node.kind == 'decision':
149149
node_lines = [f' state {node.id} <<choice>>']

pydantic_graph/pydantic_graph/beta/node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ class Fork(Generic[InputT, OutputT]):
6666
"""Fork node that creates parallel execution branches.
6767
6868
A Fork node splits the execution flow into multiple parallel branches,
69-
enabling concurrent execution of downstream nodes. It can either spread
69+
enabling concurrent execution of downstream nodes. It can either map
7070
a sequence across multiple branches or duplicate data to each branch.
7171
"""
7272

7373
id: ForkID
7474
"""Unique identifier for this fork node."""
7575

76-
is_spread: bool
76+
is_map: bool
7777
"""Determines fork behavior.
7878
7979
If True, InputT must be Sequence[OutputT] and each element is sent to a separate branch.

pydantic_graph/pydantic_graph/beta/paths.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Path and edge definition for graph navigation.
22
33
This module provides the building blocks for defining paths through a graph,
4-
including transformations, spreads, broadcasts, and routing to destinations.
4+
including transformations, maps, broadcasts, and routing to destinations.
55
Paths enable complex data flow patterns in graph execution.
66
"""
77

@@ -41,16 +41,16 @@ class TransformMarker:
4141

4242
@dataclass
4343
class SpreadMarker:
44-
"""A marker indicating that iterable data should be spread across parallel paths.
44+
"""A marker indicating that iterable data should be map across parallel paths.
4545
4646
Spread markers take iterable input and create parallel execution paths
4747
for each item in the iterable.
4848
"""
4949

5050
fork_id: ForkID
51-
"""Unique identifier for the fork created by this spread operation."""
51+
"""Unique identifier for the fork created by this map operation."""
5252
downstream_join_id: JoinID | None
53-
"""Optional identifier of a downstream join node that should be jumped to if spreading an empty iterable."""
53+
"""Optional identifier of a downstream join node that should be jumped to if mapping an empty iterable."""
5454

5555

5656
@dataclass
@@ -109,7 +109,7 @@ class Path:
109109

110110
@property
111111
def last_fork(self) -> BroadcastMarker | SpreadMarker | None:
112-
"""Get the most recent fork or spread marker in this path.
112+
"""Get the most recent fork or map marker in this path.
113113
114114
Returns:
115115
The last BroadcastMarker or SpreadMarker in the path, or None if no forks exist
@@ -134,7 +134,7 @@ class PathBuilder(Generic[StateT, DepsT, OutputT]):
134134
"""A builder for constructing paths with method chaining.
135135
136136
PathBuilder provides a fluent interface for creating paths by chaining
137-
operations like transforms, spreads, and routing to destinations.
137+
operations like transforms, maps, and routing to destinations.
138138
139139
Type Parameters:
140140
StateT: The type of the graph state
@@ -147,7 +147,7 @@ class PathBuilder(Generic[StateT, DepsT, OutputT]):
147147

148148
@property
149149
def last_fork(self) -> BroadcastMarker | SpreadMarker | None:
150-
"""Get the most recent fork or spread marker in the working path.
150+
"""Get the most recent fork or map marker in the working path.
151151
152152
Returns:
153153
The last BroadcastMarker or SpreadMarker in the working items, or None if no forks exist
@@ -208,7 +208,7 @@ def transform(self, func: StepFunction[StateT, DepsT, OutputT, Any], /) -> PathB
208208
next_item = TransformMarker(func)
209209
return PathBuilder[StateT, DepsT, Any](working_items=[*self.working_items, next_item])
210210

211-
def spread(
211+
def map(
212212
self: PathBuilder[StateT, DepsT, Iterable[Any]],
213213
*,
214214
fork_id: ForkID | None = None,
@@ -221,13 +221,13 @@ def spread(
221221
222222
Args:
223223
fork_id: Optional ID for the fork, defaults to a generated value
224-
downstream_join_id: Optional ID of a downstream join node which is involved when spreading empty iterables
224+
downstream_join_id: Optional ID of a downstream join node which is involved when mapping empty iterables
225225
226226
Returns:
227227
A new PathBuilder that operates on individual items from the iterable
228228
"""
229229
next_item = SpreadMarker(
230-
fork_id=NodeID(fork_id or 'spread_' + secrets.token_hex(8)), downstream_join_id=downstream_join_id
230+
fork_id=NodeID(fork_id or 'map_' + secrets.token_hex(8)), downstream_join_id=downstream_join_id
231231
)
232232
return PathBuilder[StateT, DepsT, Any](working_items=[*self.working_items, next_item])
233233

@@ -365,7 +365,7 @@ def to(
365365
destinations=destinations,
366366
)
367367

368-
def spread(
368+
def map(
369369
self: EdgePathBuilder[StateT, DepsT, Iterable[Any]],
370370
*,
371371
fork_id: ForkID | None = None,
@@ -375,14 +375,14 @@ def spread(
375375
376376
Args:
377377
fork_id: Optional ID for the fork, defaults to a generated value
378-
downstream_join_id: Optional ID of a downstream join node which is involved when spreading empty iterables
378+
downstream_join_id: Optional ID of a downstream join node which is involved when mapping empty iterables
379379
380380
Returns:
381381
A new EdgePathBuilder that operates on individual items from the iterable
382382
"""
383383
return EdgePathBuilder(
384384
sources=self.sources,
385-
path_builder=self.path_builder.spread(fork_id=fork_id, downstream_join_id=downstream_join_id),
385+
path_builder=self.path_builder.map(fork_id=fork_id, downstream_join_id=downstream_join_id),
386386
)
387387

388388
def transform(self, func: StepFunction[StateT, DepsT, OutputT, Any], /) -> EdgePathBuilder[StateT, DepsT, Any]:

0 commit comments

Comments
 (0)