Skip to content

Providing more than one argument to Digraph subclass leads to exception in __new__ #8367

@JEZiegler

Description

@JEZiegler

Current Behavior

When trying to instantiate a Digraph subclass with more than one argument an exception occurs in Digraph.__new__().

Expected Behavior

When subclassing Digraph, subclass's __init__ should accept args and kwargs as defined by the subclass __init__ method.

Steps to Reproduce

import networkx as nx 

class ChildGraph(nx.DiGraph):
    def __init__(self, arg1, arg2 = 4, arg3 = 'b'):
        super().__init__()
        self.arg1 = arg1
        self.arg2 = arg2
        self.arg3 = arg3

def test_child_graph():
    g1 = ChildGraph(1, 2, 'a')

    assert g1.arg1 == 1
    assert g1.arg2 == 2
    assert g1.arg3 == 'a'

yields

def test_child_graph():
>       g1 = ChildGraph(1, 2, 'a')
             ^^^^^^^^^^^^^^^^^^^^^
E       TypeError: DiGraph.__new__() takes from 1 to 2 positional arguments but 4 were given

Environment

Python version: 3.11
NetworkX version: 3.6

Additional context

Ubuntu 22.04, kernel 6.5, 6.8

Suggested fix

Add an *args argument to the __new__ method defined in the Digraph base class.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions