Skip to content

Commit 2de1f32

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 d12c73e commit 2de1f32

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
@@ -51,9 +51,9 @@ def __init__(
5151
if not isinstance(async_tool, ToolboxTool):
5252
raise TypeError("async_tool must be an instance of ToolboxTool")
5353

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

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

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

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

7890
@property
7991
def __signature__(self) -> Signature:
80-
return self.__async_tool.__signature__
92+
return self._async_tool.__signature__
8193

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

89101
def __call__(self, *args: Any, **kwargs: Any) -> str:
90102
"""
@@ -100,8 +112,8 @@ def __call__(self, *args: Any, **kwargs: Any) -> str:
100112
Returns:
101113
The string result returned by the remote tool execution.
102114
"""
103-
coro = self.__async_tool(*args, **kwargs)
104-
return asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
115+
coro = self._async_tool(*args, **kwargs)
116+
return asyncio.run_coroutine_threadsafe(coro, self._loop).result()
105117

106118
def add_auth_token_getters(
107119
self,
@@ -120,8 +132,8 @@ def add_auth_token_getters(
120132
getters registered.
121133
"""
122134

123-
new_async_tool = self.__async_tool.add_auth_token_getters(auth_token_getters)
124-
return ToolboxSyncTool(new_async_tool, self.__loop, self.__thread)
135+
new_async_tool = self._async_tool.add_auth_token_getters(auth_token_getters)
136+
return ToolboxSyncTool(new_async_tool, self._loop, self._thread)
125137

126138
def bind_params(
127139
self, bound_params: Mapping[str, Union[Callable[[], Any], Any]]
@@ -137,8 +149,8 @@ def bind_params(
137149
A new ToolboxSyncTool instance with the specified parameters bound.
138150
"""
139151

140-
new_async_tool = self.__async_tool.bind_params(bound_params)
141-
return ToolboxSyncTool(new_async_tool, self.__loop, self.__thread)
152+
new_async_tool = self._async_tool.bind_params(bound_params)
153+
return ToolboxSyncTool(new_async_tool, self._loop, self._thread)
142154

143155
def bind_param(
144156
self,

0 commit comments

Comments
 (0)