diff options
author | Tomohiro Endo <paul.thomas.goodfellas@gmail.com> | 2021-06-22 17:08:17 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-22 11:08:17 +0300 |
commit | 9315381d3fa39fed4de166457eaf50e5996b8d87 (patch) | |
tree | 2ee2da0d97b6447dc498af206c461e1ce920add5 | |
parent | 60d1d8af901c314533f8ac8d707a6c886f87a88b (diff) | |
download | networkx-9315381d3fa39fed4de166457eaf50e5996b8d87.tar.gz |
Fix graph_class usage in to_undirected method (#4912)
* fix error in the tests... now original code fails test
Co-authored-by: Dan Schult <dschult@colgate.edu>
-rw-r--r-- | networkx/classes/digraph.py | 4 | ||||
-rw-r--r-- | networkx/classes/tests/test_graph.py | 47 |
2 files changed, 49 insertions, 2 deletions
diff --git a/networkx/classes/digraph.py b/networkx/classes/digraph.py index 30b6a393..ca2f9cf4 100644 --- a/networkx/classes/digraph.py +++ b/networkx/classes/digraph.py @@ -1179,9 +1179,9 @@ class DiGraph(Graph): """ graph_class = self.to_undirected_class() if as_view is True: - return nx.graphviews.generic_graph_view(self, Graph) + return nx.graphviews.generic_graph_view(self, graph_class) # deepcopy when not a view - G = Graph() + G = graph_class() G.graph.update(deepcopy(self.graph)) G.add_nodes_from((n, deepcopy(d)) for n, d in self._node.items()) if reciprocal is True: diff --git a/networkx/classes/tests/test_graph.py b/networkx/classes/tests/test_graph.py index 288a7efb..c8554722 100644 --- a/networkx/classes/tests/test_graph.py +++ b/networkx/classes/tests/test_graph.py @@ -453,6 +453,53 @@ class BaseAttrGraphTester(BaseGraphTester): H = G.to_undirected() self.is_deepcopy(H, G) + def test_to_directed_as_view(self): + H = nx.path_graph(2, create_using=self.Graph) + H2 = H.to_directed(as_view=True) + assert H is H2._graph + assert H2.has_edge(0, 1) + assert H2.has_edge(1, 0) or H.is_directed() + pytest.raises(nx.NetworkXError, H2.add_node, -1) + pytest.raises(nx.NetworkXError, H2.add_edge, 1, 2) + H.add_edge(1, 2) + assert H2.has_edge(1, 2) + assert H2.has_edge(2, 1) or H.is_directed() + + def test_to_undirected_as_view(self): + H = nx.path_graph(2, create_using=self.Graph) + H2 = H.to_undirected(as_view=True) + assert H is H2._graph + assert H2.has_edge(0, 1) + assert H2.has_edge(1, 0) + pytest.raises(nx.NetworkXError, H2.add_node, -1) + pytest.raises(nx.NetworkXError, H2.add_edge, 1, 2) + H.add_edge(1, 2) + assert H2.has_edge(1, 2) + assert H2.has_edge(2, 1) + + def test_directed_class(self): + G = self.Graph() + + class newGraph(G.to_undirected_class()): + def to_directed_class(self): + return newDiGraph + + def to_undirected_class(self): + return newGraph + + class newDiGraph(G.to_directed_class()): + def to_directed_class(self): + return newDiGraph + + def to_undirected_class(self): + return newGraph + + G = newDiGraph() if G.is_directed() else newGraph() + H = G.to_directed() + assert isinstance(H, newDiGraph) + H = G.to_undirected() + assert isinstance(H, newGraph) + def test_to_directed(self): G = self.K3 self.add_attributes(G) |