{"id":3842,"library":"types-networkx","title":"Typing Stubs for NetworkX","description":"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.","status":"active","version":"3.6.1.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","type-hints","networkx","typeshed","graph"],"install":[{"cmd":"pip install types-networkx","lang":"bash","label":"Install Stubs"},{"cmd":"pip install networkx","lang":"bash","label":"Install NetworkX (required runtime library)"}],"dependencies":[{"reason":"This package provides type stubs for the `networkx` library; `networkx` must be installed separately for runtime execution.","package":"networkx","optional":false}],"imports":[{"note":"`types-networkx` provides type information for `networkx` imports; you do not directly import from `types_networkx`.","symbol":"Graph","correct":"import networkx as nx\nmy_graph: nx.Graph[str] = nx.Graph()"},{"note":"`types-networkx` provides type information for `networkx` imports; you do not directly import from `types_networkx`.","symbol":"DiGraph","correct":"from networkx import DiGraph\nmy_digraph: DiGraph[int] = DiGraph()"}],"quickstart":{"code":"import networkx as nx\n\ndef create_and_analyze_graph(nodes: list[str], edges: list[tuple[str, str]]) -> tuple[nx.Graph[str], int]:\n    G: nx.Graph[str] = nx.Graph()\n    G.add_nodes_from(nodes)\n    G.add_edges_from(edges)\n    num_nodes: int = G.number_of_nodes()\n    return G, num_nodes\n\nif __name__ == '__main__':\n    nodes_data = ['A', 'B', 'C', 'D']\n    edges_data = [('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'A')]\n    \n    graph, count = create_and_analyze_graph(nodes_data, edges_data)\n    print(f\"Created graph with {count} nodes and {graph.number_of_edges()} edges.\")\n    # Example of type checking at work: attempting to add a non-string node would be flagged by a type checker\n    # graph.add_node(123) # This line would cause a type error with strict type checking\n","lang":"python","description":"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."},"warnings":[{"fix":"Install `types-networkx` with a version constraint matching your `networkx` installation, e.g., `pip install 'networkx==3.6.1' 'types-networkx==3.6.1.*'`.","message":"Ensure the `types-networkx` version aligns with your installed `networkx` version. Mismatched versions can lead to incorrect or incomplete type checking results, as the stubs might not reflect the exact API of the `networkx` library you are using.","severity":"gotcha","affected_versions":"All versions"},{"fix":"As a workaround for `TypeError` related to generic runtime types, users have reported adding `nx.DiGraph.__class_getitem__ = classmethod(lambda cls, item: cls)` after importing networkx. This makes the runtime class subscriptable for type hints without affecting behavior.","message":"Some `networkx` classes might be generic in stubs (e.g., `DiGraph[NodeT]`) but not at runtime, which can cause `TypeError: type 'DiGraph' is not subscriptable` when used with other libraries like Pydantic that inspect runtime types.","severity":"gotcha","affected_versions":"networkx <= 3.x"},{"fix":"Consult the `networkx` migration guides (e.g., from 1.x to 2.0, or 2.x to 3.0) for the underlying library. Update `networkx` code to adhere to the newer API.","message":"While `types-networkx` provides stubs for the `networkx` API, `networkx` itself has undergone significant breaking changes between major versions (e.g., 1.x to 2.0, and 2.x to 3.0), particularly regarding iterator vs. view behavior, and the replacement of `numpy.matrix` with `numpy.ndarray`. Type stubs will reflect the target `networkx` version's API, but old code written for earlier `networkx` versions may still break at runtime even with correct stubs if not migrated.","severity":"breaking","affected_versions":"networkx 1.x, 2.x (when migrating to newer 3.x with corresponding stubs)"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}