Add type hints to the files
- ID
bd4cbbe- date
2025-04-01 14:23:23+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
84b291f- message
Add type hints to the files- changed files
2 files, 12 additions, 4 deletions
Changed files
concurrently.py (1327) → concurrently.py (1520)
diff --git a/concurrently.py b/concurrently.py
index 1c371e6..0737644 100644
--- a/concurrently.py
+++ b/concurrently.py
@@ -1,8 +1,16 @@
+from collections.abc import Callable, Iterable
import concurrent.futures
import itertools
+import typing
-def concurrently(handler, inputs, *, max_concurrency=5):
+In = typing.TypeVar("In")
+Out = typing.TypeVar("Out")
+
+
+def concurrently(
+ handler: Callable[[In], Out], inputs: Iterable[In], *, max_concurrency: int = 5
+) -> Iterable[tuple[In, Out]]:
"""
Calls the function ``handler`` on the values ``inputs``.
test_concurrently.py (620) → test_concurrently.py (648)
diff --git a/test_concurrently.py b/test_concurrently.py
index 81f5cb3..2927d27 100644
--- a/test_concurrently.py
+++ b/test_concurrently.py
@@ -4,12 +4,12 @@ import time
from concurrently import concurrently
-def double(x):
+def double(x: int) -> int:
time.sleep(random.random() / 100)
return x * 2
-def test_handles_iterator():
+def test_handles_iterator() -> None:
result = set(concurrently(handler=double, inputs=range(10)))
assert result == {
@@ -26,7 +26,7 @@ def test_handles_iterator():
}
-def test_handles_list():
+def test_handles_list() -> None:
result = set(concurrently(handler=double, inputs=[1, 3, 5, 7, 9, 11, 13]))
assert result == {(1, 2), (3, 6), (5, 10), (7, 14), (9, 18), (11, 22), (13, 26)}