Skip to content

feat: cache error and null result formatters #3703

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 2 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 21 additions & 6 deletions web3/_utils/method_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@

TValue = TypeVar("TValue")

CachedFormatters = Dict[RPCEndpoint, Callable[[RPCResponse], Any]]


def bytes_to_ascii(value: bytes) -> str:
return codecs.decode(value, "ascii")
Expand Down Expand Up @@ -1258,17 +1260,30 @@ def get_result_formatters(
return compose(*partial_formatters, *formatters)


_error_formatters: CachedFormatters = {}


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)
try:
return _error_formatters[method_name]
except KeyError:
formatters = _error_formatters[method_name] = compose(
*combine_formatters((ERROR_FORMATTERS,), method_name)
)
return formatters

return compose(*formatters)

_null_result_formatters: CachedFormatters = {}


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

return compose(*formatters)
try:
return _null_result_formatters[method_name]
except KeyError:
formatters = _null_result_formatters[method_name] = compose(
*combine_formatters((NULL_RESULT_FORMATTERS,), method_name)
)
return formatters