Skip to content

Commit f4eb3c6

Browse files
committed
chore: refactor out _apply_request_formatters
1 parent 6000012 commit f4eb3c6

File tree

5 files changed

+73
-59
lines changed

5 files changed

+73
-59
lines changed

tests/core/method-class/test_method.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
)
1818
from web3.method import (
1919
Method,
20-
_apply_request_formatters,
2120
default_root_munger,
2221
)
2322
from web3.module import (
@@ -61,11 +60,7 @@ def test_get_formatters_default_formatter_for_falsy_config():
6160
default_result_formatters = method.result_formatters(
6261
method.method_selector_fn(), "some module"
6362
)
64-
assert _apply_request_formatters(["a", "b", "c"], default_request_formatters) == (
65-
"a",
66-
"b",
67-
"c",
68-
)
63+
assert default_request_formatters(["a", "b", "c"]) == ("a", "b", "c")
6964
assert apply_result_formatters(default_result_formatters, ["a", "b", "c"]) == [
7065
"a",
7166
"b",

web3/_utils/method_formatters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ def get_request_formatters(
10691069
PYTHONIC_REQUEST_FORMATTERS,
10701070
)
10711071
formatters = combine_formatters(request_formatter_maps, method_name)
1072-
return compose(*formatters)
1072+
return compose(tuple, *formatters)
10731073

10741074

10751075
def raise_block_not_found(params: Tuple[BlockIdentifier, bool]) -> NoReturn:

web3/manager.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
AsyncGenerator,
77
Callable,
88
Coroutine,
9-
Dict,
109
List,
1110
Optional,
1211
Sequence,
@@ -48,6 +47,7 @@
4847
)
4948
from web3.method import (
5049
Method,
50+
ResponseFormatters,
5151
)
5252
from web3.middleware import (
5353
AttributeDictMiddleware,
@@ -360,9 +360,7 @@ async def socket_request(
360360
self,
361361
method: RPCEndpoint,
362362
params: Any,
363-
response_formatters: Optional[
364-
Tuple[Dict[str, Callable[..., Any]], Callable[..., Any], Callable[..., Any]]
365-
] = None,
363+
response_formatters: Optional[ResponseFormatters[Any]] = None,
366364
) -> RPCResponse:
367365
provider = cast(PersistentConnectionProvider, self._provider)
368366
self.logger.debug(

web3/method.py

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,15 @@
33
TYPE_CHECKING,
44
Any,
55
Callable,
6-
Dict,
76
Generic,
87
List,
98
Optional,
109
Sequence,
1110
Tuple,
1211
Type,
13-
Union,
1412
)
1513
import warnings
1614

17-
from eth_utils.curried import (
18-
to_tuple,
19-
)
20-
from eth_utils.toolz import (
21-
pipe,
22-
)
23-
2415
from web3._utils.batching import (
2516
RPC_METHODS_UNSUPPORTED_DURING_BATCH,
2617
)
@@ -40,6 +31,8 @@
4031
Web3ValueError,
4132
)
4233
from web3.types import (
34+
RequestFormatter,
35+
ResponseFormatter,
4336
RPCEndpoint,
4437
TFunc,
4538
TReturn,
@@ -54,16 +47,10 @@
5447

5548

5649
Munger = Callable[..., Any]
57-
58-
59-
@to_tuple
60-
def _apply_request_formatters(
61-
params: Any, request_formatters: Dict[RPCEndpoint, Callable[..., TReturn]]
62-
) -> Tuple[Any, ...]:
63-
if request_formatters:
64-
formatted_params = pipe(params, request_formatters)
65-
return formatted_params
66-
return params
50+
RequestArgs = Tuple[RPCEndpoint, Sequence[Any]]
51+
ResponseFormatters = Tuple[
52+
ResponseFormatter[TReturn], ResponseFormatter[TReturn], ResponseFormatter[TReturn]
53+
]
6754

6855

6956
def _set_mungers(
@@ -136,9 +123,15 @@ def __init__(
136123
self,
137124
json_rpc_method: Optional[RPCEndpoint] = None,
138125
mungers: Optional[Sequence[Munger]] = None,
139-
request_formatters: Optional[Callable[..., TReturn]] = None,
140-
result_formatters: Optional[Callable[..., TReturn]] = None,
141-
null_result_formatters: Optional[Callable[..., TReturn]] = None,
126+
request_formatters: Optional[
127+
Callable[[RPCEndpoint], RequestFormatter[Any]]
128+
] = None,
129+
result_formatters: Optional[
130+
Callable[[RPCEndpoint, "Module"], ResponseFormatter[TReturn]]
131+
] = None,
132+
null_result_formatters: Optional[
133+
Callable[[RPCEndpoint], ResponseFormatter[TReturn]]
134+
] = None,
142135
method_choice_depends_on_args: Optional[Callable[..., RPCEndpoint]] = None,
143136
is_property: bool = False,
144137
):
@@ -202,14 +195,7 @@ def input_munger(self, module: "Module", args: Any, kwargs: Any) -> List[Any]:
202195

203196
def process_params(
204197
self, module: "Module", *args: Any, **kwargs: Any
205-
) -> Tuple[
206-
Tuple[Union[RPCEndpoint, Callable[..., RPCEndpoint]], Tuple[RPCEndpoint, ...]],
207-
Tuple[
208-
Union[TReturn, Dict[str, Callable[..., Any]]],
209-
Callable[..., Any],
210-
Union[TReturn, Callable[..., Any]],
211-
],
212-
]:
198+
) -> Tuple[RequestArgs, ResponseFormatters[TReturn]]:
213199
params = self.input_munger(module, args, kwargs)
214200

215201
if self.method_choice_depends_on_args:
@@ -232,10 +218,12 @@ def process_params(
232218
get_error_formatters(method),
233219
self.null_result_formatters(method),
234220
)
235-
request = (
236-
method,
237-
_apply_request_formatters(params, self.request_formatters(method)),
238-
)
221+
222+
if request_formatters := self.request_formatters(method):
223+
params = request_formatters(params)
224+
225+
request = method, params
226+
239227
return request, response_formatters
240228

241229

web3/module.py

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
Coroutine,
66
Dict,
77
Optional,
8-
Sequence,
98
Tuple,
109
TypeVar,
1110
Union,
12-
cast,
11+
overload,
1312
)
1413

1514
from eth_abi.codec import (
@@ -27,14 +26,16 @@
2726
)
2827
from web3.method import (
2928
Method,
29+
RequestArgs,
30+
ResponseFormatters,
3031
)
3132
from web3.providers.persistent import (
3233
PersistentConnectionProvider,
3334
)
3435
from web3.types import (
3536
FormattedEthSubscriptionResponse,
36-
RPCEndpoint,
3737
RPCResponse,
38+
TFunc,
3839
)
3940

4041
if TYPE_CHECKING:
@@ -58,34 +59,52 @@ def apply_result_formatters(
5859
TReturn = TypeVar("TReturn")
5960

6061

62+
@overload
63+
def retrieve_request_information_for_batching(
64+
w3: "AsyncWeb3",
65+
module: "Module",
66+
method: Method[Callable[..., Any]],
67+
) -> Callable[..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters[Any]]]]:
68+
...
69+
70+
71+
@overload
72+
def retrieve_request_information_for_batching(
73+
w3: "Web3",
74+
module: "Module",
75+
method: Method[Callable[..., Any]],
76+
) -> Callable[..., Tuple[RequestArgs, ResponseFormatters[Any]]]:
77+
...
78+
79+
6180
@curry
6281
def retrieve_request_information_for_batching(
6382
w3: Union["AsyncWeb3", "Web3"],
6483
module: "Module",
6584
method: Method[Callable[..., Any]],
6685
) -> Union[
67-
Callable[..., Tuple[Tuple[RPCEndpoint, Any], Sequence[Any]]],
68-
Callable[..., Coroutine[Any, Any, Tuple[Tuple[RPCEndpoint, Any], Sequence[Any]]]],
86+
Callable[..., Tuple[RequestArgs, ResponseFormatters[Any]]],
87+
Callable[..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters[Any]]]],
6988
]:
7089
async def async_inner(
7190
*args: Any, **kwargs: Any
72-
) -> Tuple[Tuple[RPCEndpoint, Any], Sequence[Any]]:
91+
) -> Tuple[RequestArgs, ResponseFormatters[Any]]:
92+
response_formatters: ResponseFormatters[Any]
7393
(method_str, params), response_formatters = method.process_params(
7494
module, *args, **kwargs
7595
)
7696
if isinstance(w3.provider, PersistentConnectionProvider):
7797
w3.provider._request_processor.cache_request_information(
78-
None, cast(RPCEndpoint, method_str), params, response_formatters
98+
None, method_str, params, response_formatters
7999
)
80-
return (cast(RPCEndpoint, method_str), params), response_formatters
100+
return (method_str, params), response_formatters
81101

