-
Notifications
You must be signed in to change notification settings - Fork 1.8k
chore: refactor out _apply_request_formatters #3670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BobTheBuidler
wants to merge
23
commits into
ethereum:main
Choose a base branch
from
BobTheBuidler:refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7992eb0
feat: cache Method.method_selector_fn
BobTheBuidler eed202e
chore: make formatter typing more specific
BobTheBuidler c2d2fec
feat: cache Method.method_selector_fn
BobTheBuidler 923de71
chore: refactor out _apply_request_formatters
BobTheBuidler c085ca2
Update method_formatters.py
BobTheBuidler 4288b3e
Update types.py
BobTheBuidler e8a001f
Update types.py
BobTheBuidler f2fb7db
Update method.py
BobTheBuidler a47dd82
Update validation.py
BobTheBuidler 61942e6
chore: fix lint errs
BobTheBuidler 86ae1be
chore: fix line length err
BobTheBuidler 29758f5
chore: fix line length err
BobTheBuidler f6ed29d
Delete newsfragments/3696.performance.rst
BobTheBuidler 4cec846
Update module.py
BobTheBuidler 9279121
Update method.py
BobTheBuidler 58ab29a
Update module.py
BobTheBuidler cd9aa91
Merge branch 'main' into refactor
BobTheBuidler 1ca371b
Update method.py
BobTheBuidler 4124617
Update manager.py
BobTheBuidler 0d88f8f
Update test_method.py
BobTheBuidler 57059cb
Update method.py
BobTheBuidler 1ad67bf
feat: RequestAndFormatters type
BobTheBuidler 71c9a93
Merge branch 'refactor' of https://github.com/BobTheBuidler/web3.py i…
BobTheBuidler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ( | ||
Formatter, | ||
ResponseFormatter, | ||
RPCEndpoint, | ||
TFunc, | ||
TReturn, | ||
|
@@ -54,16 +47,11 @@ | |
|
||
|
||
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[Any], ResponseFormatter[Any], ResponseFormatter[Any] | ||
] | ||
RequestAndFormatters = Tuple[RequestArgs, ResponseFormatters] | ||
|
||
|
||
def _set_mungers( | ||
|
@@ -136,9 +124,13 @@ 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], Formatter[TReturn]]] = 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, | ||
): | ||
|
@@ -166,13 +158,13 @@ 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 " | ||
"request." | ||
) | ||
return module.retrieve_request_information(self) | ||
return module.retrieve_request_information(self) # type: ignore [return-value] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not really sure how to deal with the line len err here |
||
else: | ||
return module.retrieve_caller_fn(self) | ||
|
||
|
@@ -203,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]], | ||
], | ||
]: | ||
) -> RequestAndFormatters: | ||
params = self.input_munger(module, args, kwargs) | ||
|
||
if self.method_choice_depends_on_args: | ||
|
@@ -233,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 | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
had to move this due to line len err