comparison mercurial/pycompat.py @ 50218:0ab92dabea6e

typing: add type hints to pycompat.maplist() The typeshed hints define 5 overloads with an increasing number of parameters on the passed function, and then a catchall that ignores the argument list on the passed function and allows an `*iterators` arg. All of our uses are fulfilled by the 1 function + 1 iterable overload, but add the second overload as a hint in case it's needed in the future.
author Matt Harbison <matt_harbison@yahoo.com>
date Tue, 21 Feb 2023 13:24:12 -0500
parents b900f40c343e
children 829aa604d71a
comparison
equal deleted inserted replaced
50217:596a6b9b0570 50218:0ab92dabea6e
30 30
31 from typing import ( 31 from typing import (
32 Any, 32 Any,
33 AnyStr, 33 AnyStr,
34 BinaryIO, 34 BinaryIO,
35 Callable,
35 Dict, 36 Dict,
36 Iterable, 37 Iterable,
37 Iterator, 38 Iterator,
38 List, 39 List,
39 Mapping, 40 Mapping,
56 57
57 TYPE_CHECKING = typing.TYPE_CHECKING 58 TYPE_CHECKING = typing.TYPE_CHECKING
58 59
59 _GetOptResult = Tuple[List[Tuple[bytes, bytes]], List[bytes]] 60 _GetOptResult = Tuple[List[Tuple[bytes, bytes]], List[bytes]]
60 _T0 = TypeVar('_T0') 61 _T0 = TypeVar('_T0')
62 _T1 = TypeVar('_T1')
63 _S = TypeVar('_S')
61 _Tbytestr = TypeVar('_Tbytestr', bound='bytestr') 64 _Tbytestr = TypeVar('_Tbytestr', bound='bytestr')
62 65
63 66
64 def future_set_exception_info(f, exc_info): 67 def future_set_exception_info(f, exc_info):
65 f.set_exception(exc_info[0]) 68 f.set_exception(exc_info[0])
127 130
128 sysplatform: bytes = sys.platform.encode('ascii') 131 sysplatform: bytes = sys.platform.encode('ascii')
129 sysexecutable: bytes = os.fsencode(sys.executable) if sys.executable else b'' 132 sysexecutable: bytes = os.fsencode(sys.executable) if sys.executable else b''
130 133
131 134
132 def maplist(*args): 135 if TYPE_CHECKING:
133 return list(map(*args)) 136
137 @overload
138 def maplist(f: Callable[[_T0], _S], arg: Iterable[_T0]) -> List[_S]:
139 ...
140
141 @overload
142 def maplist(
143 f: Callable[[_T0, _T1], _S], arg1: Iterable[_T0], arg2: Iterable[_T1]
144 ) -> List[_S]:
145 ...
146
147
148 def maplist(f, *args):
149 return list(map(f, *args))
134 150
135 151
136 def rangelist(*args): 152 def rangelist(*args):
137 return list(range(*args)) 153 return list(range(*args))
138 154