types-futures
types-futures provides static type annotations (stubs) for the `concurrent.futures` module, which is part of Python's standard library. It enables type checkers like MyPy, Pyright, or PyCharm to perform static analysis and provide type hints for code utilizing thread and process pools. This package is maintained by the Typeshed project and is released automatically to PyPI, often daily, to keep up with changes in the runtime library.
Warnings
- gotcha Do not confuse `types-futures` with the `futures` PyPI package. The `futures` package is a Python 2 backport of `concurrent.futures` and is not compatible with Python 3. `types-futures` provides stubs for Python 3's built-in `concurrent.futures` module.
- breaking As `types-futures` is part of Typeshed, any version bump can introduce changes to the stubs that might cause your code to fail type checking, even if the runtime behavior remains the same. This is inherent to the nature of stub files aligning with potentially evolving APIs.
- gotcha The `asyncio.Future` and `concurrent.futures.Future` classes are distinct and incompatible. Code expecting one will not correctly process the other, leading to runtime errors or incorrect type checking. `asyncio.Future` instances are awaitable, whereas `concurrent.futures.Future` instances are not.
Install
-
pip install types-futures
Imports
- Future
from concurrent.futures import Future
- ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor
- ProcessPoolExecutor
from concurrent.futures import ProcessPoolExecutor
- as_completed
from concurrent.futures import as_completed
- wait
from concurrent.futures import wait
Quickstart
from concurrent.futures import ThreadPoolExecutor, Future
from typing import List
def long_running_task(n: int) -> int:
import time
time.sleep(0.1)
return n * n
def main() -> None:
with ThreadPoolExecutor(max_workers=5) as executor:
futures: List[Future[int]] = [
executor.submit(long_running_task, i) for i in range(10)
]
results: List[int] = [f.result() for f in futures]
print(f"Computed results: {results}")
if __name__ == '__main__':
main()