Typing Stubs for gevent

26.4.0.20260409 · active · verified Sun Apr 12

types-gevent provides external type annotations (stubs) for the gevent library. It enables static type checkers like MyPy and Pyright to perform type checking, inference, and autocompletion for code that uses gevent. As part of the typeshed project, its releases are automatically generated and follow a versioning scheme tied to the gevent runtime package version.

Warnings

Install

Imports

Quickstart

This example demonstrates how types-gevent provides type hints for the gevent library. After installing both 'gevent' and 'types-gevent', a type checker like MyPy can analyze the code. The type hints for `fetch_data` and `process_urls` will be checked, and `gevent.spawn` and `gevent.joinall` will have their expected types available for static analysis.

import gevent
import gevent.monkey
import typing

gevent.monkey.patch_all()

def fetch_data(url: str) -> str:
    """Simulates fetching data asynchronously."""
    gevent.sleep(0.1) # Simulate I/O delay
    return f"Data from {url}"

def process_urls(urls: typing.List[str]) -> typing.List[str]:
    greenlets = [gevent.spawn(fetch_data, url) for url in urls]
    gevent.joinall(greenlets)
    results = [g.value for g in greenlets]
    return results

if __name__ == "__main__":
    urls_to_process = ["http://example.com/1", "http://example.com/2"]
    processed_data = process_urls(urls_to_process)
    print(f"Processed: {processed_data}")

# To run type checking, save this as e.g., 'main.py' and run:
# mypy main.py

view raw JSON →