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):
        super().__init__()
        self._dirty = True 
        self._critical_path = None

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

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':
                continue
            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’

Answer

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. 🙂