82-
def inner(
83-
*args: Any, **kwargs: Any
84-
) -> Tuple[Tuple[RPCEndpoint, Any], Sequence[Any]]:
102+
def inner(*args: Any, **kwargs: Any) -> Tuple[RequestArgs, ResponseFormatters[Any]]:
103+
response_formatters: ResponseFormatters[Any]
85104
(method_str, params), response_formatters = method.process_params(
86105
module, *args, **kwargs
87106
)
88-
return (cast(RPCEndpoint, method_str), params), response_formatters
107+
return (method_str, params), response_formatters
89108

90109
return async_inner if module.is_async else inner
91110

@@ -97,6 +116,8 @@ def retrieve_blocking_method_call_fn(
97116
method: Method[Callable[..., TReturn]],
98117
) -> Callable[..., Union[TReturn, LogFilter]]:
99118
def caller(*args: Any, **kwargs: Any) -> Union[TReturn, LogFilter]:
119+
response_formatters: ResponseFormatters[Any]
120+
100121
try:
101122
(method_str, params), response_formatters = method.process_params(
102123
module, *args, **kwargs
@@ -133,6 +154,8 @@ def retrieve_async_method_call_fn(
133154
async def caller(
134155
*args: Any, **kwargs: Any
135156
) -> Union[RPCResponse, FormattedEthSubscriptionResponse, AsyncLogFilter]:
157+
response_formatters: ResponseFormatters[Any]
158+
136159
try:
137160
(method_str, params), response_formatters = method.process_params(
138161
module, *args, **kwargs
@@ -142,7 +165,7 @@ async def caller(
142165

143166
if isinstance(async_w3.provider, PersistentConnectionProvider):
144167
return await async_w3.manager.socket_request(
145-
cast(RPCEndpoint, method_str),
168+
method_str,
146169
params,
147170
response_formatters=response_formatters,
148171
)
@@ -167,12 +190,22 @@ async def caller(
167190
class Module:
168191
is_async = False
169192

193+
retrieve_request_information: Callable[
194+
[Method[TFunc]],
195+
Union[
196+
Callable[..., Tuple[RequestArgs, ResponseFormatters[Any]]],
197+
Callable[
198+
..., Coroutine[Any, Any, Tuple[RequestArgs, ResponseFormatters[Any]]]
199+
],
200+
],
201+
]
202+
170203
def __init__(self, w3: Union["AsyncWeb3", "Web3"]) -> None:
171204
if self.is_async:
172205
self.retrieve_caller_fn = retrieve_async_method_call_fn(w3, self)
173206
else:
174207
self.retrieve_caller_fn = retrieve_blocking_method_call_fn(w3, self)
175-
self.retrieve_request_information = retrieve_request_information_for_batching(
208+
self.retrieve_request_information = retrieve_request_information_for_batching( # type: ignore [call-overload]
176209
w3, self
177210
)
178211
self.w3 = w3

0 commit comments

Comments
 (0)