From 941edb680a5330dfe006d88e6c8fa1ec708cb1d4 Mon Sep 17 00:00:00 2001 From: Twisha Bansal Date: Tue, 29 Apr 2025 10:18:21 +0530 Subject: [PATCH 1/9] try --- .../toolbox-core/src/toolbox_core/client.py | 2 +- .../src/toolbox_core/sync_tool.py | 2 +- .../toolbox-core/src/toolbox_core/tool.py | 33 +++++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/toolbox-core/src/toolbox_core/client.py b/packages/toolbox-core/src/toolbox_core/client.py index 4d4d9db2..a5c19fdb 100644 --- a/packages/toolbox-core/src/toolbox_core/client.py +++ b/packages/toolbox-core/src/toolbox_core/client.py @@ -88,7 +88,7 @@ def __parse_tool( base_url=self.__base_url, name=name, description=schema.description, - params=params, + params=tuple(params), # create a read-only values for the maps to prevent mutation required_authn_params=types.MappingProxyType(authn_params), auth_service_token_getters=types.MappingProxyType(auth_token_getters), diff --git a/packages/toolbox-core/src/toolbox_core/sync_tool.py b/packages/toolbox-core/src/toolbox_core/sync_tool.py index ad24e8e3..4062e55f 100644 --- a/packages/toolbox-core/src/toolbox_core/sync_tool.py +++ b/packages/toolbox-core/src/toolbox_core/sync_tool.py @@ -67,7 +67,7 @@ def __init__( @property def __name__(self) -> str: - return self.__async_tool.__name__ + return self.__async_tool._name @property def __doc__(self) -> Union[str, None]: # type: ignore[override] diff --git a/packages/toolbox-core/src/toolbox_core/tool.py b/packages/toolbox-core/src/toolbox_core/tool.py index d26d9287..303a2785 100644 --- a/packages/toolbox-core/src/toolbox_core/tool.py +++ b/packages/toolbox-core/src/toolbox_core/tool.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. - -import types from inspect import Signature +import copy from typing import Any, Callable, Coroutine, Mapping, Optional, Sequence, Union - +from types import MappingProxyType from aiohttp import ClientSession from .protocol import ParameterSchema @@ -113,6 +112,34 @@ def __init__( # map of client headers to their value/callable/coroutine self.__client_headers = client_headers + @property + def _name(self) -> str: + return self.__name__ + + @property + def _description(self) -> str: + return self.__description + + @property + def _params(self) -> Sequence[ParameterSchema]: + return copy.deepcopy(self.__params) + + @property + def _bound_params(self) -> Mapping[str, Union[Callable[[], Any], Any]]: + return MappingProxyType(self.__bound_parameters) + + @property + def _required_auth_params(self) -> Mapping[str, list[str]]: + return MappingProxyType(self.__required_authn_params) + + @property + def _auth_service_token_getters(self) -> Mapping[str, Callable[[], str]]: + return MappingProxyType(self.__auth_service_token_getters) + + @property + def _client_headers(self) -> Mapping[str, Union[Callable, Coroutine, str]]: + return MappingProxyType(self.__client_headers) + def __copy( self, session: Optional[ClientSession] = None, From db29a619507d72a478a91816b82e871228a276ed Mon Sep 17 00:00:00 2001 From: Twisha Bansal Date: Mon, 5 May 2025 15:02:27 +0530 Subject: [PATCH 2/9] lint --- packages/toolbox-core/src/toolbox_core/tool.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/toolbox-core/src/toolbox_core/tool.py b/packages/toolbox-core/src/toolbox_core/tool.py index 303a2785..dc6c648b 100644 --- a/packages/toolbox-core/src/toolbox_core/tool.py +++ b/packages/toolbox-core/src/toolbox_core/tool.py @@ -12,10 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from inspect import Signature import copy -from typing import Any, Callable, Coroutine, Mapping, Optional, Sequence, Union +from inspect import Signature from types import MappingProxyType +from typing import Any, Callable, Coroutine, Mapping, Optional, Sequence, Union + from aiohttp import ClientSession from .protocol import ParameterSchema From 6c332e0f448cab17d43ef8d1b3130b8471443b0d Mon Sep 17 00:00:00 2001 From: Twisha Bansal Date: Mon, 5 May 2025 15:05:42 +0530 Subject: [PATCH 3/9] fix --- packages/toolbox-core/src/toolbox_core/tool.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/toolbox-core/src/toolbox_core/tool.py b/packages/toolbox-core/src/toolbox_core/tool.py index dc6c648b..dec8ae35 100644 --- a/packages/toolbox-core/src/toolbox_core/tool.py +++ b/packages/toolbox-core/src/toolbox_core/tool.py @@ -286,11 +286,11 @@ def add_auth_token_getters( ) # create a read-only updated value for new_getters - new_getters = types.MappingProxyType( + new_getters = MappingProxyType( dict(self.__auth_service_token_getters, **auth_token_getters) ) # create a read-only updated for params that are still required - new_req_authn_params = types.MappingProxyType( + new_req_authn_params = MappingProxyType( identify_required_authn_params( self.__required_authn_params, auth_token_getters.keys() ) @@ -328,5 +328,5 @@ def bind_parameters( return self.__copy( params=new_params, - bound_params=types.MappingProxyType(all_bound_params), + bound_params=MappingProxyType(all_bound_params), ) From 46884a381e9f5de87f820b1362b56e7a412608f8 Mon Sep 17 00:00:00 2001 From: Twisha Bansal Date: Mon, 5 May 2025 15:11:40 +0530 Subject: [PATCH 4/9] add properties to sync tool --- .../src/toolbox_core/sync_tool.py | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/toolbox-core/src/toolbox_core/sync_tool.py b/packages/toolbox-core/src/toolbox_core/sync_tool.py index 4062e55f..159e0f01 100644 --- a/packages/toolbox-core/src/toolbox_core/sync_tool.py +++ b/packages/toolbox-core/src/toolbox_core/sync_tool.py @@ -12,11 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +import copy import asyncio from asyncio import AbstractEventLoop from inspect import Signature from threading import Thread -from typing import Any, Callable, Mapping, TypeVar, Union +from typing import Any, Callable, Mapping, TypeVar, Union, Sequence, Coroutine + +from .protocol import ParameterSchema + from .tool import ToolboxTool @@ -66,26 +70,32 @@ def __init__( self.__qualname__ = f"{self.__class__.__qualname__}.{self.__name__}" @property - def __name__(self) -> str: - return self.__async_tool._name + def _name(self) -> str: + return self.__name__ + + @property + def _description(self) -> str: + return self.__async_tool._description + + @property + def _params(self) -> Sequence[ParameterSchema]: + return self.__async_tool._params + + @property + def _bound_params(self) -> Mapping[str, Union[Callable[[], Any], Any]]: + return self.__async_tool._bound_params @property - def __doc__(self) -> Union[str, None]: # type: ignore[override] - # Standard Python object attributes like __doc__ are technically "writable". - # But not defining a setter function makes this a read-only property. - # Mypy flags this issue in the type checks. - return self.__async_tool.__doc__ + def _required_auth_params(self) -> Mapping[str, list[str]]: + return self.__async_tool._required_auth_params @property - def __signature__(self) -> Signature: - return self.__async_tool.__signature__ + def _auth_service_token_getters(self) -> Mapping[str, Callable[[], str]]: + return self.__async_tool._auth_service_token_getters @property - def __annotations__(self) -> dict[str, Any]: # type: ignore[override] - # Standard Python object attributes like __doc__ are technically "writable". - # But not defining a setter function makes this a read-only property. - # Mypy flags this issue in the type checks. - return self.__async_tool.__annotations__ + def _client_headers(self) -> Mapping[str, Union[Callable, Coroutine, str]]: + return self.__async_tool._client_headers def __call__(self, *args: Any, **kwargs: Any) -> str: """ From ec2fe41a333b8e712a408e45516269a524620291 Mon Sep 17 00:00:00 2001 From: Twisha Bansal Date: Mon, 5 May 2025 15:12:11 +0530 Subject: [PATCH 5/9] lint --- packages/toolbox-core/src/toolbox_core/sync_tool.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/toolbox-core/src/toolbox_core/sync_tool.py b/packages/toolbox-core/src/toolbox_core/sync_tool.py index 159e0f01..518c79db 100644 --- a/packages/toolbox-core/src/toolbox_core/sync_tool.py +++ b/packages/toolbox-core/src/toolbox_core/sync_tool.py @@ -12,16 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import copy import asyncio from asyncio import AbstractEventLoop -from inspect import Signature from threading import Thread -from typing import Any, Callable, Mapping, TypeVar, Union, Sequence, Coroutine +from typing import Any, Callable, Coroutine, Mapping, Sequence, TypeVar, Union from .protocol import ParameterSchema - - from .tool import ToolboxTool T = TypeVar("T") From faeaa4b2a1adcf22892dc16b4a5612112cbbbe9b Mon Sep 17 00:00:00 2001 From: Twisha Bansal Date: Mon, 5 May 2025 15:14:48 +0530 Subject: [PATCH 6/9] fix --- packages/toolbox-core/src/toolbox_core/sync_tool.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/toolbox-core/src/toolbox_core/sync_tool.py b/packages/toolbox-core/src/toolbox_core/sync_tool.py index 518c79db..b774a959 100644 --- a/packages/toolbox-core/src/toolbox_core/sync_tool.py +++ b/packages/toolbox-core/src/toolbox_core/sync_tool.py @@ -63,11 +63,11 @@ def __init__( # itself is being processed during module import or class definition. # Defining __qualname__ as a property leads to a TypeError because the class object needs # a string value immediately, not a descriptor that evaluates later. - self.__qualname__ = f"{self.__class__.__qualname__}.{self.__name__}" + self.__qualname__ = f"{self.__class__.__qualname__}.{self.__async_tool._name}" @property def _name(self) -> str: - return self.__name__ + return self.__async_tool._name @property def _description(self) -> str: From d81a53bac4d49cfa94081b7e99e9dba3df1ef4d5 Mon Sep 17 00:00:00 2001 From: Twisha Bansal Date: Mon, 5 May 2025 15:19:12 +0530 Subject: [PATCH 7/9] do not remove existing properties --- .../src/toolbox_core/sync_tool.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/toolbox-core/src/toolbox_core/sync_tool.py b/packages/toolbox-core/src/toolbox_core/sync_tool.py index b774a959..7721e0c8 100644 --- a/packages/toolbox-core/src/toolbox_core/sync_tool.py +++ b/packages/toolbox-core/src/toolbox_core/sync_tool.py @@ -15,6 +15,7 @@ import asyncio from asyncio import AbstractEventLoop from threading import Thread +from inspect import Signature from typing import Any, Callable, Coroutine, Mapping, Sequence, TypeVar, Union from .protocol import ParameterSchema @@ -65,6 +66,28 @@ def __init__( # a string value immediately, not a descriptor that evaluates later. self.__qualname__ = f"{self.__class__.__qualname__}.{self.__async_tool._name}" + @property + def __name__(self) -> str: + return self.__async_tool.__name__ + + @property + def __doc__(self) -> Union[str, None]: # type: ignore[override] + # Standard Python object attributes like __doc__ are technically "writable". + # But not defining a setter function makes this a read-only property. + # Mypy flags this issue in the type checks. + return self.__async_tool.__doc__ + + @property + def __signature__(self) -> Signature: + return self.__async_tool.__signature__ + + @property + def __annotations__(self) -> dict[str, Any]: # type: ignore[override] + # Standard Python object attributes like __doc__ are technically "writable". + # But not defining a setter function makes this a read-only property. + # Mypy flags this issue in the type checks. + return self.__async_tool.__annotations__ + @property def _name(self) -> str: return self.__async_tool._name From 67613ab58e2ebe2c1a7e239c698fdb22caa5134e Mon Sep 17 00:00:00 2001 From: Twisha Bansal Date: Mon, 5 May 2025 15:22:15 +0530 Subject: [PATCH 8/9] lint --- packages/toolbox-core/src/toolbox_core/sync_tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/toolbox-core/src/toolbox_core/sync_tool.py b/packages/toolbox-core/src/toolbox_core/sync_tool.py index 7721e0c8..ec05d9e0 100644 --- a/packages/toolbox-core/src/toolbox_core/sync_tool.py +++ b/packages/toolbox-core/src/toolbox_core/sync_tool.py @@ -14,8 +14,8 @@ import asyncio from asyncio import AbstractEventLoop -from threading import Thread from inspect import Signature +from threading import Thread from typing import Any, Callable, Coroutine, Mapping, Sequence, TypeVar, Union from .protocol import ParameterSchema From 7ef23742c9ac43dc6a80eb454f5ab8d63d921090 Mon Sep 17 00:00:00 2001 From: Twisha Bansal <58483338+twishabansal@users.noreply.github.com> Date: Tue, 6 May 2025 13:47:03 +0530 Subject: [PATCH 9/9] move comment --- packages/toolbox-core/src/toolbox_core/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/toolbox-core/src/toolbox_core/client.py b/packages/toolbox-core/src/toolbox_core/client.py index 633ef94b..e59a934b 100644 --- a/packages/toolbox-core/src/toolbox_core/client.py +++ b/packages/toolbox-core/src/toolbox_core/client.py @@ -88,8 +88,8 @@ def __parse_tool( base_url=self.__base_url, name=name, description=schema.description, + # create a read-only values to prevent mutation params=tuple(params), - # create a read-only values for the maps to prevent mutation required_authn_params=types.MappingProxyType(authn_params), auth_service_token_getters=types.MappingProxyType(auth_token_getters), bound_params=types.MappingProxyType(bound_params),