types-futures

3.3.8 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates using `concurrent.futures.ThreadPoolExecutor` with type hints. By installing `types-futures`, type checkers will correctly interpret the types of `Future` objects and executor methods, allowing for static analysis of your concurrent code.

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()

view raw JSON →