diff --git a/README.md b/README.md index 9d985b23a9..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 @@ -40,8 +41,13 @@ 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) + ``` +NOTE:The library supports standard Python type annotations (e.g., `PyGraph[NodeData, EdgeData]`) for specifying expected data types. + + + ## Installing rustworkx rustworkx is published on [PyPI](https://pypi.org/project/rustworkx/) so on x86\_64, i686, ppc64le, s390x, and 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 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