TypeError: add_edge() got an unexpected keyword argument ‘weight’

I try to make a PERT graph. So, I tried to put a bool variable “_dirty” that checks whether the graph is deformed. However, the add_egde function does not work properly. Is the code wrong?

My graph code:

class RailwayGraph(nx.MultiDiGraph):
    def __init__(self):
        self._dirty = True 
        self._critical_path = None

    def add_edge(self, *args):
        """ add one edge """
        self._dirty = True

Using add_edge:

def set_scheduled_edges(graph_name):
    node_list = list(graph_name.nodes)

    for train in trains:
        tmp = 0   
        for node in node_list:
            if node == 'Start_node':
            elif graph_name.nodes[node]['train'] == train.getName():
                graph_name.add_edge('Start_node', node, weight = train.getScheduleTime()[tmp])
                tmp += 1

Error I got:
TypeError: add_edge() got an unexpected keyword argument ‘weight’


See that you only specify to get arguments, not keyword arguments(weight in your case.)

Try adding **kwargs like add_edge(self, *args,**kwargs), and it should be fine. Also dont forget to update the super after that. 🙂