diff --git a/eth_utils/applicators.py b/eth_utils/applicators.py index 9bfd5939..5cb3e91f 100644 --- a/eth_utils/applicators.py +++ b/eth_utils/applicators.py @@ -28,18 +28,19 @@ @return_arg_type(2) def apply_formatter_at_index( - formatter: Callable[..., Any], at_index: int, value: List[Any] -) -> Generator[List[Any], None, None]: - if at_index + 1 > len(value): + formatter: Callable[..., Any], at_index: int, value: Sequence[Any] +) -> Generator[Any, None, None]: + try: + item = value[at_index] + except IndexError: raise IndexError( f"Not enough values in iterable to apply formatter. Got: {len(value)}. " f"Need: {at_index + 1}" - ) - for index, item in enumerate(value): - if index == at_index: - yield formatter(item) - else: - yield item + ) from None + + yield from value[:at_index] + yield formatter(item) + yield from value[at_index + 1 :] def combine_argument_formatters(*formatters: List[Callable[..., Any]]) -> Formatters: diff --git a/newsfragments/306.performance.rst b/newsfragments/306.performance.rst new file mode 100644 index 00000000..1b08a297 --- /dev/null +++ b/newsfragments/306.performance.rst @@ -0,0 +1 @@ +optimize apply_formatter_at_index