Skip to content

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

Closed

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.

Activity

  1. JEZiegler commented on Dec 5, 2025

    @JEZiegler
    Author

    If instead the arguments beyond the first are provided as keyword arguments this behaves fine (b/c __new__ accepts **attrs ?). If only one argument is provided this behaves fine (b/c __new__ has one argument?)

  2. amcandio commented on Dec 5, 2025

    @amcandio
    Contributor

    This is caused due to the DiGraph class implementing __new__ method. To fix this, you should implement new in your subclass to match your init signature.

    Is this something that was working for you and it recently broke?

  3. JEZiegler commented on Dec 5, 2025

    @JEZiegler
    Author

    Yeah I already put this fix in but it looked somewhat like the __new__ implementation in Digraph was intended to be agnostic by accepting **attrs.

    Yes this was working before on older versions of networkx which makes sense given the __new__ implementation was added in 3.6 or so?

  4. amcandio commented on Dec 5, 2025

    @amcandio
    Contributor

    Yes, it was caused by #7760 which we included in 3.6 release. Thanks for the report! cc: @eriknw

    I don't have my laptop to test but hopefully changing __new__ in graph classes to take *arg and **kwarg might do it.

  5. dschult commented on Dec 5, 2025

    @dschult
    Member

    This sounds like something we might want to fix and then make a 3.6.1 release.
    Flagging for @MridulS

    Thanks for the report @JEZiegler !!

  6. added a commit that references this issue on Dec 5, 2025
    683a8a8
  7. eriknw commented on Dec 5, 2025

    @eriknw
    Contributor

    Oops, my bad! Hopefully fixed in #8369. Thanks for the ping @amcandio, and thanks all for the issue and discussion.

    I think you can work around this issue in nx 3.6 by adding the following to your new class:

        def __new__(cls, *args, **kwargs):
            return object.__new__(cls)

    or you can make the signature match the __init__ as @amcandio suggested.

    Obviously not ideal, sorry. I/we know to be careful with user-defined graph classes, which is an important feature, but this mistake slipped through the cracks.

    This won't be the first time I'm responsible for needing a hot-fix dot release 😶‍🌫️

  8. amcandio commented on Dec 5, 2025

    @amcandio
    Contributor

    Thanks for the quick turn around @eriknw!

  9. rossbar commented on Dec 5, 2025

    @rossbar
    Contributor

    This sounds like something we might want to fix and then make a 3.6.1 release.
    Flagging for @MridulS

    Agreed on the patch release (which we don't cover in the release guide!). I assume the easiest thing to do is create a 3.6.1-dev branch from the tagged 3.6.0 commit, cherry-pick the fix on that branch, then tag and go through the remaining release steps from there.

  10. dschult commented on Dec 6, 2025

    @dschult
    Member

    If cherry-picking for 3.6.1 is too messy, we could probably release from main. I don't think we have pushed anything that shouldn't be in 3.6.1. I think @jarrodmillman has done that in the past. (once we start updating deprecations and stuff like that then we wouldn't be able to do that of course...)

  11. MridulS commented on Dec 6, 2025

    @MridulS
    Member

    I think it will be quicker just to do a 3.6.1 release from main, we only have doc and tests related PRs merged into main right now.

  12. dschult commented on Dec 7, 2025

    @dschult
    Member

    I think there is also the addition of spectral bipartition #8347. But that should not mess up any users. It is adding new functions.
    I'm in favor of doing the 3.6.1 from main.

  13. added this to the 3.6.1 milestone on Dec 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions