typing: add type hints to pycompat.maplist()
authorMatt Harbison <matt_harbison@yahoo.com>
Tue, 21 Feb 2023 13:24:12 -0500
changeset 50175 0ab92dabea6e
parent 50174 596a6b9b0570
child 50176 829aa604d71a
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.
mercurial/pycompat.py
--- a/mercurial/pycompat.py	Wed Feb 22 18:42:09 2023 +0100
+++ b/mercurial/pycompat.py	Tue Feb 21 13:24:12 2023 -0500
@@ -32,6 +32,7 @@
     Any,
     AnyStr,
     BinaryIO,
+    Callable,
     Dict,
     Iterable,
     Iterator,
@@ -58,6 +59,8 @@
 
 _GetOptResult = Tuple[List[Tuple[bytes, bytes]], List[bytes]]
 _T0 = TypeVar('_T0')
+_T1 = TypeVar('_T1')
+_S = TypeVar('_S')
 _Tbytestr = TypeVar('_Tbytestr', bound='bytestr')
 
 
@@ -129,8 +132,21 @@
 sysexecutable: bytes = os.fsencode(sys.executable) if sys.executable else b''
 
 
-def maplist(*args):
-    return list(map(*args))
+if TYPE_CHECKING:
+
+    @overload
+    def maplist(f: Callable[[_T0], _S], arg: Iterable[_T0]) -> List[_S]:
+        ...
+
+    @overload
+    def maplist(
+        f: Callable[[_T0, _T1], _S], arg1: Iterable[_T0], arg2: Iterable[_T1]
+    ) -> List[_S]:
+        ...
+
+
+def maplist(f, *args):
+    return list(map(f, *args))
 
 
 def rangelist(*args):