From 577bf5205a614fdc6f6db1819e53acdca0d39be7 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Mon, 23 Oct 2023 15:26:41 +0100 Subject: [PATCH] Rename Context channel --- README.md | 2 +- examples/recursive-web-loader.py | 2 +- permchain/channels/__init__.py | 4 ++-- permchain/channels/context.py | 8 ++++---- tests/test_channels.py | 6 +++--- tests/test_pregel.py | 2 +- tests/test_pregel_async.py | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e8c919d12..e7cc038c1 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Channels are used to communicate between chains. Each channel has a value type, - `Channels.Archive`: stores a persistent sequence of values sent to the channel, useful for accumulating data over multiple steps - `Channels.UniqueArchive`: same as Archive, but deduplicates values sent to the channel - `Channels.BinaryOperatorAggregate`: stores a persistent value, updated by applying a binary operator to the current value and each update sent to the channel, useful for computing aggregates over multiple steps. eg. `total = Channels.BinaryOperatorAggregate(int, operator.add)` -- `Channels.ContextManager`: exposes the value of a context manager, managing its lifecycle. Useful for accessing external resources that require setup and/or teardown. eg. `client = Channels.ContextManager(httpx.Client)` +- `Channels.Context`: exposes the value of a context manager, managing its lifecycle. Useful for accessing external resources that require setup and/or teardown. eg. `client = Channels.Context(httpx.Client)` ### Chains diff --git a/examples/recursive-web-loader.py b/examples/recursive-web-loader.py index a29e66e84..c16881380 100644 --- a/examples/recursive-web-loader.py +++ b/examples/recursive-web-loader.py @@ -88,7 +88,7 @@ def recursive_web_loader( "next_urls": Channels.UniqueInbox(str), "documents": Channels.Archive(Document), "visited": Channels.UniqueArchive(str), - "client": Channels.ContextManager(httpx_client, httpx_aclient), + "client": Channels.Context(httpx_client, httpx_aclient), } # the main chain that gets executed recursively visitor = ( diff --git a/permchain/channels/__init__.py b/permchain/channels/__init__.py index 4574034d5..90d415010 100644 --- a/permchain/channels/__init__.py +++ b/permchain/channels/__init__.py @@ -1,6 +1,6 @@ from permchain.channels.archive import Archive, UniqueArchive from permchain.channels.binop import BinaryOperatorAggregate -from permchain.channels.context import ContextManager +from permchain.channels.context import Context from permchain.channels.inbox import Inbox, UniqueInbox from permchain.channels.last_value import LastValue @@ -11,5 +11,5 @@ __all__ = [ "Archive", "UniqueArchive", "BinaryOperatorAggregate", - "ContextManager", + "Context", ] diff --git a/permchain/channels/context.py b/permchain/channels/context.py index a95f2eff7..99d10c19e 100644 --- a/permchain/channels/context.py +++ b/permchain/channels/context.py @@ -4,13 +4,13 @@ from typing import ( AsyncContextManager, AsyncGenerator, Callable, + ContextManager, Generator, Generic, Optional, Sequence, Type, ) -from typing import ContextManager as ContextManagerType from typing_extensions import Self @@ -22,7 +22,7 @@ from permchain.channels.base import ( ) -class ContextManager(Generic[Value], BaseChannel[Value, None]): +class Context(Generic[Value], BaseChannel[Value, None]): """Exposes the value of a context manager, for the duration of an invocation. Context manager is entered before the first step, and exited after the last step. Optionally, provide an equivalent async context manager, which will be used @@ -31,7 +31,7 @@ class ContextManager(Generic[Value], BaseChannel[Value, None]): ```python import httpx - client = Channels.ContextManager(httpx.Client, httpx.AsyncClient) + client = Channels.Context(httpx.Client, httpx.AsyncClient) ``` """ @@ -39,7 +39,7 @@ class ContextManager(Generic[Value], BaseChannel[Value, None]): def __init__( self, - ctx: Optional[Callable[[], ContextManagerType[Value]]] = None, + ctx: Optional[Callable[[], ContextManager[Value]]] = None, actx: Optional[Callable[[], AsyncContextManager[Value]]] = None, typ: Optional[Type[Value]] = None, ) -> None: diff --git a/tests/test_channels.py b/tests/test_channels.py index 640520fde..4b45a67c6 100644 --- a/tests/test_channels.py +++ b/tests/test_channels.py @@ -135,7 +135,7 @@ def test_ctx_manager(mocker: MockerFixture) -> None: finally: cleanup() - with Channels.ContextManager(an_int, None, int).empty() as channel: + with Channels.Context(an_int, None, int).empty() as channel: assert setup.call_count == 1 assert cleanup.call_count == 0 @@ -153,7 +153,7 @@ def test_ctx_manager(mocker: MockerFixture) -> None: def test_ctx_manager_ctx(mocker: MockerFixture) -> None: - with Channels.ContextManager(httpx.Client).empty() as channel: + with Channels.Context(httpx.Client).empty() as channel: assert channel.ValueType is httpx.Client with pytest.raises(InvalidUpdateError): assert channel.UpdateType is None @@ -183,7 +183,7 @@ async def test_ctx_manager_async(mocker: MockerFixture) -> None: finally: cleanup() - async with Channels.ContextManager(an_int_sync, an_int, int).aempty() as channel: + async with Channels.Context(an_int_sync, an_int, int).aempty() as channel: assert setup.call_count == 1 assert cleanup.call_count == 0 diff --git a/tests/test_pregel.py b/tests/test_pregel.py index a36b12c2c..6f544c671 100644 --- a/tests/test_pregel.py +++ b/tests/test_pregel.py @@ -426,7 +426,7 @@ def test_channel_enter_exit_timing(mocker: MockerFixture) -> None: "input": Channels.LastValue(int), "output": Channels.LastValue(int), "inbox": Channels.Inbox(int), - "ctx": Channels.ContextManager(an_int, typ=int), + "ctx": Channels.Context(an_int, typ=int), }, input="input", output=["inbox", "output"], diff --git a/tests/test_pregel_async.py b/tests/test_pregel_async.py index fec8f7892..99487adfd 100644 --- a/tests/test_pregel_async.py +++ b/tests/test_pregel_async.py @@ -427,7 +427,7 @@ async def test_channel_enter_exit_timing(mocker: MockerFixture) -> None: "input": Channels.LastValue(int), "output": Channels.LastValue(int), "inbox": Channels.Inbox(int), - "ctx": Channels.ContextManager(an_int, an_int_async, typ=int), + "ctx": Channels.Context(an_int, an_int_async, typ=int), }, input="input", output=["inbox", "output"],