Trio Typing

0.10.0 · active · verified Sat Apr 11

trio-typing provides static type checking support for Trio and related projects. It offers PEP 561 typing stubs for `trio`, `outcome`, and `async_generator`, along with a `mypy` plugin to enhance type checking capabilities for asynchronous code. The current version is 0.10.0, and it follows the release cadence of the broader Trio ecosystem.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `trio_typing.TaskStatus` for type-hinting tasks started with `trio.Nursery.start()`. It also implicitly shows how `trio.Nursery` is imported directly from `trio`. To leverage the full type-checking power, ensure you install `trio-typing[mypy]` and configure the plugin in your `mypy.ini`.

import trio
from trio_typing import TaskStatus
from typing import TypeVar

T = TypeVar("T")

async def child_task(value: T, *, task_status: TaskStatus[T]) -> None:
    """An asynchronous task that signals its start status."""
    print(f"Child task received: {value}")
    task_status.started(f"Processed: {value}") # Type-checked by trio-typing
    await trio.sleep(0.1)

async def main() -> None:
    """Main function demonstrating nursery.start with type hints."""
    print("Starting main...")
    async with trio.open_nursery() as nursery:
        # mypy will check that 'child_task' matches the expected signature for nursery.start
        result = await nursery.start(child_task, "hello")
        print(f"Nursery start returned: {result}")
    print("Main finished.")

if __name__ == "__main__":
    # To enable mypy checks, ensure you have mypy installed with trio-typing[mypy]
    # and 'plugins = trio_typing.plugin' in your mypy.ini file.
    # Example mypy.ini:
    # [mypy]
    # plugins = trio_typing.plugin
    # 
    # Then run: mypy your_script_name.py
    trio.run(main)

view raw JSON →