1
- from collections .abc import (
2
- Mapping ,
3
- )
4
1
from typing import (
5
2
Any ,
6
3
Callable ,
7
4
Dict ,
8
5
Iterable ,
6
+ Iterator ,
7
+ Mapping ,
9
8
Optional ,
10
9
Tuple ,
11
10
TypeVar ,
11
+ overload ,
12
12
)
13
13
14
14
from eth_typing import (
38
38
)
39
39
40
40
TReturn = TypeVar ("TReturn" )
41
+ TKey = TypeVar ("TKey" )
41
42
TValue = TypeVar ("TValue" )
42
43
43
44
@@ -59,18 +60,73 @@ def apply_formatters_to_args(
59
60
)
60
61
61
62
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
+
62
121
def map_collection (func : Callable [..., TReturn ], collection : Any ) -> Any :
63
122
"""
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.
66
125
"""
67
- datatype = type (collection )
68
126
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]
74
130
else :
75
131
return collection
76
132
0 commit comments