Skip to content

Improve error message for removed random_tree function - #8105

Merged
dschult merged 6 commits into
networkx:mainfrom
supreethmv:improve-random-tree-error
Jun 7, 2025
Merged

dschult merged 6 commits into
networkx:mainfrom
supreethmv:improve-random-tree-error

Conversation

@supreethmv

@supreethmv supreethmv commented Jun 7, 2025 •

Copy link
Copy Markdown
Contributor

Closes #8104

This PR improves the developer experience when calling the removed networkx.random_tree function (removed in v3.4) by raising a clearer, more informative AttributeError.

❌ Current behavior

Accessing networkx.random_tree raises:

AttributeError: module 'networkx' has no attribute 'random_tree'

Screenshot:
before

This error message is not helpful for users upgrading from older versions or reading legacy code.


✅ Proposed behavior

Adds a module-level __getattr__ in networkx/__init__.py that catches attempts to access random_tree and raises:

AttributeError: `networkx.random_tree` was removed in version 3.4. Use `networkx.random_labeled_tree` instead.
See: https://networkx.org/documentation/networkx-3.5/release/release_3.4.html

Screenshot:
after

This change is minimal and does not affect any existing internal logic or runtime performance.


Let me know if you'd like me to generalize this into a utility for future removals as well.

@Schefflera-Arboricola

Copy link
Copy Markdown
Member

Thank you for your work but we have a deprecation policy-- we show deprecation warnings to users for 2 release cycles before removing an algorithm or making any code breaking change. This kind of explicit error handling is not needed. Thanks!

@amcandio

amcandio commented Jun 7, 2025 •

Copy link
Copy Markdown
Contributor

Thank you for your work but we have a deprecation policy-- we show deprecation warnings to users for 2 release cycles before removing an algorithm or making any code breaking change. This kind of explicit error handling is not needed. Thanks!

I'm +1 on this change, it improves user experience. Sure, the warning is there for 2 minor versions but users don't necessarily update versions 1 by 1. Many times you have to make big updates when working in projects with multiple dependencies (and conflicts!).

@rossbar

rossbar commented Jun 7, 2025

Copy link
Copy Markdown
Contributor

@Schefflera-Arboricola it's true that the deprecation policy was followed in this case - this PR proposes a solution to a different problem. What happens when code that was written before the deprecation warnings were added is run now? When it hits the function that has been removed, you get an AttributeError which is not at all informative. This replaces the uninformative error with something much more clear and directly related to the problem at hand!

Comment thread networkx/__init__.py Outdated
def __getattr__(name):
if name == "random_tree":
raise AttributeError(
"`networkx.random_tree` was removed in version 3.4. Use `networkx.random_labeled_tree` instead.\n"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think you can generalize this with a dict/dataclass? There are many more to add and it will make adding them very easy.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's hold off on generalizing for now!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize the intention to avoid cluttering the main module, so an alternative idea would be to modularize such cases via something like a _removed.py helper file, where we can intercept top-level calls to recently removed functions and offer helpful redirection just for the next minor version or two. That would keep the codebase clean.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the idea is to keep this forever or only for a few versions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @rossbar to hold.
Still, @amcandio here's a direction for generalizing.

1. A new file: networkx/_removed.py

This will house all removed APIs.

# networkx/_removed.py

def handle_removed_api(name: str):
    removed_apis = {
        "random_tree": (
            "`networkx.random_tree` was removed in version 3.4. Use `networkx.random_labeled_tree` instead.\n"
            "See: https://networkx.org/documentation/networkx-3.5/release/release_3.4.html"
        ),
        # Future entries can be added here easily
        # "old_function": "Helpful message"
    }

    if name in removed_apis:
        raise AttributeError(removed_apis[name])

    raise AttributeError(f"module 'networkx' has no attribute '{name}'")

2. Modify networkx/__init__.py to delegate

At the end of __init__.py, do:

from networkx._removed import handle_removed_api

def __getattr__(name):
    return handle_removed_api(name)

This makes __init__.py super clean, and new removals are easily handled.

@rossbar rossbar Jun 7, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize the intention to avoid cluttering the main module, so an alternative idea would be to modularize such cases via something like a _removed.py helper file, where we can intercept top-level calls to recently removed functions and offer helpful redirection just for the next minor version or two. That would keep the codebase clean.

I'd prefer to keep it as simple as possible, which IMO means using __getattr__ directly in the usage pattern that matches the motivation from the PEP that added it. There are a lot of things that could be done, but I think there's a lot of value in keeping things simple, especially at the beginning. To quote from one of my favorite Python books:

Premature abstraction is as bad as premature optimization (Fluent Python)

In other words - I think it's fine as-is!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the idea is to keep this forever or only for a few versions?

I would think in perpetuity... I don't see the harm in doing so.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't view it as premature bc we have many cases to add. But that goes back to the other question: do we plan to do this for every removed method? If not, yeah it's premature

@amcandio

amcandio commented Jun 7, 2025

Copy link
Copy Markdown
Contributor

Do we need to update contributors guidelines or something?

@rossbar

rossbar commented Jun 7, 2025

Copy link
Copy Markdown
Contributor

Do we need to update contributors guidelines or something?

My vote is for incremental adoption. I don't think that every removed function necessarily needs this treatment, especially "helper" functions from subpackages etc. In the general case there are other questions that would need to be answered, e.g. should we add module __getattr__ s with specialized exceptions to each namespace from which the removed function is (was) accessible? Again, I tend towards no - but there are enough moving parts in the general case that I don't think it's worthwhile to codify a policy (yet).

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, thanks @supreethmv .

I took the liberty of adding tests for the new functionality, i.e. making sure the exception messages are updated. Make sure to pull before pushing to incorporate those changes!

Note that the tests will currently fail, but that's just because of the opinionated choice I made re: the alias in the exception message (accepting the suggest changes should fix it).

Comment thread networkx/__init__.py Resolved
Comment thread networkx/__init__.py Resolved
supreethmv and others added 2 commits June 7, 2025 19:08
Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Ross Barnowski <[email protected]>

@supreethmv supreethmv left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding the test

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM, thanks @supreethmv for starting the discussion and contributing!

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve of this feature and implementation.
Thanks everyone -- especially @supreethmv !!

@dschult
dschult merged commit 87ced81 into networkx:main Jun 7, 2025
@supreethmv

Copy link
Copy Markdown
Contributor Author

Thank you all for the thoughtful reviews and support.

amcandio pushed a commit to amcandio/networkx that referenced this pull request Jul 20, 2025
* Improve error message for removed `random_tree` function

* Add missing newline at the end

* TST: Add tests for exception message.

* Update networkx/__init__.py

Co-authored-by: Ross Barnowski <[email protected]>

* Update networkx/__init__.py

Co-authored-by: Ross Barnowski <[email protected]>

* TST: tweak regex.

---------

Co-authored-by: Ross Barnowski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Improve error message for removed random_tree function in NetworkX ≥ 3.4

5 participants