Skip to content

Commit b85820b

Browse files
authored
chore(toolbox-core): Add property decorators to tool (#218)
* try * lint * fix * add properties to sync tool * lint * fix * do not remove existing properties * lint * move comment
1 parent b8281c8 commit b85820b

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def __parse_tool(
8888
base_url=self.__base_url,
8989
name=name,
9090
description=schema.description,
91-
params=params,
92-
# create a read-only values for the maps to prevent mutation
91+
# create a read-only values to prevent mutation
92+
params=tuple(params),
9393
required_authn_params=types.MappingProxyType(authn_params),
9494
auth_service_token_getters=types.MappingProxyType(auth_token_getters),
9595
bound_params=types.MappingProxyType(bound_params),

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
from asyncio import AbstractEventLoop
1717
from inspect import Signature
1818
from threading import Thread
19-
from typing import Any, Callable, Mapping, TypeVar, Union
19+
from typing import Any, Callable, Coroutine, Mapping, Sequence, TypeVar, Union
2020

21+
from .protocol import ParameterSchema
2122
from .tool import ToolboxTool
2223

2324
T = TypeVar("T")
@@ -63,7 +64,7 @@ def __init__(
6364
# itself is being processed during module import or class definition.
6465
# Defining __qualname__ as a property leads to a TypeError because the class object needs
6566
# a string value immediately, not a descriptor that evaluates later.
66-
self.__qualname__ = f"{self.__class__.__qualname__}.{self.__name__}"
67+
self.__qualname__ = f"{self.__class__.__qualname__}.{self.__async_tool._name}"
6768

6869
@property
6970
def __name__(self) -> str:
@@ -87,6 +88,34 @@ def __annotations__(self) -> dict[str, Any]: # type: ignore[override]
8788
# Mypy flags this issue in the type checks.
8889
return self.__async_tool.__annotations__
8990

91+
@property
92+
def _name(self) -> str:
93+
return self.__async_tool._name
94+
95+
@property
96+
def _description(self) -> str:
97+
return self.__async_tool._description
98+
99+
@property
100+
def _params(self) -> Sequence[ParameterSchema]:
101+
return self.__async_tool._params
102+
103+
@property
104+
def _bound_params(self) -> Mapping[str, Union[Callable[[], Any], Any]]:
105+
return self.__async_tool._bound_params
106+
107+
@property
108+
def _required_auth_params(self) -> Mapping[str, list[str]]:
109+
return self.__async_tool._required_auth_params
110+
111+
@property
112+
def _auth_service_token_getters(self) -> Mapping[str, Callable[[], str]]:
113+
return self.__async_tool._auth_service_token_getters
114+
115+
@property
116+
def _client_headers(self) -> Mapping[str, Union[Callable, Coroutine, str]]:
117+
return self.__async_tool._client_headers
118+
90119
def __call__(self, *args: Any, **kwargs: Any) -> str:
91120
"""
92121
Synchronously calls the remote tool with the provided arguments.

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
16-
import types
15+
import copy
1716
from inspect import Signature
17+
from types import MappingProxyType
1818
from typing import Any, Callable, Coroutine, Mapping, Optional, Sequence, Union
1919

2020
from aiohttp import ClientSession
@@ -113,6 +113,34 @@ def __init__(
113113
# map of client headers to their value/callable/coroutine
114114
self.__client_headers = client_headers
115115

116+
@property
117+
def _name(self) -> str:
118+
return self.__name__
119+
120+
@property
121+
def _description(self) -> str:
122+
return self.__description
123+
124+
@property
125+
def _params(self) -> Sequence[ParameterSchema]:
126+
return copy.deepcopy(self.__params)
127+
128+
@property
129+
def _bound_params(self) -> Mapping[str, Union[Callable[[], Any], Any]]:
130+
return MappingProxyType(self.__bound_parameters)
131+
132+
@property
133+
def _required_auth_params(self) -> Mapping[str, list[str]]:
134+
return MappingProxyType(self.__required_authn_params)
135+
136+
@property
137+
def _auth_service_token_getters(self) -> Mapping[str, Callable[[], str]]:
138+
return MappingProxyType(self.__auth_service_token_getters)
139+
140+
@property
141+
def _client_headers(self) -> Mapping[str, Union[Callable, Coroutine, str]]:
142+
return MappingProxyType(self.__client_headers)
143+
116144
def __copy(
117145
self,
118146
session: Optional[ClientSession] = None,
@@ -258,11 +286,11 @@ def add_auth_token_getters(
258286
)
259287

260288
# create a read-only updated value for new_getters
261-
new_getters = types.MappingProxyType(
289+
new_getters = MappingProxyType(
262290
dict(self.__auth_service_token_getters, **auth_token_getters)
263291
)
264292
# create a read-only updated for params that are still required
265-
new_req_authn_params = types.MappingProxyType(
293+
new_req_authn_params = MappingProxyType(
266294
identify_required_authn_params(
267295
self.__required_authn_params, auth_token_getters.keys()
268296
)
@@ -300,5 +328,5 @@ def bind_parameters(
300328

301329
return self.__copy(
302330
params=new_params,
303-
bound_params=types.MappingProxyType(all_bound_params),
331+
bound_params=MappingProxyType(all_bound_params),
304332
)

0 commit comments

Comments
 (0)