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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
31 changes: 14 additions & 17 deletions web3/_utils/method_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
BlockIdentifier,
Formatters,
RPCEndpoint,
RPCResponse,
SimulateV1Payload,
StateOverrideParams,
TReturn,
Expand Down Expand Up @@ -600,19 +601,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 @@ -1190,7 +1187,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 @@ -1201,7 +1198,7 @@ def apply_module_to_formatters(
def get_result_formatters(
method_name: Union[RPCEndpoint, Callable[..., 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 @@ -1214,7 +1211,7 @@ def get_result_formatters(

def get_error_formatters(
method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is method_name actually supposed to be a Union[RPCEndpoint, Callable[..., RPCEndpoint]]?

I notice Union[RPCEndpoint, Callable[..., RPCEndpoint]] is used throughout the codebase but the code itself seems to assume method_name is always a string value, never callable. Should this be updated as well?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, nice catch. Yeah, these can only be RPCEndpoints once they're here.

) -> Callable[..., Any]:
) -> 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 @@ -1224,7 +1221,7 @@ def get_error_formatters(

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

return compose(*formatters)