Repository navigation
Fix round-trip to and from pygraphviz.AGraph setting spurious graph attributes - #8121
Merged
Merged
Conversation
rossbar
approved these changes
Jun 25, 2025
rossbar
left a comment
Contributor
There was a problem hiding this comment.
Thanks @dschult . I agree with the approach - it'd be good to fix this upstream but looking at the pygraphviz issue tracker it seems that graphviz 13 may require more extensive changes. I'm in favor of getting this patch in and modifying nx_agraph as necessary if/when upstream changes are made. Thanks for chasing this down and the helpful explanations!
Contributor
|
I'm going to go ahead and put this one in to unblock the CI problems! |
amcandio
pushed a commit
to amcandio/networkx
that referenced
this pull request
Jul 20, 2025
…ttributes (networkx#8121) fix roundtrip AGraph <-> nx.Graph spurious graph attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It looks like, for historical reasons, pygraphviz is adding a default node attribute named 'label' and assigning it the value '\N'. The NetworkX code has been accommodating that, and tests for the second round-trip resulting in graph attributes the same as the results for the first round-trip.
The test of this strange behavior recently started failing because the default node attribute for label is being reported as of GraphViz v13.0. See #8119. So we need to fix something. And it'd be nice to have it work across graphviz and pygraphviz versions.
This PR checks if pygraphviz reports either 1) no node attributes or 2) node attributes that are named 'label' and have the value '\N'. If either happens, we don't set any graph attributes in the graph created by
from_agraph. Similarly we also check whether no graph attributes or no edge attributes are are reported and in that case we also don't update the newly created graph attribute dict.The result is that a round-trip with no default attribute values set creates a networkx graph without any graph attributes set during the round-trip. The avoids the original (very old) issue with the round-trip not being the same as the original graph. And it avoids the difference between graphviz versions.
I will also make a PR to pygraphviz to remove the addition of node label default values set to
'\\N'as it hopefully is no longer needed. But this workaround should continue to work across any version changes of graphviz and pygraphviz. Fingers crossed. But for now -- does this do what we want it to do?