diff options
| -rw-r--r-- | networkx/classes/tests/test_multidigraph.py | 4 | ||||
| -rw-r--r-- | networkx/classes/tests/test_multigraph.py | 58 | ||||
| -rw-r--r-- | networkx/convert.py | 8 |
3 files changed, 56 insertions, 14 deletions
diff --git a/networkx/classes/tests/test_multidigraph.py b/networkx/classes/tests/test_multidigraph.py index 74818494..814420ce 100644 --- a/networkx/classes/tests/test_multidigraph.py +++ b/networkx/classes/tests/test_multidigraph.py @@ -416,7 +416,7 @@ class CustomDictClass(UserDict): pass -class SubMultiDiGraph(nx.MultiDiGraph): +class MultiDiGraphSubClass(nx.MultiDiGraph): node_dict_factory = CustomDictClass # type: ignore node_attr_dict_factory = CustomDictClass # type: ignore adjlist_outer_dict_factory = CustomDictClass # type: ignore @@ -428,7 +428,7 @@ class SubMultiDiGraph(nx.MultiDiGraph): class TestMultiDiGraphSubclass(TestMultiDiGraph): def setup_method(self): - self.Graph = SubMultiDiGraph + self.Graph = MultiDiGraphSubClass # build K3 self.k3edges = [(0, 1), (0, 2), (1, 2)] self.k3nodes = [0, 1, 2] diff --git a/networkx/classes/tests/test_multigraph.py b/networkx/classes/tests/test_multigraph.py index f9d78dc6..0584f562 100644 --- a/networkx/classes/tests/test_multigraph.py +++ b/networkx/classes/tests/test_multigraph.py @@ -1,3 +1,5 @@ +from collections import UserDict + import pytest import networkx as nx @@ -345,6 +347,16 @@ class TestMultiGraph(BaseMultiGraphTester, _TestGraph): with pytest.raises(TypeError): G.add_edges_from([0]) + def test_multigraph_add_edges_from_four_tuple_misordered(self): + """add_edges_from expects 4-tuples of the format (u, v, key, data_dict). + + Ensure 4-tuples of form (u, v, data_dict, key) raise exception. + """ + G = nx.MultiGraph() + with pytest.raises(TypeError): + # key/data values flipped in 4-tuple + G.add_edges_from([(0, 1, {"color": "red"}, 0)]) + def test_remove_edge(self): G = self.K3 G.remove_edge(0, 1) @@ -472,12 +484,42 @@ class TestEdgeSubgraph: assert self.G.graph is self.H.graph -def test_multigraph_add_edges_from_four_tuple_misordered(): - """add_edges_from expects 4-tuples of the format (u, v, key, data_dict). +class CustomDictClass(UserDict): + pass + + +class MultiGraphSubClass(nx.MultiGraph): + node_dict_factory = CustomDictClass # type: ignore + node_attr_dict_factory = CustomDictClass # type: ignore + adjlist_outer_dict_factory = CustomDictClass # type: ignore + adjlist_inner_dict_factory = CustomDictClass # type: ignore + edge_key_dict_factory = CustomDictClass # type: ignore + edge_attr_dict_factory = CustomDictClass # type: ignore + graph_attr_dict_factory = CustomDictClass # type: ignore - Ensure 4-tuples of form (u, v, data_dict, key) raises exception. - """ - G = nx.MultiGraph() - with pytest.raises(TypeError): - # key/data values flipped in 4-tuple - G.add_edges_from([(0, 1, {"color": "red"}, 0)]) + +class TestMultiGraphSubclass(TestMultiGraph): + def setup_method(self): + self.Graph = MultiGraphSubClass + # build K3 + self.k3edges = [(0, 1), (0, 2), (1, 2)] + self.k3nodes = [0, 1, 2] + self.K3 = self.Graph() + self.K3._adj = self.K3.adjlist_outer_dict_factory( + { + 0: self.K3.adjlist_inner_dict_factory(), + 1: self.K3.adjlist_inner_dict_factory(), + 2: self.K3.adjlist_inner_dict_factory(), + } + ) + self.K3._pred = {0: {}, 1: {}, 2: {}} + for u in self.k3nodes: + for v in self.k3nodes: + if u != v: + d = {0: {}} + self.K3._adj[u][v] = d + self.K3._adj[v][u] = d + self.K3._node = self.K3.node_dict_factory() + self.K3._node[0] = self.K3.node_attr_dict_factory() + self.K3._node[1] = self.K3.node_attr_dict_factory() + self.K3._node[2] = self.K3.node_attr_dict_factory() diff --git a/networkx/convert.py b/networkx/convert.py index 25be6ba6..d1ba28ae 100644 --- a/networkx/convert.py +++ b/networkx/convert.py @@ -103,15 +103,15 @@ def to_networkx_graph(data, create_using=None, multigraph_input=False): return from_dict_of_dicts( data, create_using=create_using, multigraph_input=multigraph_input ) - except Exception as err: + except Exception as err1: if multigraph_input is True: raise nx.NetworkXError( - f"converting multigraph_input raised:\n{type(err)}: {err}" + f"converting multigraph_input raised:\n{type(err1)}: {err1}" ) try: return from_dict_of_lists(data, create_using=create_using) - except Exception as err: - raise TypeError("Input is not known type.") from err + except Exception as err2: + raise TypeError("Input is not known type.") from err2 # Pandas DataFrame try: |
