Skip to content

Commit ceaae2f

Browse files
committed
chore: Make sync tool member variables protected
This change modifies the visibility of sync tool member variables to `protected`. This allows for direct and type-safe access by subclasses, such as those in orchestration-specific packages like `toolbox-langchain`'s `ToolboxTool` implementations. Making these variables protected also ensures they are read-only from outside the class hierarchy, enhancing encapsulation.
1 parent 7c61263 commit ceaae2f

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def __init__(
5252
if not isinstance(async_tool, ToolboxTool):
5353
raise TypeError("async_tool must be an instance of ToolboxTool")
5454

55-
self.__async_tool = async_tool
56-
self.__loop = loop
57-
self.__thread = thread
55+
self._async_tool = async_tool
56+
self._loop = loop
57+
self._thread = thread
5858

5959
# NOTE: We cannot define __qualname__ as a @property here.
6060
# Properties are designed to compute values dynamically when accessed on an *instance* (using 'self').
@@ -65,27 +65,39 @@ def __init__(
6565
# a string value immediately, not a descriptor that evaluates later.
6666
self.__qualname__ = f"{self.__class__.__qualname__}.{self.__async_tool._name}"
6767

68+
@property
69+
def _async_tool(self) -> ToolboxTool:
70+
return self._async_tool
71+
72+
@property
73+
def _loop(self) -> AbstractEventLoop:
74+
return self._loop
75+
76+
@property
77+
def _thread(self) -> Thread:
78+
return self._thread
79+
6880
@property
6981
def __name__(self) -> str:
70-
return self.__async_tool.__name__
82+
return self._async_tool.__name__
7183

7284
@property
7385
def __doc__(self) -> Union[str, None]: # type: ignore[override]
7486
# Standard Python object attributes like __doc__ are technically "writable".
7587
# But not defining a setter function makes this a read-only property.
7688
# Mypy flags this issue in the type checks.
77-
return self.__async_tool.__doc__
89+
return self._async_tool.__doc__
7890

7991
@property
8092
def __signature__(self) -> Signature:
81-
return self.__async_tool.__signature__
93+
return self._async_tool.__signature__
8294

8395
@property
8496
def __annotations__(self) -> dict[str, Any]: # type: ignore[override]
8597
# Standard Python object attributes like __doc__ are technically "writable".
8698
# But not defining a setter function makes this a read-only property.
8799
# Mypy flags this issue in the type checks.
88-
return self.__async_tool.__annotations__
100+
return self._async_tool.__annotations__
89101

90102
@property
91103
def _name(self) -> str:
@@ -129,8 +141,8 @@ def __call__(self, *args: Any, **kwargs: Any) -> str:
129141
Returns:
130142
The string result returned by the remote tool execution.
131143
"""
132-
coro = self.__async_tool(*args, **kwargs)
133-
return asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
144+
coro = self._async_tool(*args, **kwargs)
145+
return asyncio.run_coroutine_threadsafe(coro, self._loop).result()
134146

135147
def add_auth_token_getters(
136148
self,
@@ -149,8 +161,8 @@ def add_auth_token_getters(
149161
getters registered.
150162
"""
151163

152-
new_async_tool = self.__async_tool.add_auth_token_getters(auth_token_getters)
153-
return ToolboxSyncTool(new_async_tool, self.__loop, self.__thread)
164+
new_async_tool = self._async_tool.add_auth_token_getters(auth_token_getters)
165+
return ToolboxSyncTool(new_async_tool, self._loop, self._thread)
154166

155167
def bind_params(
156168
self, bound_params: Mapping[str, Union[Callable[[], Any], Any]]
@@ -166,8 +178,8 @@ def bind_params(
166178
A new ToolboxSyncTool instance with the specified parameters bound.
167179
"""
168180

169-
new_async_tool = self.__async_tool.bind_params(bound_params)
170-
return ToolboxSyncTool(new_async_tool, self.__loop, self.__thread)
181+
new_async_tool = self._async_tool.bind_params(bound_params)
182+
return ToolboxSyncTool(new_async_tool, self._loop, self._thread)
171183

172184
def bind_param(
173185
self,

0 commit comments

Comments
 (0)