Skip to content

Commit 239143f

Browse files
committed
fix docstrings
1 parent 9c0d0db commit 239143f

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

packages/toolbox-core/src/toolbox_core/sync_client.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626

2727
class ToolboxSyncClient:
2828
"""
29-
A synchronous client for interacting with a Toolbox service.
29+
An synchronous client for interacting with a Toolbox service.
3030
3131
Provides methods to discover and load tools defined by a remote Toolbox
32-
service endpoint, returning synchronous tool wrappers (`ToolboxSyncTool`).
33-
It manages an underlying asynchronous `ToolboxClient`.
32+
service endpoint.
3433
"""
3534

3635
__session: Optional[ClientSession] = None
@@ -42,7 +41,7 @@ def __init__(
4241
url: str,
4342
):
4443
"""
45-
Initializes the ToolboxSyncClient.
44+
Initializes the ToolboxClient.
4645
4746
Args:
4847
url: The base URL for the Toolbox service API (e.g., "http://localhost:5000").
@@ -91,31 +90,45 @@ async def __run_as_async(self, coro: Awaitable[T]) -> T:
9190
asyncio.run_coroutine_threadsafe(coro, self.__loop)
9291
)
9392

93+
def close(self):
94+
"""
95+
Synchronously closes the underlying client session. Doing so will cause
96+
any tools created by this Client to cease to function.
97+
98+
If the session was provided externally during initialization, the caller
99+
is responsible for its lifecycle, but calling close here will still
100+
attempt to close it.
101+
"""
102+
coro = self.__session.close()
103+
self.__run_as_sync(coro)
104+
94105
def load_tool(
95106
self,
96-
tool_name: str,
107+
name: str,
97108
auth_token_getters: dict[str, Callable[[], str]] = {},
98109
bound_params: Mapping[str, Union[Callable[[], Any], Any]] = {},
99110
) -> ToolboxSyncTool:
100111
"""
101112
Synchronously loads a tool from the server.
102113
103-
Retrieves the schema for the specified tool and returns a callable,
104-
synchronous object (`ToolboxSyncTool`) that can be used to invoke the
114+
Retrieves the schema for the specified tool from the Toolbox server and
115+
returns a callable object (`ToolboxSyncTool`) that can be used to invoke the
105116
tool remotely.
106117
107118
Args:
108-
tool_name: Name of the tool to load.
119+
name: The unique name or identifier of the tool to load.
109120
auth_token_getters: A mapping of authentication service names to
110121
callables that return the corresponding authentication token.
111122
bound_params: A mapping of parameter names to bind to specific values or
112123
callables that are called to produce values as needed.
113124
114125
Returns:
115-
ToolboxSyncTool: A synchronous callable object representing the loaded tool.
126+
ToolboxSyncTool: A callable object representing the loaded tool, ready
127+
for execution. The specific arguments and behavior of the callable
128+
depend on the tool itself.
116129
"""
117130
async_tool = self.__run_as_sync(
118-
self.__async_client.load_tool(tool_name, auth_token_getters, bound_params)
131+
self.__async_client.load_tool(name, auth_token_getters, bound_params)
119132
)
120133

121134
if not self.__loop or not self.__thread:
@@ -124,27 +137,27 @@ def load_tool(
124137

125138
def load_toolset(
126139
self,
127-
toolset_name: str,
140+
name: str,
128141
auth_token_getters: dict[str, Callable[[], str]] = {},
129142
bound_params: Mapping[str, Union[Callable[[], Any], Any]] = {},
130143
) -> list[ToolboxSyncTool]:
131144
"""
132145
Synchronously fetches a toolset and loads all tools defined within it.
133146
134147
Args:
135-
toolset_name: Name of the toolset to load tools from.
148+
name: Name of the toolset to load tools.
136149
auth_token_getters: A mapping of authentication service names to
137150
callables that return the corresponding authentication token.
138151
bound_params: A mapping of parameter names to bind to specific values or
139152
callables that are called to produce values as needed.
140153
141154
Returns:
142-
list[ToolboxSyncTool]: A list of synchronous callables, one for each
143-
tool defined in the toolset.
155+
list[ToolboxSyncTool]: A list of callables, one for each tool defined
156+
in the toolset.
144157
"""
145158
async_tools = self.__run_as_sync(
146159
self.__async_client.load_toolset(
147-
toolset_name, auth_token_getters, bound_params
160+
name, auth_token_getters, bound_params
148161
)
149162
)
150163

@@ -155,12 +168,6 @@ def load_toolset(
155168
tools.append(ToolboxSyncTool(async_tool, self.__loop, self.__thread))
156169
return tools
157170

158-
def close(self):
159-
"""
160-
Synchronously closes the client session if it was created internally by the client.
161-
"""
162-
coro = self.__session.close()
163-
self.__run_as_sync(coro)
164171

165172
def __enter__(self):
166173
"""Enter the runtime context related to this client instance."""

packages/toolbox-core/src/toolbox_core/sync_tool.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,28 @@
2424

2525
class ToolboxSyncTool:
2626
"""
27-
A synchronous wrapper proxying asynchronous ToolboxTool instance.
27+
A callable proxy object representing a specific tool on a remote Toolbox server.
2828
29-
This class allows calling the underlying async tool synchronously.
30-
It also proxies methods like `add_auth_token_getters` and
31-
`bind_parameters` to ensure they return new instances of this synchronous
32-
wrapper.
29+
Instances of this class behave like synchronous functions. When called, they
30+
send a request to the corresponding tool's endpoint on the Toolbox server with
31+
the provided arguments.
32+
33+
It utilizes Python's introspection features (`__name__`, `__doc__`,
34+
`__signature__`, `__annotations__`) so that standard tools like `help()`
35+
and `inspect` work as expected.
3336
"""
3437

3538
def __init__(
3639
self, async_tool: ToolboxTool, loop: AbstractEventLoop, thread: Thread
3740
):
3841
"""
39-
Initializes the synchronous wrapper.
42+
Initializes a callable that will trigger the tool invocation through the
43+
Toolbox server.
4044
4145
Args:
4246
async_tool: An instance of the asynchronous ToolboxTool.
47+
loop: The event loop used to run asynchronous tasks.
48+
thread: The thread to run blocking operations in.
4349
"""
4450
if not isinstance(async_tool, ToolboxTool):
4551
raise TypeError("async_tool must be an instance of ToolboxTool")
@@ -77,21 +83,17 @@ async def __run_as_async(self, coro: Awaitable[T]) -> T:
7783

7884
def __call__(self, *args: Any, **kwargs: Any) -> str:
7985
"""
80-
Synchronously calls the underlying remote tool.
86+
Synchronously calls the remote tool with the provided arguments.
8187
82-
This method blocks until the tool call completes and returns
83-
the result.
88+
Validates arguments against the tool's signature, then sends them
89+
as a JSON payload in a POST request to the tool's invoke URL.
8490
8591
Args:
8692
*args: Positional arguments for the tool.
8793
**kwargs: Keyword arguments for the tool.
8894
8995
Returns:
9096
The string result returned by the remote tool execution.
91-
92-
Raises:
93-
Any exception raised by the underlying async tool's __call__ method
94-
or during asyncio execution.
9597
"""
9698
return self.__run_as_sync(self.__async_tool(**kwargs))
9799

@@ -100,14 +102,16 @@ def add_auth_token_getters(
100102
auth_token_getters: Mapping[str, Callable[[], str]],
101103
) -> "ToolboxSyncTool":
102104
"""
103-
Registers auth token getters and returns a new SyncToolboxTool instance.
105+
Registers an auth token getter function that is used for AuthServices when tools
106+
are invoked.
104107
105108
Args:
106109
auth_token_getters: A mapping of authentication service names to
107110
callables that return the corresponding authentication token.
108111
109112
Returns:
110-
A new SyncToolboxTool instance wrapping the updated async tool.
113+
A new ToolboxSyncTool instance with the specified authentication token
114+
getters registered.
111115
"""
112116
new_async_tool = self.__async_tool.add_auth_token_getters(auth_token_getters)
113117
return ToolboxSyncTool(new_async_tool, self.__loop, self.__thread)
@@ -116,14 +120,14 @@ def bind_parameters(
116120
self, bound_params: Mapping[str, Union[Callable[[], Any], Any]]
117121
) -> "ToolboxSyncTool":
118122
"""
119-
Binds parameters and returns a new SyncToolboxTool instance.
123+
Binds parameters to values or callables that produce values.
120124
121125
Args:
122126
bound_params: A mapping of parameter names to values or callables that
123127
produce values.
124128
125129
Returns:
126-
A new SyncToolboxTool instance wrapping the updated async tool.
130+
A new ToolboxSyncTool instance with the specified parameters bound.
127131
"""
128132
new_async_tool = self.__async_tool.bind_parameters(bound_params)
129133
return ToolboxSyncTool(new_async_tool, self.__loop, self.__thread)

0 commit comments

Comments
 (0)