Typing Stubs for toposort

1.10.0.20260408 · active · verified Sat Apr 11

types-toposort provides type hints (typing stubs) for the `toposort` Python library, which implements a topological sort algorithm for directed acyclic graphs (DAGs). This stub package, currently at version 1.10.0.20260408, is part of the `typeshed` project and is released automatically, often daily, reflecting updates to the underlying typeshed definitions for `toposort`.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic usage of the `toposort` library for which `types-toposort` provides stubs. It shows how to define dependencies and then perform a layered or flattened topological sort. The input is a dictionary where keys represent dependent nodes, and values are sets of their direct dependencies.

from toposort import toposort, toposort_flatten

# Example graph where keys depend on values
dependencies = {
    2: {11},
    9: {11, 8, 10},
    10: {11, 3},
    11: {7, 5},
    8: {7, 3},
    3: set(), # 3 has no dependencies
    5: set(), # 5 has no dependencies
    7: set()  # 7 has no dependencies
}

# Perform a topological sort, returning sets of independent nodes at each level
layered_sort = toposort(dependencies)
print(f"Layered topological sort: {list(layered_sort)}")

# Perform a topological sort, returning a single flattened list
flattened_sort = toposort_flatten(dependencies)
print(f"Flattened topological sort: {list(flattened_sort)}")

view raw JSON →