Typing Stubs for NetworkX

3.6.1.20260408 · active · verified Sat Apr 11

Typing stubs for the `networkx` library. This package provides external type annotations, allowing static type checkers like MyPy and PyRight to analyze code that uses `networkx` for graph manipulation. It is an automatically generated component of the `typeshed` project and aims to provide accurate annotations for specific `networkx` versions. Stubs are released frequently, often daily, by typeshed's automated processes.

Warnings

Install

Imports

Quickstart

This example demonstrates basic `networkx` usage with type hints. When `types-networkx` is installed, a static type checker (like MyPy) will use these stubs to verify the types of variables and function signatures, catching potential errors at development time.

import networkx as nx

def create_and_analyze_graph(nodes: list[str], edges: list[tuple[str, str]]) -> tuple[nx.Graph[str], int]:
    G: nx.Graph[str] = nx.Graph()
    G.add_nodes_from(nodes)
    G.add_edges_from(edges)
    num_nodes: int = G.number_of_nodes()
    return G, num_nodes

if __name__ == '__main__':
    nodes_data = ['A', 'B', 'C', 'D']
    edges_data = [('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'A')]
    
    graph, count = create_and_analyze_graph(nodes_data, edges_data)
    print(f"Created graph with {count} nodes and {graph.number_of_edges()} edges.")
    # Example of type checking at work: attempting to add a non-string node would be flagged by a type checker
    # graph.add_node(123) # This line would cause a type error with strict type checking

view raw JSON →