Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b4e2bda
state persistence
samuelcolvin Feb 19, 2025
4c1d50d
fixing tests
samuelcolvin Feb 23, 2025
b737c97
tests all passing
samuelcolvin Feb 24, 2025
1c46cf9
simplify
samuelcolvin Feb 26, 2025
3889e53
Merge branch 'main' into state-persistence
samuelcolvin Mar 3, 2025
456560d
fixing tests
samuelcolvin Mar 3, 2025
767e08d
fix tests for 3.9 etc
samuelcolvin Mar 3, 2025
daffea5
refactoring state persistence
samuelcolvin Mar 6, 2025
4562244
snapshot id on node
samuelcolvin Mar 7, 2025
2bdb029
fixing snapshot id
samuelcolvin Mar 8, 2025
e9d8052
improving docs
samuelcolvin Mar 8, 2025
576d4af
Merge branch 'main' into state-persistence
samuelcolvin Mar 8, 2025
09a5174
fix spans
samuelcolvin Mar 8, 2025
e271af5
more tests, improve coverage
samuelcolvin Mar 8, 2025
3d93cb0
add file persistence
samuelcolvin Mar 9, 2025
9581979
improve coverage
samuelcolvin Mar 9, 2025
88723e3
fix for 3.9 and 3.10
samuelcolvin Mar 9, 2025
2a9f90e
improve coverage
samuelcolvin Mar 9, 2025
294cbd2
more docs
samuelcolvin Mar 10, 2025
f1e4ca1
complete docs
samuelcolvin Mar 10, 2025
1edbb06
Merge branch 'main' into state-persistence
samuelcolvin Mar 10, 2025
0ad49fd
replace human in the loop example
samuelcolvin Mar 10, 2025
27aee1a
Apply suggestions from code review
samuelcolvin Mar 12, 2025
76cae5a
Apply suggestions from code review
samuelcolvin Mar 12, 2025
f51699e
tweak docs as suggested
samuelcolvin Mar 12, 2025
1cccf75
add iter_from_persistence, deprecate next()
samuelcolvin Mar 12, 2025
2186e47
improve documentation
samuelcolvin Mar 13, 2025
894a590
fix tests
samuelcolvin Mar 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more tests, improve coverage
  • Loading branch information
samuelcolvin committed Mar 8, 2025
commit e271af50b01cfc6dfde795c755d1fe9fa34a0bfd
17 changes: 2 additions & 15 deletions pydantic_graph/pydantic_graph/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import asyncio
import types
from functools import partial
from typing import Any, Callable, TypeVar
from typing import Any, TypeVar

from typing_extensions import ParamSpec, TypeIs, get_args, get_origin
from typing_extensions import TypeIs, get_args, get_origin
from typing_inspection import typing_objects
from typing_inspection.introspection import is_union_origin

Expand Down Expand Up @@ -88,15 +87,3 @@ class Unset:

def is_set(t_or_unset: T | Unset) -> TypeIs[T]:
return t_or_unset is not UNSET


_P = ParamSpec('_P')
_R = TypeVar('_R')


async def run_in_executor(func: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R:
if kwargs:
# noinspection PyTypeChecker
return await asyncio.get_running_loop().run_in_executor(None, partial(func, *args, **kwargs))
else:
return await asyncio.get_running_loop().run_in_executor(None, func, *args) # type: ignore
7 changes: 4 additions & 3 deletions pydantic_graph/pydantic_graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ async def iter(
A GraphRun that can be async iterated over to drive the graph to completion.
"""
if infer_name and self.name is None:
self._infer_name(inspect.currentframe())
# f_back because `asynccontextmanager` adds one frame
if frame := inspect.currentframe(): # pragma: no branch
self._infer_name(frame.f_back)

if persistence is None:
persistence = SimpleStatePersistence()
Expand Down Expand Up @@ -725,8 +727,7 @@ async def __anext__(self) -> BaseNode[StateT, DepsT, RunEndT] | End[RunEndT]:
return await self.next(self._next_node)

def __repr__(self) -> str:
step = -1 # TODO
return f'<GraphRun name={self.graph.name or "<unnamed>"} step={step}>'
return f'<GraphRun graph={self.graph.name or "[unnamed]"}>'


@dataclass
Expand Down
16 changes: 16 additions & 0 deletions tests/graph/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,22 @@ async def run(self, ctx: GraphRunContext) -> End[None]:
assert exc_info.value.message == snapshot('Invalid node return type: `int`. Expected `BaseNode` or `End`.')


async def test_iter():
my_graph = Graph(nodes=(Float2String, String2Length, Double))
assert my_graph.name is None
assert my_graph._inferred_types == (type(None), int)
node_reprs: list[str] = []
async with my_graph.iter(Float2String(3.14)) as graph_iter:
assert repr(graph_iter) == snapshot('<GraphRun graph=my_graph>')
async for node in graph_iter:
node_reprs.append(repr(node))
# len('3.14') * 2 == 8
assert graph_iter.result
assert graph_iter.result.output == 8

assert node_reprs == snapshot(["String2Length(input_data='3.14')", 'Double(input_data=4)', 'End(data=8)'])


async def test_next(mock_snapshot_id: object):
@dataclass
class Foo(BaseNode):
Expand Down
4 changes: 2 additions & 2 deletions tests/graph/test_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test_one_node(mock_snapshot_id: object):
class MyNode(BaseNode[None, None, int]):
node_field: int

async def run(self, ctx: GraphRunContext) -> End[int]:
async def run(self, ctx: GraphRunContext) -> End[int]: # pragma: no cover
return End(123)

g = Graph(nodes=[MyNode])
Expand Down Expand Up @@ -196,7 +196,7 @@ async def run(self, ctx: GraphRunContext) -> End[int]:
def test_no_generic_arg(mock_snapshot_id: object):
@dataclass
class NoGenericArgsNode(BaseNode):
async def run(self, ctx: GraphRunContext) -> NoGenericArgsNode:
async def run(self, ctx: GraphRunContext) -> NoGenericArgsNode: # pragma: no cover
return NoGenericArgsNode()

g = Graph(nodes=[NoGenericArgsNode])
Expand Down
Loading