From 0876bbd16826424f97e8e3f5d2daa42bdc63edd6 Mon Sep 17 00:00:00 2001 From: Yabir Benchakhtir Date: Sat, 13 Jun 2026 02:05:43 +0200 Subject: [PATCH] Avoid pattern of creating a variable that is read once --- web3/method.py | 5 ++--- web3/middleware/filter.py | 3 +-- web3/providers/base.py | 9 +++------ 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/web3/method.py b/web3/method.py index 511b68b8d0..e9ca2c48da 100644 --- a/web3/method.py +++ b/web3/method.py @@ -210,11 +210,10 @@ def process_params( # the first parameter determines which method needs to be called self.json_rpc_method = self.method_choice_depends_on_args(value=params[0]) - pending_or_latest_filter_methods = [ + if self.json_rpc_method in ( RPC.eth_newPendingTransactionFilter, RPC.eth_newBlockFilter, - ] - if self.json_rpc_method in pending_or_latest_filter_methods: + ): # For pending or latest filter methods, use params to determine # which method to call, but don't pass them through with the request params = [] diff --git a/web3/middleware/filter.py b/web3/middleware/filter.py index 3affbfcc26..cee69e7157 100644 --- a/web3/middleware/filter.py +++ b/web3/middleware/filter.py @@ -221,8 +221,7 @@ def get_logs_multipart( The getLog request is partitioned into multiple calls of the max number of blocks ``max_blocks``. """ - _block_ranges = block_ranges(start_block, stop_block, max_blocks) - for from_block, to_block in _block_ranges: + for from_block, to_block in block_ranges(start_block, stop_block, max_blocks): params = { "fromBlock": from_block, "toBlock": to_block, diff --git a/web3/providers/base.py b/web3/providers/base.py index 696352bb42..d24a5d2eb6 100644 --- a/web3/providers/base.py +++ b/web3/providers/base.py @@ -111,8 +111,7 @@ def request_func( """ middleware: tuple[Middleware, ...] = middleware_onion.as_tuple_of_middleware() - cache_key = self._request_func_cache[0] - if cache_key != middleware: + if self._request_func_cache[0] != middleware: self._request_func_cache = ( middleware, combine_middleware( @@ -184,15 +183,13 @@ def batch_request_func( ) -> Callable[..., list[RPCResponse] | RPCResponse]: middleware: tuple[Middleware, ...] = middleware_onion.as_tuple_of_middleware() - cache_key = self._batch_request_func_cache[0] - if cache_key != middleware: + if self._batch_request_func_cache[0] != middleware: accumulator_fn = self.make_batch_request for mw in reversed(middleware): - initialized = mw(w3) # type ignore bc in order to wrap the method, we have to call # `wrap_make_batch_request` with the accumulator_fn as the argument # which breaks the type hinting for this particular case. - accumulator_fn = initialized.wrap_make_batch_request( # type: ignore + accumulator_fn = mw(w3).wrap_make_batch_request( # type: ignore accumulator_fn ) self._batch_request_func_cache = (middleware, accumulator_fn)