Skip to content

Commit 6ffa9bd

Browse files
authored
Use PyGenericAlias directly from PyO3 (#1421)
1 parent 51b7c72 commit 6ffa9bd

File tree

3 files changed

+9
-21
lines changed

3 files changed

+9
-21
lines changed

src/digraph.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use smallvec::SmallVec;
3232
use pyo3::exceptions::PyIndexError;
3333
use pyo3::gc::PyVisit;
3434
use pyo3::prelude::*;
35-
use pyo3::types::{IntoPyDict, PyBool, PyDict, PyList, PyString, PyTuple, PyType};
35+
use pyo3::types::{IntoPyDict, PyBool, PyDict, PyGenericAlias, PyList, PyString, PyTuple, PyType};
3636
use pyo3::IntoPyObjectExt;
3737
use pyo3::PyTraverseError;
3838
use pyo3::Python;
@@ -57,8 +57,8 @@ use super::iterators::{
5757
EdgeIndexMap, EdgeIndices, EdgeList, NodeIndices, NodeMap, WeightedEdgeList,
5858
};
5959
use super::{
60-
find_node_by_weight, generic_class_getitem, weight_callable, DAGHasCycle, DAGWouldCycle, IsNan,
61-
NoEdgeBetweenNodes, NoSuitableNeighbors, NodesRemoved, StablePyGraph,
60+
find_node_by_weight, weight_callable, DAGHasCycle, DAGWouldCycle, IsNan, NoEdgeBetweenNodes,
61+
NoSuitableNeighbors, NodesRemoved, StablePyGraph,
6262
};
6363

6464
use super::dag_algo::is_directed_acyclic_graph;
@@ -3440,7 +3440,8 @@ impl PyDiGraph {
34403440
cls: &Bound<'_, PyType>,
34413441
key: &Bound<'_, PyAny>,
34423442
) -> PyResult<PyObject> {
3443-
generic_class_getitem(cls, key)
3443+
let alias = PyGenericAlias::new(cls.py(), cls.as_any(), key)?;
3444+
Ok(alias.into())
34443445
}
34453446

34463447
// Functions to enable Python Garbage Collection

src/graph.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustworkx_core::graph_ext::*;
2626
use pyo3::exceptions::PyIndexError;
2727
use pyo3::gc::PyVisit;
2828
use pyo3::prelude::*;
29-
use pyo3::types::{IntoPyDict, PyBool, PyDict, PyList, PyString, PyTuple, PyType};
29+
use pyo3::types::{IntoPyDict, PyBool, PyDict, PyGenericAlias, PyList, PyString, PyTuple, PyType};
3030
use pyo3::IntoPyObjectExt;
3131
use pyo3::PyTraverseError;
3232
use pyo3::Python;
@@ -41,8 +41,7 @@ use crate::iterators::NodeMap;
4141
use super::dot_utils::build_dot;
4242
use super::iterators::{EdgeIndexMap, EdgeIndices, EdgeList, NodeIndices, WeightedEdgeList};
4343
use super::{
44-
find_node_by_weight, generic_class_getitem, weight_callable, IsNan, NoEdgeBetweenNodes,
45-
NodesRemoved, StablePyGraph,
44+
find_node_by_weight, weight_callable, IsNan, NoEdgeBetweenNodes, NodesRemoved, StablePyGraph,
4645
};
4746

4847
use crate::RxPyResult;
@@ -2058,7 +2057,8 @@ impl PyGraph {
20582057
cls: &Bound<'_, PyType>,
20592058
key: &Bound<'_, PyAny>,
20602059
) -> PyResult<PyObject> {
2061-
generic_class_getitem(cls, key)
2060+
let alias = PyGenericAlias::new(cls.py(), cls.as_any(), key)?;
2061+
Ok(alias.into())
20622062
}
20632063

20642064
// Functions to enable Python Garbage Collection

src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -371,19 +371,6 @@ fn find_node_by_weight<Ty: EdgeType>(
371371
Ok(index)
372372
}
373373

374-
fn generic_class_getitem(
375-
cls: &Bound<'_, pyo3::types::PyType>,
376-
key: &Bound<'_, PyAny>,
377-
) -> PyResult<PyObject> {
378-
Python::with_gil(|py| -> PyResult<PyObject> {
379-
let types_mod = py.import("types")?;
380-
let types_generic_alias = types_mod.getattr("GenericAlias")?;
381-
let args = (cls, key);
382-
let generic_alias = types_generic_alias.call1(args)?;
383-
Ok(generic_alias.into())
384-
})
385-
}
386-
387374
// The provided node is invalid.
388375
create_exception!(rustworkx, InvalidNode, PyException);
389376
// Performing this operation would result in trying to add a cycle to a DAG.

0 commit comments

Comments
 (0)