Skip to content

Commit 75bdd73

Browse files
eriknwrossbar
andauthored
Fix: allow graph subclasses to have additional arguments (#8369)
Co-authored-by: Ross Barnowski <rossbar@caltech.edu>
1 parent 7663fa5 commit 75bdd73

5 files changed

Lines changed: 27 additions & 4 deletions

File tree

networkx/classes/digraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class DiGraph(Graph):
338338
# in order to provide a backend that allows class instantiation, this method
339339
# can be overridden to return your own backend graph class.
340340
@nx._dispatchable(name="digraph__new__", graphs=None, returns_graph=True)
341-
def __new__(cls, incoming_graph_data=None, **attr):
341+
def __new__(cls, *args, **kwargs):
342342
return object.__new__(cls)
343343

344344
def __init__(self, incoming_graph_data=None, **attr):

networkx/classes/graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def to_undirected_class(self):
343343
# in order to provide a backend that allows class instantiation, this method
344344
# can be overridden to return your own backend graph class.
345345
@nx._dispatchable(name="graph__new__", graphs=None, returns_graph=True)
346-
def __new__(cls, incoming_graph_data=None, **attr):
346+
def __new__(cls, *args, **kwargs):
347347
return object.__new__(cls)
348348

349349
def __init__(self, incoming_graph_data=None, **attr):

networkx/classes/multidigraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class MultiDiGraph(MultiGraph, DiGraph):
306306
# in order to provide a backend that allows class instantiation, this method
307307
# can be overridden to return your own backend graph class.
308308
@nx._dispatchable(name="multidigraph__new__", graphs=None, returns_graph=True)
309-
def __new__(cls, incoming_graph_data=None, multigraph_input=None, **attr):
309+
def __new__(cls, *args, **kwargs):
310310
return object.__new__(cls)
311311

312312
def __init__(self, incoming_graph_data=None, multigraph_input=None, **attr):

networkx/classes/multigraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def to_undirected_class(self):
314314
# in order to provide a backend that allows class instantiation, this method
315315
# can be overridden to return your own backend graph class.
316316
@nx._dispatchable(name="multigraph__new__", graphs=None, returns_graph=True)
317-
def __new__(cls, incoming_graph_data=None, multigraph_input=None, **attr):
317+
def __new__(cls, *args, **kwargs):
318318
return object.__new__(cls)
319319

320320
def __init__(self, incoming_graph_data=None, multigraph_input=None, **attr):

networkx/classes/tests/test_graph.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,3 +925,26 @@ def test_graph_attr_dict(self):
925925
926926
"""
927927
assert self.G.graph is self.H.graph
928+
929+
930+
def test_graph_new_extra_args():
931+
"""Test that subclasses can accept additional arguments.
932+
933+
See: https://github.com/networkx/networkx/issues/8367
934+
"""
935+
936+
class MyGraph(nx.Graph):
937+
def __init__(self, incoming_graph_data=None, extra_arg=None, **attr):
938+
super().__init__(incoming_graph_data, **attr)
939+
self.extra_arg = extra_arg
940+
941+
G = MyGraph(extra_arg="extra arg")
942+
assert G.extra_arg == "extra arg"
943+
944+
G = MyGraph([], "extra arg")
945+
assert G.extra_arg == "extra arg"
946+
947+
G = MyGraph([(0, 1)], extra_arg="foo", name="bar")
948+
assert G.extra_arg == "foo"
949+
assert G.graph["name"] == "bar"
950+
assert nx.utils.edges_equal(G.edges, [(0, 1)])

0 commit comments

Comments
 (0)