Skip to content

fix(types): update type hints in web3._utils.method_formatters.py #3669

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

Merged
merged 2 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3669.internal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update some types in ``web3._utils.method_formatters``
41 changes: 17 additions & 24 deletions web3/_utils/method_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
BlockIdentifier,
Formatters,
RPCEndpoint,
RPCResponse,
SimulateV1Payload,
StateOverrideParams,
TReturn,
Expand Down Expand Up @@ -650,19 +651,15 @@ def storage_key_to_hexstr(value: Union[bytes, int, str]) -> HexStr:
)

block_result_formatters_copy = BLOCK_RESULT_FORMATTERS.copy()
block_result_formatters_copy.update(
{
"calls": apply_list_to_array_formatter(
type_aware_apply_formatters_to_dict(
{
"returnData": HexBytes,
"logs": apply_list_to_array_formatter(log_entry_formatter),
"gasUsed": to_integer_if_hex,
"status": to_integer_if_hex,
}
)
)
}
block_result_formatters_copy["calls"] = apply_list_to_array_formatter(
type_aware_apply_formatters_to_dict(
{
"returnData": HexBytes,
"logs": apply_list_to_array_formatter(log_entry_formatter),
"gasUsed": to_integer_if_hex,
"status": to_integer_if_hex,
}
)
)
simulate_v1_result_formatter = apply_formatter_if(
is_not_null,
Expand Down Expand Up @@ -1107,9 +1104,7 @@ def combine_formatters(
yield formatter_map[method_name]


def get_request_formatters(
method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]],
) -> Dict[str, Callable[..., Any]]:
def get_request_formatters(method_name: RPCEndpoint) -> Callable[[RPCResponse], Any]:
request_formatter_maps = (
ABI_REQUEST_FORMATTERS,
# METHOD_NORMALIZERS needs to be after ABI_REQUEST_FORMATTERS
Expand Down Expand Up @@ -1241,7 +1236,7 @@ def filter_wrapper(

@to_tuple
def apply_module_to_formatters(
formatters: Tuple[Callable[..., TReturn]],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious about the reasoning behind this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change just indicates to mypy that the function will work with any iterable of callables.

The original hint indicates that the function must take a tuple input, so while it didn't cause any type errors internally here it can cause unneeded type errors for downstream users.

formatters: Iterable[Callable[..., TReturn]],
module: "Module",
method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]],
) -> Iterable[Callable[..., TReturn]]:
Expand All @@ -1250,9 +1245,9 @@ def apply_module_to_formatters(


def get_result_formatters(
method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]],
method_name: RPCEndpoint,
module: "Module",
) -> Dict[str, Callable[..., Any]]:
) -> Callable[[RPCResponse], Any]:
formatters = combine_formatters((PYTHONIC_RESULT_FORMATTERS,), method_name)
formatters_requiring_module = combine_formatters(
(FILTER_RESULT_FORMATTERS,), method_name
Expand All @@ -1263,9 +1258,7 @@ def get_result_formatters(
return compose(*partial_formatters, *formatters)


def get_error_formatters(
method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]],
) -> Callable[..., Any]:
def get_error_formatters(method_name: RPCEndpoint) -> Callable[[RPCResponse], Any]:
# Note error formatters work on the full response dict
error_formatter_maps = (ERROR_FORMATTERS,)
formatters = combine_formatters(error_formatter_maps, method_name)
Expand All @@ -1274,8 +1267,8 @@ def get_error_formatters(


def get_null_result_formatters(
method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]],
) -> Callable[..., Any]:
method_name: RPCEndpoint,
) -> Callable[[RPCResponse], Any]:
formatters = combine_formatters((NULL_RESULT_FORMATTERS,), method_name)

return compose(*formatters)
2 changes: 1 addition & 1 deletion web3/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any:
@property
def method_selector_fn(
self,
) -> Callable[..., Union[RPCEndpoint, Callable[..., RPCEndpoint]]]:
) -> Callable[..., RPCEndpoint]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌🏼

"""Gets the method selector from the config."""
if callable(self.json_rpc_method):
return self.json_rpc_method
Expand Down