Skip to content

Commit ee5190b

Browse files
committed
feat: optimize map_collection
1 parent 4111474 commit ee5190b

File tree

1 file changed

+67
-11
lines changed

1 file changed

+67
-11
lines changed

web3/_utils/formatters.py

+67-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from collections.abc import (
2-
Mapping,
3-
)
41
from typing import (
52
Any,
63
Callable,
74
Dict,
85
Iterable,
6+
Iterator,
7+
Mapping,
98
Optional,
109
Tuple,
1110
TypeVar,
11+
overload,
1212
)
1313

1414
from eth_typing import (
@@ -38,6 +38,7 @@
3838
)
3939

4040
TReturn = TypeVar("TReturn")
41+
TKey = TypeVar("TKey")
4142
TValue = TypeVar("TValue")
4243

4344

@@ -59,18 +60,73 @@ def apply_formatters_to_args(
5960
)
6061

6162

63+
@overload
64+
def map_collection(
65+
func: Callable[[TValue], TReturn], mapping: Mapping[TKey, TValue]
66+
) -> Mapping[TKey, TReturn]:
67+
"""
68+
Apply `func` to each value of a mapping.
69+
If `collection` is not a collection, return it unmodified.
70+
"""
71+
72+
73+
@overload
74+
def map_collection(func: Callable[..., TReturn], collection: str) -> str:
75+
"""
76+
Return `collection` unmodified, since it is not a collection.
77+
"""
78+
79+
80+
@overload
81+
def map_collection(
82+
func: Callable[[TValue], TReturn], iterable: "map[TValue]"
83+
) -> "map[TReturn]":
84+
"""
85+
Apply `func` to each element of a map.
86+
"""
87+
88+
89+
@overload
90+
def map_collection(
91+
func: Callable[[TValue], TReturn], iterable: Iterator[TValue]
92+
) -> Iterator[TReturn]:
93+
"""
94+
Apply `func` to each element of an iteratol.
95+
"""
96+
97+
98+
@overload
99+
def map_collection(
100+
func: Callable[[TValue], TReturn], iterable: Iterable[TValue]
101+
) -> Iterable[TReturn]:
102+
"""
103+
Apply `func` to each element of an iterable.
104+
"""
105+
106+
107+
@overload
108+
def map_collection(
109+
func: Callable[[TValue], TReturn], collection: TValue
110+
) -> TValue:
111+
"""
112+
Return `collection` unmodified, since it is not a collection.
113+
"""
114+
115+
116+
@overload
117+
def map_collection(func: Callable[..., TReturn], collection: Any) -> Any:
118+
...
119+
120+
62121
def map_collection(func: Callable[..., TReturn], collection: Any) -> Any:
63122
"""
64-
Apply func to each element of a collection, or value of a dictionary.
65-
If the value is not a collection, return it unmodified
123+
Apply `func` to each element of a collection, or value of a mapping.
124+
If `collection` is not a collection, return it unmodified.
66125
"""
67-
datatype = type(collection)
68126
if isinstance(collection, Mapping):
69-
return datatype((key, func(val)) for key, val in collection.items())
70-
if is_string(collection):
71-
return collection
72-
elif isinstance(collection, Iterable):
73-
return datatype(map(func, collection))
127+
return type(collection)(zip(collection.keys(), map(func, collection.values()))) # type: ignore[call-arg]
128+
elif not is_string(collection) and isinstance(collection, Iterable):
129+
return type(collection)(map(func, collection)) # type: ignore[call-arg]
74130
else:
75131
return collection
76132

0 commit comments

Comments
 (0)