Registry / type-stubs / types-networkx

types-networkx

JSON →
library3.6.1.20260728pypypi✓ verified 24d ago

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.

pip install types-networkx
INSTALL
IMPORT
SIG · TYPES-NETWORKX
T
types-networkx
type-stubspythonv3.6.1.20260728
Install
3.1s avg
Import
Disk
91MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.6.1.20260728 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 90.7MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 3.1s · import 0.000s · 87MB
91MB installed
● package 91MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Graph
from networkx_stubs import Graph
from networkx-stubs import Graph

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
Debug
Known issues
gotchaEnsure 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.
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.*'`.
affects: All versions
gotchaSome `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.
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.
affects: networkx <= 3.x
breakingWhile `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.
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.
affects: networkx 1.x, 2.x (when migrating to newer 3.x with corresponding stubs)
Upgrade
Version history
3.6.1.20260728latest on PyPI · released Jul 28, 2026
Audit
Dependencies
networkxrequiredThis package provides type stubs for the `networkx` library; `networkx` must be installed separately for runtime execution.
Agent activity
39 hits · last 30 days
node
32
OpenAI (training)
1
Resources
types-networkx — pip install types-networkx · libregistry