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 7 commits into
base: main
Choose a base branch
from

Conversation

BobTheBuidler
Copy link
Contributor

@BobTheBuidler BobTheBuidler commented Apr 17, 2025

What was wrong?

get_result_formatters had return type Dict[str, Callable[..., Any]] which is incorrect because the function returns a callable toolz.compose object

get_error_formatters, and get_null_result_formatters both had return type Callable[..., Any] which is techincally correct but could be made more specific for clarity for contributors.

Related to Issue #N/A
Closes #N/A

How was it fixed?

I've changed the return type for all 3 functions to Callable[[RPCResponse], Any].

Todo:

  • Clean up commit history
  • Add or update documentation related to these changes

I did not add an entry to release notes as this change is not relevant for users.

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@@ -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.

@BobTheBuidler BobTheBuidler marked this pull request as ready for review April 17, 2025 21:07
@BobTheBuidler
Copy link
Contributor Author

Now that you've merged #3665 and #3666 I rebased my branch on master and reran the tests, looks like everything passes now

@@ -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.

@@ -1214,7 +1211,7 @@ def get_result_formatters(

def get_error_formatters(
method_name: Union[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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants