Repository navigation
Improve error message for removed random_tree function - #8105
Conversation
|
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!). |
|
@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 |
| 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" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Let's hold off on generalizing for now!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
So the idea is to keep this forever or only for a few versions?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
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 |
rossbar
left a comment
There was a problem hiding this comment.
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).
Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Ross Barnowski <[email protected]>
supreethmv
left a comment
There was a problem hiding this comment.
Thank you for adding the test
rossbar
left a comment
There was a problem hiding this comment.
This LGTM, thanks @supreethmv for starting the discussion and contributing!
dschult
left a comment
There was a problem hiding this comment.
I approve of this feature and implementation.
Thanks everyone -- especially @supreethmv !!
|
Thank you all for the thoughtful reviews and support. |
* 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]>
Closes #8104
This PR improves the developer experience when calling the removed
networkx.random_treefunction (removed in v3.4) by raising a clearer, more informativeAttributeError.❌ Current behavior
Accessing
networkx.random_treeraises:Screenshot:

This error message is not helpful for users upgrading from older versions or reading legacy code.
✅ Proposed behavior
Adds a module-level
__getattr__innetworkx/__init__.pythat catches attempts to accessrandom_treeand raises:Screenshot:

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.