From 0d6b626ffb5abbcebe455a763e65a4997c2319cb Mon Sep 17 00:00:00 2001 From: Krishnendu Rajesh Date: Fri, 25 Sep 2026 23:50:07 +0530 Subject: [PATCH 1/4] Add type annotation docs for PyGraph --- src/graph.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/graph.rs b/src/graph.rs index ed54430fc0..57ce713406 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -132,6 +132,31 @@ use petgraph::visit::{ /// :math:`2^{32} - 1` (4,294,967,294) each. Attempting to add more nodes or /// edges than this will result in an exception being raised. /// +/// Type Annotations: +/// +/// You can use standard Python typing for specifying expected data types for both node and edge parameters. +/// The generic parameters for ``PyGraph`` are ordered as ``PyGraph[NodeData, EdgeData]``. +/// +/// .. code-block:: python +/// +/// import rustworkx as rx +/// +/// graph: rx.PyGraph[str, int] = rx.PyGraph() +/// node_a = graph.add_node("qubit0") +/// node_b = graph.add_node("qubit1") +/// graph.add_edge(node_a, node_b, 10) +/// +/// If you plan to use methods that create edges without data (connecting 2 nodes with an edge with no value), such as +/// :meth:`~rustworkx.PyGraph.add_edges_from_no_data`, you must include +/// ``None`` in your edge (_T) type annotation (using ``| None``). +/// +/// .. code-block:: python +/// +/// # Type hinting a graph where edges can be created without a value +/// graph: rx.PyGraph[str, int | None] = rx.PyGraph() +/// graph.add_nodes_from(["qubit0", "qubit1"]) +/// graph.add_edges_from_no_data([(0, 1)]) +/// /// :param bool multigraph: When this is set to ``False`` the created PyGraph /// object will not be a multigraph. When ``False`` if a method call is /// made that would add parallel edges the weight/weight from that From ccf56f8ab85853777ef5c7b53c9edb7b38a94a80 Mon Sep 17 00:00:00 2001 From: Krishnendu Rajesh Date: Wed, 30 Sep 2026 22:39:28 +0530 Subject: [PATCH 2/4] Document type annotations for PyGraph in README Added section on type annotations for PyGraph parameters and usage examples. --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 9d985b23a9..cc9df68c01 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,32 @@ graph.add_edges_from([(a, b, 1.5), (a, c, 5.0), (b, c, 2.5)]) # Returns the path A -> B -> C rustworkx.dijkstra_shortest_paths(graph, a, c, weight_fn=float) + +``` + +### Type Annotations + +You could use the standard Python typing for specifying data types for node and edge parameters. The parameters for `PyGraph` are ordered like `PyGraph[NodeData, EdgeData]`. + +```python +import rustworkx as rx + +graph: rx.PyGraph[str, int] = rx.PyGraph() +node_a = graph.add_node("qubit0") +node_b = graph.add_node("qubit1") +graph.add_edge(node_a, node_b, 10) ``` +If you want to use methods that create edges without data (like connecting two nodes with no edge value), such as `rustworkx.PyGraph.add_edges_from_no_data`, you must include `None` in your edge (`_T`) type annotation using `| None`. + +```python +graph: rx.PyGraph[str, int | None] = rx.PyGraph() +graph.add_nodes_from(["qubit0", "qubit1"]) +graph.add_edges_from_no_data([(0, 1)]) +``` + + + ## Installing rustworkx rustworkx is published on [PyPI](https://pypi.org/project/rustworkx/) so on x86\_64, i686, ppc64le, s390x, and From 86b8781169d6fc8d519bd281acdbc0f67104d9b1 Mon Sep 17 00:00:00 2001 From: Krishnendu Rajesh Date: Sat, 3 Oct 2026 11:07:43 +0530 Subject: [PATCH 3/4] Update README with type annotations and examples Removed outdated examples and added note on type annotations. --- README.md | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index cc9df68c01..c29368a956 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ All graph classes and top-level functions are accessible with a single import. To illustrate this, the following example calculates the shortest path between two nodes `A` and `C` in an undirected graph. + ```python3 import rustworkx @@ -43,26 +44,7 @@ rustworkx.dijkstra_shortest_paths(graph, a, c, weight_fn=float) ``` -### Type Annotations - -You could use the standard Python typing for specifying data types for node and edge parameters. The parameters for `PyGraph` are ordered like `PyGraph[NodeData, EdgeData]`. - -```python -import rustworkx as rx - -graph: rx.PyGraph[str, int] = rx.PyGraph() -node_a = graph.add_node("qubit0") -node_b = graph.add_node("qubit1") -graph.add_edge(node_a, node_b, 10) -``` - -If you want to use methods that create edges without data (like connecting two nodes with no edge value), such as `rustworkx.PyGraph.add_edges_from_no_data`, you must include `None` in your edge (`_T`) type annotation using `| None`. - -```python -graph: rx.PyGraph[str, int | None] = rx.PyGraph() -graph.add_nodes_from(["qubit0", "qubit1"]) -graph.add_edges_from_no_data([(0, 1)]) -``` +NOTE:The library supports standard Python type annotations (e.g., `PyGraph[NodeData, EdgeData]`) for specifying expected data types. From 25d2d1e0883d27a22ab0759357ead61563df1222 Mon Sep 17 00:00:00 2001 From: Krishnendu Rajesh Date: Sat, 3 Oct 2026 12:29:33 +0530 Subject: [PATCH 4/4] Type annotations for PyDiGraph Added type annotation examples for PyDiGraph parameters. --- src/digraph.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/digraph.rs b/src/digraph.rs index fd601d7b90..c80f7d4d00 100644 --- a/src/digraph.rs +++ b/src/digraph.rs @@ -170,6 +170,31 @@ use super::dag_algo::is_directed_acyclic_graph; /// :math:`2^{32} - 1` (4,294,967,294) each. Attempting to add more nodes or /// edges than this will result in an exception being raised. /// +/// Type Annotations: +/// +/// You may use standard Python typing for specifying expected data types for node and edge parameters. +/// The generic parameters for ``PyDiGraph`` are ordered like this: ``PyDiGraph[NodeData, EdgeData]``. +/// +/// .. code-block:: python +/// +/// import rustworkx as rx +/// +/// graph: rx.PyDiGraph[str, int] = rx.PyDiGraph() +/// node_a = graph.add_node("qubit0") +/// node_b = graph.add_node("qubit1") +/// graph.add_edge(node_a, node_b, 10) +/// +/// If you use methods that create edges without data (that is ,connecting 2 nodes with an edge with no value), such as +/// :meth:`~rustworkx.PyDiGraph.add_edges_from_no_data`, you must include +/// ``None`` in your edge (``_T``) type annotation (using ``| None``). +/// +/// .. code-block:: python +/// +/// # Type hinting a graph where edges can be created without a value +/// graph: rx.PyDiGraph[str, int | None] = rx.PyDiGraph() +/// graph.add_nodes_from(["qubit0", "qubit1"]) +/// graph.add_edges_from_no_data([(0, 1)]) # parent=0, child=1 +/// /// :param bool check_cycle: When this is set to ``True`` the created /// ``PyDiGraph`` has runtime cycle detection enabled. /// :param bool multigraph: When this is set to ``False`` the created