From 7992eb0a0269a44fa1052fc67a95d6d60a949a5f Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Wed, 14 May 2025 05:27:09 +0000 Subject: [PATCH 01/21] feat: cache Method.method_selector_fn --- newsfragments/3696.performance.rst | 1 + web3/method.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 newsfragments/3696.performance.rst diff --git a/newsfragments/3696.performance.rst b/newsfragments/3696.performance.rst new file mode 100644 index 0000000000..34323f3336 --- /dev/null +++ b/newsfragments/3696.performance.rst @@ -0,0 +1 @@ +cache Method.method_selector_function \ No newline at end of file diff --git a/web3/method.py b/web3/method.py index c9ced51f17..3c3bc478ba 100644 --- a/web3/method.py +++ b/web3/method.py @@ -179,15 +179,15 @@ def __get__( def __call__(self, *args: Any, **kwargs: Any) -> Any: return self.__get__(self._module)(*args, **kwargs) - @property + @functools.cached_property def method_selector_fn( self, - ) -> Callable[..., Union[RPCEndpoint, Callable[..., RPCEndpoint]]]: + ) -> Callable[[], RPCEndpoint]: """Gets the method selector from the config.""" if callable(self.json_rpc_method): return self.json_rpc_method elif isinstance(self.json_rpc_method, (str,)): - return lambda *_: self.json_rpc_method + return lambda: self.json_rpc_method raise Web3ValueError( "``json_rpc_method`` config invalid. May be a string or function" ) From eed202eeabc46f716910e7ef512080c45e4f3d09 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Wed, 14 May 2025 05:27:40 +0000 Subject: [PATCH 02/21] chore: make formatter typing more specific --- web3/middleware/validation.py | 4 ++-- web3/types.py | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/web3/middleware/validation.py b/web3/middleware/validation.py index e90cff8690..42409be58b 100644 --- a/web3/middleware/validation.py +++ b/web3/middleware/validation.py @@ -36,8 +36,8 @@ FormattingMiddlewareBuilder, ) from web3.types import ( - Formatters, FormattersDict, + RequestFormatters, RPCEndpoint, TxParams, ) @@ -151,7 +151,7 @@ def build_method_validators(w3: "Web3", method: RPCEndpoint) -> FormattersDict: async def async_build_method_validators( async_w3: "AsyncWeb3", method: RPCEndpoint ) -> FormattersDict: - request_formatters: Formatters = {} + request_formatters: RequestFormatters = {} if RPCEndpoint(method) in METHODS_TO_VALIDATE: w3_chain_id = await async_w3.eth.chain_id for method in METHODS_TO_VALIDATE: diff --git a/web3/types.py b/web3/types.py index 8a1f67ad44..ffea603229 100644 --- a/web3/types.py +++ b/web3/types.py @@ -69,7 +69,10 @@ Timestamp = NewType("Timestamp", int) Wei = NewType("Wei", int) Gwei = NewType("Gwei", int) -Formatters = Dict[RPCEndpoint, Callable[..., Any]] +RequestFormatter = Callable[..., TReturn] +RequestFormatters = Dict[RPCEndpoint, RequestFormatter[Any]] +ResponseFormatter = Callable[["RPCResponse"], TReturn] +ResponseFormatters = Dict[RPCEndpoint, ResponseFormatter[Any]] class AccessListEntry(TypedDict): @@ -335,9 +338,9 @@ class CreateAccessListResponse(TypedDict): class FormattersDict(TypedDict, total=False): - error_formatters: Optional[Formatters] - request_formatters: Optional[Formatters] - result_formatters: Optional[Formatters] + error_formatters: Optional[ResponseFormatters] + request_formatters: Optional[ResponseFormatters] + result_formatters: Optional[ResponseFormatters] class FilterParams(TypedDict, total=False): From c2d2fec1128b869513ab1047afb8719d432cafe3 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Wed, 14 May 2025 05:27:40 +0000 Subject: [PATCH 03/21] feat: cache Method.method_selector_fn --- web3/method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web3/method.py b/web3/method.py index 3c3bc478ba..0fd56bd02e 100644 --- a/web3/method.py +++ b/web3/method.py @@ -166,7 +166,7 @@ def __get__( ) provider = module.w3.provider - if hasattr(provider, "_is_batching") and provider._is_batching: + if getattr(provider, "_is_batching", False): if self.json_rpc_method in RPC_METHODS_UNSUPPORTED_DURING_BATCH: raise MethodNotSupported( f"Method `{self.json_rpc_method}` is not supported within a batch " From 923de715267bf940cb28a5bf9d48022987821b9a Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Wed, 14 May 2025 05:27:40 +0000 Subject: [PATCH 04/21] chore: refactor out _apply_request_formatters --- tests/core/method-class/test_method.py | 7 +-- web3/_utils/method_formatters.py | 2 +- web3/manager.py | 6 +-- web3/method.py | 56 ++++++++++------------- web3/module.py | 61 ++++++++++++++++++++------ 5 files changed, 73 insertions(+), 59 deletions(-) diff --git a/tests/core/method-class/test_method.py b/tests/core/method-class/test_method.py index 0d82a6b602..88766d9d09 100644 --- a/tests/core/method-class/test_method.py +++ b/tests/core/method-class/test_method.py @@ -17,7 +17,6 @@ ) from web3.method import ( Method, - _apply_request_formatters, default_root_munger, ) from web3.module import ( @@ -61,11 +60,7 @@ def test_get_formatters_default_formatter_for_falsy_config(): default_result_formatters = method.result_formatters( method.method_selector_fn(), "some module" ) - assert _apply_request_formatters(["a", "b", "c"], default_request_formatters) == ( - "a", - "b", - "c", - ) + assert default_request_formatters(["a", "b", "c"]) == ("a", "b", "c") assert apply_result_formatters(default_result_formatters, ["a", "b", "c"]) == [ "a", "b", diff --git a/web3/_utils/method_formatters.py b/web3/_utils/method_formatters.py index 8cc3eab58e..b895e008a6 100644 --- a/web3/_utils/method_formatters.py +++ b/web3/_utils/method_formatters.py @@ -1120,7 +1120,7 @@ def get_request_formatters( PYTHONIC_REQUEST_FORMATTERS, ) formatters = combine_formatters(request_formatter_maps, method_name) - return compose(*formatters) + return compose(tuple, *formatters) def raise_block_not_found(params: Tuple[BlockIdentifier, bool]) -> NoReturn: diff --git a/web3/manager.py b/web3/manager.py index 0ee3e60669..a4adeba740 100644 --- a/web3/manager.py +++ b/web3/manager.py @@ -6,7 +6,6 @@ AsyncGenerator, Callable, Coroutine, - Dict, List, Optional, Sequence, @@ -48,6 +47,7 @@ ) from web3.method import ( Method, + ResponseFormatters, ) from web3.middleware import ( AttributeDictMiddleware, @@ -360,9 +360,7 @@ async def socket_request( self, method: RPCEndpoint, params: Any, - response_formatters: Optional[ - Tuple[Dict[str, Callable[..., Any]], Callable[..., Any], Callable[..., Any]] - ] = None, + response_formatters: Optional[ResponseFormatters[Any]] = None, ) -> RPCResponse: provider = cast(PersistentConnectionProvider, self._provider) self.logger.debug( diff --git a/web3/method.py b/web3/method.py index 0fd56bd02e..9170d1c121 100644 --- a/web3/method.py +++ b/web3/method.py @@ -3,24 +3,15 @@ TYPE_CHECKING, Any, Callable, - Dict, Generic, List, Optional, Sequence, Tuple, Type, - Union, ) import warnings -from eth_utils.curried import ( - to_tuple, -) -from eth_utils.toolz import ( - pipe, -) - from web3._utils.batching import ( RPC_METHODS_UNSUPPORTED_DURING_BATCH, ) @@ -40,6 +31,8 @@ Web3ValueError, ) from web3.types import ( + RequestFormatter, + ResponseFormatter, RPCEndpoint, TFunc, TReturn, @@ -54,16 +47,10 @@ Munger = Callable[..., Any] - - -@to_tuple -def _apply_request_formatters( - params: Any, request_formatters: Dict[RPCEndpoint, Callable[..., TReturn]] -) -> Tuple[Any, ...]: - if request_formatters: - formatted_params = pipe(params, request_formatters) - return formatted_params - return params +RequestArgs = Tuple[RPCEndpoint, Sequence[Any]] +ResponseFormatters = Tuple[ + ResponseFormatter[TReturn], ResponseFormatter[TReturn], ResponseFormatter[TReturn] +] def _set_mungers( @@ -136,9 +123,15 @@ def __init__( self, json_rpc_method: Optional[RPCEndpoint] = None, mungers: Optional[Sequence[Munger]] = None, - request_formatters: Optional[Callable[..., TReturn]] = None, - result_formatters: Optional[Callable[..., TReturn]] = None, - null_result_formatters: Optional[Callable[..., TReturn]] = None, + request_formatters: Optional[ + Callable[[RPCEndpoint], RequestFormatter[Any]] + ] = None, + result_formatters: Optional[ + Callable[[RPCEndpoint, "Module"], ResponseFormatter[TReturn]] + ] = None, + null_result_formatters: Optional[ + Callable[[RPCEndpoint], ResponseFormatter[TReturn]] + ] = None, method_choice_depends_on_args: Optional[Callable[..., RPCEndpoint]] = None, is_property: bool = False, ): @@ -202,14 +195,7 @@ def input_munger(self, module: "Module", args: Any, kwargs: Any) -> List[Any]: def process_params( self, module: "Module", *args: Any, **kwargs: Any - ) -> Tuple[ - Tuple[Union[RPCEndpoint, Callable[..., RPCEndpoint]], Tuple[RPCEndpoint, ...]], - Tuple[ - Union[TReturn, Dict[str, Callable[..., Any]]], - Callable[..., Any], - Union[TReturn, Callable[..., Any]], - ], - ]: + ) -> Tuple[RequestArgs, ResponseFormatters[TReturn]]: params = self.input_munger(module, args, kwargs) if self.method_choice_depends_on_args: @@ -232,10 +218,12 @@ def process_params( get_error_formatters(method), self.null_result_formatters(method), ) - request = ( - method, - _apply_request_formatters(params, self.request_formatters(method)), - ) + + if request_formatters := self.request_formatters(method): + params = request_formatters(params) + + request = method, params + return request, response_formatters diff --git a/web3/module.py b/web3/module.py index 0f9d85d7bd..cd4c95766f 100644 --- a/web3/module.py +++ b/web3/module.py @@ -5,11 +5,10 @@ Coroutine, Dict, Optional, - Sequence, Tuple, TypeVar, Union, - cast, + overload, ) from eth_abi.codec import ( @@ -27,14 +26,16 @@ ) from web3.method import ( Method, + RequestArgs, + ResponseFormatters, ) from web3.providers.persistent import ( PersistentConnectionProvider, ) from web3.types import ( FormattedEthSubscriptionResponse, - RPCEndpoint, RPCResponse, + TFunc, ) if TYPE_CHECKING: @@ -58,34 +59,52 @@ def apply_result_formatters( TReturn = TypeVar("TReturn") +@overload +def retrieve_request_information_for_batching( + w3: "AsyncWeb3", + module: "Module", + method: Method[Callable[..., Any]], +) -> Callable[..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters[Any]]]]: + ... + + +@overload +def retrieve_request_information_for_batching( + w3: "Web3", + module: "Module", + method: Method[Callable[..., Any]], +) -> Callable[..., Tuple[RequestArgs, ResponseFormatters[Any]]]: + ... + + @curry def retrieve_request_information_for_batching( w3: Union["AsyncWeb3", "Web3"], module: "Module", method: Method[Callable[..., Any]], ) -> Union[ - Callable[..., Tuple[Tuple[RPCEndpoint, Any], Sequence[Any]]], - Callable[..., Coroutine[Any, Any, Tuple[Tuple[RPCEndpoint, Any], Sequence[Any]]]], + Callable[..., Tuple[RequestArgs, ResponseFormatters[Any]]], + Callable[..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters[Any]]]], ]: async def async_inner( *args: Any, **kwargs: Any - ) -> Tuple[Tuple[RPCEndpoint, Any], Sequence[Any]]: + ) -> Tuple[RequestArgs, ResponseFormatters[Any]]: + response_formatters: ResponseFormatters[Any] (method_str, params), response_formatters = method.process_params( module, *args, **kwargs ) if isinstance(w3.provider, PersistentConnectionProvider): w3.provider._request_processor.cache_request_information( - None, cast(RPCEndpoint, method_str), params, response_formatters + None, method_str, params, response_formatters ) - return (cast(RPCEndpoint, method_str), params), response_formatters + return (method_str, params), response_formatters - def inner( - *args: Any, **kwargs: Any - ) -> Tuple[Tuple[RPCEndpoint, Any], Sequence[Any]]: + def inner(*args: Any, **kwargs: Any) -> Tuple[RequestArgs, ResponseFormatters[Any]]: + response_formatters: ResponseFormatters[Any] (method_str, params), response_formatters = method.process_params( module, *args, **kwargs ) - return (cast(RPCEndpoint, method_str), params), response_formatters + return (method_str, params), response_formatters return async_inner if module.is_async else inner @@ -97,6 +116,8 @@ def retrieve_blocking_method_call_fn( method: Method[Callable[..., TReturn]], ) -> Callable[..., Union[TReturn, LogFilter]]: def caller(*args: Any, **kwargs: Any) -> Union[TReturn, LogFilter]: + response_formatters: ResponseFormatters[Any] + try: (method_str, params), response_formatters = method.process_params( module, *args, **kwargs @@ -133,6 +154,8 @@ def retrieve_async_method_call_fn( async def caller( *args: Any, **kwargs: Any ) -> Union[RPCResponse, FormattedEthSubscriptionResponse, AsyncLogFilter]: + response_formatters: ResponseFormatters[Any] + try: (method_str, params), response_formatters = method.process_params( module, *args, **kwargs @@ -142,7 +165,7 @@ async def caller( if isinstance(async_w3.provider, PersistentConnectionProvider): return await async_w3.manager.socket_request( - cast(RPCEndpoint, method_str), + method_str, params, response_formatters=response_formatters, ) @@ -167,12 +190,22 @@ async def caller( class Module: is_async = False + retrieve_request_information: Callable[ + [Method[TFunc]], + Union[ + Callable[..., Tuple[RequestArgs, ResponseFormatters[Any]]], + Callable[ + ..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters[Any]]] + ], + ], + ] + def __init__(self, w3: Union["AsyncWeb3", "Web3"]) -> None: if self.is_async: self.retrieve_caller_fn = retrieve_async_method_call_fn(w3, self) else: self.retrieve_caller_fn = retrieve_blocking_method_call_fn(w3, self) - self.retrieve_request_information = retrieve_request_information_for_batching( + self.retrieve_request_information = retrieve_request_information_for_batching( # type: ignore [call-overload] w3, self ) self.w3 = w3 From c085ca2aee83dd475b7ab48f0bf2a0e0d0ea0670 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Tue, 13 May 2025 22:33:44 -0400 Subject: [PATCH 05/21] Update method_formatters.py --- web3/_utils/method_formatters.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/web3/_utils/method_formatters.py b/web3/_utils/method_formatters.py index b895e008a6..50cc22968e 100644 --- a/web3/_utils/method_formatters.py +++ b/web3/_utils/method_formatters.py @@ -171,12 +171,9 @@ def type_aware_apply_formatters_to_dict( if isinstance(value, BaseModel): value = value.model_dump(by_alias=True) - formatted_dict: Dict[str, Any] = apply_formatters_to_dict(formatters, dict(value)) - return ( - AttributeDict.recursive(formatted_dict) - if is_attrdict(value) - else formatted_dict - ) + formatted: Dict[str, Any] + formatted = apply_formatters_to_dict(formatters, value) + return AttributeDict.recursive(formatted) if is_attrdict(value) else formatted def type_aware_apply_formatters_to_dict_keys_and_values( From 4288b3ec418bf1dfa8cb6355d9345a08d9262d6c Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Tue, 13 May 2025 22:46:18 -0400 Subject: [PATCH 06/21] Update types.py --- web3/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web3/types.py b/web3/types.py index ffea603229..8f6aef87ba 100644 --- a/web3/types.py +++ b/web3/types.py @@ -69,8 +69,8 @@ Timestamp = NewType("Timestamp", int) Wei = NewType("Wei", int) Gwei = NewType("Gwei", int) -RequestFormatter = Callable[..., TReturn] -RequestFormatters = Dict[RPCEndpoint, RequestFormatter[Any]] +Formatter = Callable[TParams, TReturn] +Formatters = Dict[RPCEndpoint, Formatter[Any]] ResponseFormatter = Callable[["RPCResponse"], TReturn] ResponseFormatters = Dict[RPCEndpoint, ResponseFormatter[Any]] From e8a001ffef54e1b1c459e7b7881c8fb3b7215965 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Tue, 13 May 2025 22:46:48 -0400 Subject: [PATCH 07/21] Update types.py --- web3/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web3/types.py b/web3/types.py index 8f6aef87ba..fb8a17ec07 100644 --- a/web3/types.py +++ b/web3/types.py @@ -69,7 +69,7 @@ Timestamp = NewType("Timestamp", int) Wei = NewType("Wei", int) Gwei = NewType("Gwei", int) -Formatter = Callable[TParams, TReturn] +Formatter = Callable[..., TReturn] Formatters = Dict[RPCEndpoint, Formatter[Any]] ResponseFormatter = Callable[["RPCResponse"], TReturn] ResponseFormatters = Dict[RPCEndpoint, ResponseFormatter[Any]] From f2fb7db4d06237824452cb7f0406252c5a303ec7 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Tue, 13 May 2025 22:49:34 -0400 Subject: [PATCH 08/21] Update method.py --- web3/method.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web3/method.py b/web3/method.py index 9170d1c121..f7be49b19d 100644 --- a/web3/method.py +++ b/web3/method.py @@ -31,7 +31,7 @@ Web3ValueError, ) from web3.types import ( - RequestFormatter, + Formatter, ResponseFormatter, RPCEndpoint, TFunc, @@ -124,7 +124,7 @@ def __init__( json_rpc_method: Optional[RPCEndpoint] = None, mungers: Optional[Sequence[Munger]] = None, request_formatters: Optional[ - Callable[[RPCEndpoint], RequestFormatter[Any]] + Callable[[RPCEndpoint], Formatter[Any]] ] = None, result_formatters: Optional[ Callable[[RPCEndpoint, "Module"], ResponseFormatter[TReturn]] From a47dd82cccd726ef3481a8118927b7e7ac241fe2 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Tue, 13 May 2025 22:52:09 -0400 Subject: [PATCH 09/21] Update validation.py --- web3/middleware/validation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web3/middleware/validation.py b/web3/middleware/validation.py index 42409be58b..e90cff8690 100644 --- a/web3/middleware/validation.py +++ b/web3/middleware/validation.py @@ -36,8 +36,8 @@ FormattingMiddlewareBuilder, ) from web3.types import ( + Formatters, FormattersDict, - RequestFormatters, RPCEndpoint, TxParams, ) @@ -151,7 +151,7 @@ def build_method_validators(w3: "Web3", method: RPCEndpoint) -> FormattersDict: async def async_build_method_validators( async_w3: "AsyncWeb3", method: RPCEndpoint ) -> FormattersDict: - request_formatters: RequestFormatters = {} + request_formatters: Formatters = {} if RPCEndpoint(method) in METHODS_TO_VALIDATE: w3_chain_id = await async_w3.eth.chain_id for method in METHODS_TO_VALIDATE: From 61942e67c2c8d94d153089f99441476d17c93cdc Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Tue, 13 May 2025 23:04:47 -0400 Subject: [PATCH 10/21] chore: fix lint errs --- web3/method.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/web3/method.py b/web3/method.py index f7be49b19d..21bf0f9b48 100644 --- a/web3/method.py +++ b/web3/method.py @@ -123,9 +123,7 @@ def __init__( self, json_rpc_method: Optional[RPCEndpoint] = None, mungers: Optional[Sequence[Munger]] = None, - request_formatters: Optional[ - Callable[[RPCEndpoint], Formatter[Any]] - ] = None, + request_formatters: Optional[Callable[[RPCEndpoint], Formatter[Any]]] = None, result_formatters: Optional[ Callable[[RPCEndpoint, "Module"], ResponseFormatter[TReturn]] ] = None, @@ -165,7 +163,7 @@ def __get__( f"Method `{self.json_rpc_method}` is not supported within a batch " "request." ) - return module.retrieve_request_information(self) + return module.retrieve_request_information(self) # type: ignore [return-value] else: return module.retrieve_caller_fn(self) From 86ae1be37561eb16974993f2b8baffc9f9c5d3ac Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Tue, 13 May 2025 23:12:41 -0400 Subject: [PATCH 11/21] chore: fix line length err --- web3/_utils/method_formatters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web3/_utils/method_formatters.py b/web3/_utils/method_formatters.py index 50cc22968e..5103b6b827 100644 --- a/web3/_utils/method_formatters.py +++ b/web3/_utils/method_formatters.py @@ -172,7 +172,7 @@ def type_aware_apply_formatters_to_dict( value = value.model_dump(by_alias=True) formatted: Dict[str, Any] - formatted = apply_formatters_to_dict(formatters, value) + formatted = apply_formatters_to_dict(formatters, value) # type: ignore [arg-type] return AttributeDict.recursive(formatted) if is_attrdict(value) else formatted From 29758f58beb4cfe498aad8925e56b53627a8a95e Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Tue, 13 May 2025 23:14:15 -0400 Subject: [PATCH 12/21] chore: fix line length err --- web3/module.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/web3/module.py b/web3/module.py index cd4c95766f..4689e6bc63 100644 --- a/web3/module.py +++ b/web3/module.py @@ -205,8 +205,10 @@ def __init__(self, w3: Union["AsyncWeb3", "Web3"]) -> None: self.retrieve_caller_fn = retrieve_async_method_call_fn(w3, self) else: self.retrieve_caller_fn = retrieve_blocking_method_call_fn(w3, self) - self.retrieve_request_information = retrieve_request_information_for_batching( # type: ignore [call-overload] - w3, self + self.retrieve_request_information = ( + retrieve_request_information_for_batching( # type: ignore [call-overload] + w3, self + ) ) self.w3 = w3 From f6ed29d876a7f45dac729690896086416a3d03e2 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Wed, 14 May 2025 02:56:48 -0400 Subject: [PATCH 13/21] Delete newsfragments/3696.performance.rst --- newsfragments/3696.performance.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 newsfragments/3696.performance.rst diff --git a/newsfragments/3696.performance.rst b/newsfragments/3696.performance.rst deleted file mode 100644 index 34323f3336..0000000000 --- a/newsfragments/3696.performance.rst +++ /dev/null @@ -1 +0,0 @@ -cache Method.method_selector_function \ No newline at end of file From 4cec8463cb0594ac4a435139a4c2bba1cf13aeb2 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Wed, 14 May 2025 03:28:27 -0400 Subject: [PATCH 14/21] Update module.py --- web3/module.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/web3/module.py b/web3/module.py index 4689e6bc63..d65463b335 100644 --- a/web3/module.py +++ b/web3/module.py @@ -89,7 +89,6 @@ def retrieve_request_information_for_batching( async def async_inner( *args: Any, **kwargs: Any ) -> Tuple[RequestArgs, ResponseFormatters[Any]]: - response_formatters: ResponseFormatters[Any] (method_str, params), response_formatters = method.process_params( module, *args, **kwargs ) @@ -100,7 +99,6 @@ async def async_inner( return (method_str, params), response_formatters def inner(*args: Any, **kwargs: Any) -> Tuple[RequestArgs, ResponseFormatters[Any]]: - response_formatters: ResponseFormatters[Any] (method_str, params), response_formatters = method.process_params( module, *args, **kwargs ) @@ -116,8 +114,6 @@ def retrieve_blocking_method_call_fn( method: Method[Callable[..., TReturn]], ) -> Callable[..., Union[TReturn, LogFilter]]: def caller(*args: Any, **kwargs: Any) -> Union[TReturn, LogFilter]: - response_formatters: ResponseFormatters[Any] - try: (method_str, params), response_formatters = method.process_params( module, *args, **kwargs @@ -154,8 +150,6 @@ def retrieve_async_method_call_fn( async def caller( *args: Any, **kwargs: Any ) -> Union[RPCResponse, FormattedEthSubscriptionResponse, AsyncLogFilter]: - response_formatters: ResponseFormatters[Any] - try: (method_str, params), response_formatters = method.process_params( module, *args, **kwargs From 92791219a144e24ca77a6f8a5ac4301a89142b5d Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Wed, 14 May 2025 03:30:07 -0400 Subject: [PATCH 15/21] Update method.py --- web3/method.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web3/method.py b/web3/method.py index 21bf0f9b48..18e3fb2535 100644 --- a/web3/method.py +++ b/web3/method.py @@ -49,7 +49,7 @@ Munger = Callable[..., Any] RequestArgs = Tuple[RPCEndpoint, Sequence[Any]] ResponseFormatters = Tuple[ - ResponseFormatter[TReturn], ResponseFormatter[TReturn], ResponseFormatter[TReturn] + ResponseFormatter[Any], ResponseFormatter[Any], ResponseFormatter[Any] ] @@ -193,7 +193,7 @@ def input_munger(self, module: "Module", args: Any, kwargs: Any) -> List[Any]: def process_params( self, module: "Module", *args: Any, **kwargs: Any - ) -> Tuple[RequestArgs, ResponseFormatters[TReturn]]: + ) -> Tuple[RequestArgs, ResponseFormatters]: params = self.input_munger(module, args, kwargs) if self.method_choice_depends_on_args: From 58ab29a683bd45ee00b8ad3a498065d7b0bf139b Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Wed, 14 May 2025 03:31:32 -0400 Subject: [PATCH 16/21] Update module.py --- web3/module.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/web3/module.py b/web3/module.py index d65463b335..318d8a1304 100644 --- a/web3/module.py +++ b/web3/module.py @@ -64,7 +64,7 @@ def retrieve_request_information_for_batching( w3: "AsyncWeb3", module: "Module", method: Method[Callable[..., Any]], -) -> Callable[..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters[Any]]]]: +) -> Callable[..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters]]]: ... @@ -73,7 +73,7 @@ def retrieve_request_information_for_batching( w3: "Web3", module: "Module", method: Method[Callable[..., Any]], -) -> Callable[..., Tuple[RequestArgs, ResponseFormatters[Any]]]: +) -> Callable[..., Tuple[RequestArgs, ResponseFormatters]]: ... @@ -83,12 +83,12 @@ def retrieve_request_information_for_batching( module: "Module", method: Method[Callable[..., Any]], ) -> Union[ - Callable[..., Tuple[RequestArgs, ResponseFormatters[Any]]], - Callable[..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters[Any]]]], + Callable[..., Tuple[RequestArgs, ResponseFormatters]], + Callable[..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters]]], ]: async def async_inner( *args: Any, **kwargs: Any - ) -> Tuple[RequestArgs, ResponseFormatters[Any]]: + ) -> Tuple[RequestArgs, ResponseFormatters]: (method_str, params), response_formatters = method.process_params( module, *args, **kwargs ) @@ -98,7 +98,7 @@ async def async_inner( ) return (method_str, params), response_formatters - def inner(*args: Any, **kwargs: Any) -> Tuple[RequestArgs, ResponseFormatters[Any]]: + def inner(*args: Any, **kwargs: Any) -> Tuple[RequestArgs, ResponseFormatters]: (method_str, params), response_formatters = method.process_params( module, *args, **kwargs ) @@ -187,9 +187,9 @@ class Module: retrieve_request_information: Callable[ [Method[TFunc]], Union[ - Callable[..., Tuple[RequestArgs, ResponseFormatters[Any]]], + Callable[..., Tuple[RequestArgs, ResponseFormatters]], Callable[ - ..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters[Any]]] + ..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters]] ], ], ] From 1ca371bc0c14bff660c3d137c2e3bb30959675a5 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Fri, 16 May 2025 19:04:51 -0400 Subject: [PATCH 17/21] Update method.py --- web3/method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web3/method.py b/web3/method.py index 276acda503..fa17b3e7e6 100644 --- a/web3/method.py +++ b/web3/method.py @@ -170,7 +170,7 @@ def __get__( def __call__(self, *args: Any, **kwargs: Any) -> Any: return self.__get__(self._module)(*args, **kwargs) - @functools.cached_property + @property def method_selector_fn( self, ) -> Callable[[], RPCEndpoint]: From 4124617589ac613d08459265bbf6361030847f35 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Fri, 16 May 2025 19:10:27 -0400 Subject: [PATCH 18/21] Update manager.py --- web3/manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web3/manager.py b/web3/manager.py index 8ee4a78841..4e95293094 100644 --- a/web3/manager.py +++ b/web3/manager.py @@ -360,7 +360,7 @@ async def socket_request( self, method: RPCEndpoint, params: Any, - response_formatters: Optional[ResponseFormatters[Any]] = None, + response_formatters: Optional[ResponseFormatters] = None, ) -> RPCResponse: provider = cast(PersistentConnectionProvider, self._provider) self.logger.debug( From 0d88f8f9c08089b3f2641c9a6fd483f2278f9250 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Fri, 16 May 2025 21:15:06 -0400 Subject: [PATCH 19/21] Update test_method.py --- tests/core/method-class/test_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core/method-class/test_method.py b/tests/core/method-class/test_method.py index 88766d9d09..d9671ab24c 100644 --- a/tests/core/method-class/test_method.py +++ b/tests/core/method-class/test_method.py @@ -76,7 +76,7 @@ def test_get_formatters_non_falsy_config_retrieval(): method_name = method.method_selector_fn() first_formatter = (method.request_formatters(method_name).first,) all_other_formatters = method.request_formatters(method_name).funcs - assert len(first_formatter + all_other_formatters) == 2 + assert len(first_formatter + all_other_formatters) == 3 assert (method.request_formatters("eth_getBalance").first,) == first_formatter From 57059cba606161668b203688635827ecc4e586ed Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Mon, 19 May 2025 17:29:02 -0400 Subject: [PATCH 20/21] Update method.py --- web3/method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web3/method.py b/web3/method.py index fa17b3e7e6..8336b4a0c0 100644 --- a/web3/method.py +++ b/web3/method.py @@ -123,7 +123,7 @@ def __init__( self, json_rpc_method: Optional[RPCEndpoint] = None, mungers: Optional[Sequence[Munger]] = None, - request_formatters: Optional[Callable[[RPCEndpoint], Formatter[Any]]] = None, + request_formatters: Optional[Callable[[RPCEndpoint], Formatter[TReturn]]] = None, result_formatters: Optional[ Callable[[RPCEndpoint, "Module"], ResponseFormatter[TReturn]] ] = None, From 1ad67bf2ef1b12ca5f097262c3b090e703884631 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Mon, 19 May 2025 21:40:43 +0000 Subject: [PATCH 21/21] feat: RequestAndFormatters type --- web3/method.py | 3 ++- web3/module.py | 22 +++++++++------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/web3/method.py b/web3/method.py index fa17b3e7e6..487f8a532c 100644 --- a/web3/method.py +++ b/web3/method.py @@ -51,6 +51,7 @@ ResponseFormatters = Tuple[ ResponseFormatter[Any], ResponseFormatter[Any], ResponseFormatter[Any] ] +RequestAndFormatters = Tuple[RequestArgs, ResponseFormatters] def _set_mungers( @@ -194,7 +195,7 @@ def input_munger(self, module: "Module", args: Any, kwargs: Any) -> List[Any]: def process_params( self, module: "Module", *args: Any, **kwargs: Any - ) -> Tuple[RequestArgs, ResponseFormatters]: + ) -> RequestAndFormatters: params = self.input_munger(module, args, kwargs) if self.method_choice_depends_on_args: diff --git a/web3/module.py b/web3/module.py index 318d8a1304..b259d7402b 100644 --- a/web3/module.py +++ b/web3/module.py @@ -5,7 +5,6 @@ Coroutine, Dict, Optional, - Tuple, TypeVar, Union, overload, @@ -26,8 +25,7 @@ ) from web3.method import ( Method, - RequestArgs, - ResponseFormatters, + RequestAndFormatters, ) from web3.providers.persistent import ( PersistentConnectionProvider, @@ -64,7 +62,7 @@ def retrieve_request_information_for_batching( w3: "AsyncWeb3", module: "Module", method: Method[Callable[..., Any]], -) -> Callable[..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters]]]: +) -> Callable[..., Coroutine[Any, Any, RequestAndFormatters]]: ... @@ -73,7 +71,7 @@ def retrieve_request_information_for_batching( w3: "Web3", module: "Module", method: Method[Callable[..., Any]], -) -> Callable[..., Tuple[RequestArgs, ResponseFormatters]]: +) -> Callable[..., RequestAndFormatters]: ... @@ -83,12 +81,12 @@ def retrieve_request_information_for_batching( module: "Module", method: Method[Callable[..., Any]], ) -> Union[ - Callable[..., Tuple[RequestArgs, ResponseFormatters]], - Callable[..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters]]], + Callable[..., RequestAndFormatters], + Callable[..., Coroutine[Any, Any, RequestAndFormatters]], ]: async def async_inner( *args: Any, **kwargs: Any - ) -> Tuple[RequestArgs, ResponseFormatters]: + ) -> RequestAndFormatters: (method_str, params), response_formatters = method.process_params( module, *args, **kwargs ) @@ -98,7 +96,7 @@ async def async_inner( ) return (method_str, params), response_formatters - def inner(*args: Any, **kwargs: Any) -> Tuple[RequestArgs, ResponseFormatters]: + def inner(*args: Any, **kwargs: Any) -> RequestAndFormatters: (method_str, params), response_formatters = method.process_params( module, *args, **kwargs ) @@ -187,10 +185,8 @@ class Module: retrieve_request_information: Callable[ [Method[TFunc]], Union[ - Callable[..., Tuple[RequestArgs, ResponseFormatters]], - Callable[ - ..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters]] - ], + Callable[..., RequestAndFormatters], + Callable[..., Coroutine[Any, Any, RequestAndFormatters]], ], ]