Merge pull request #50 from alexwlchan/add-types
- ID
d3b2216- date
2025-04-01 14:25:28+00:00- author
Alex Chan <alex@alexwlchan.net>- parents
79bcfc6,bd4cbbe- message
Merge pull request #50 from alexwlchan/add-types Add types to `concurrently.py`- changed files
5 files, 27 additions, 15 deletions
Changed files
.github/workflows/main.yml (765) → .github/workflows/main.yml (824)
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index f82a14d..4cb30bd 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -32,6 +32,9 @@ jobs:
ruff check .
ruff format --check .
+ - name: Check types
+ run: mypy *.py --strict
+
- name: Run test suite
run: |
coverage run -m pytest test_concurrently.py
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``.
dev_requirements.in (16) → dev_requirements.in (21)
diff --git a/dev_requirements.in b/dev_requirements.in
index c6ec9f6..87ea621 100644
--- a/dev_requirements.in
+++ b/dev_requirements.in
@@ -1,2 +1,3 @@
+mypy
pytest-cov
ruff
dev_requirements.txt (505) → dev_requirements.txt (530)
diff --git a/dev_requirements.txt b/dev_requirements.txt
index c1dd334..90b20f1 100644
--- a/dev_requirements.txt
+++ b/dev_requirements.txt
@@ -1,22 +1,22 @@
# This file was autogenerated by uv via the following command:
# uv pip compile dev_requirements.in --output-file dev_requirements.txt
-coverage[toml]==7.5.1
+coverage==7.8.0
# via pytest-cov
-exceptiongroup==1.2.2
+iniconfig==2.1.0
# via pytest
-iniconfig==2.0.0
- # via pytest
-packaging==24.0
+mypy==1.15.0
+ # via -r dev_requirements.in
+mypy-extensions==1.0.0
+ # via mypy
+packaging==24.2
# via pytest
pluggy==1.5.0
# via pytest
-pytest==8.2.0
+pytest==8.3.5
# via pytest-cov
-pytest-cov==6.0.0
+pytest-cov==6.1.0
# via -r dev_requirements.in
ruff==0.11.2
# via -r dev_requirements.in
-tomli==2.2.1
- # via
- # coverage
- # pytest
+typing-extensions==4.13.0
+ # via mypy
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)}