Skip to content

Fix round-trip to and from pygraphviz.AGraph setting spurious graph attributes - #8121

Merged
rossbar merged 1 commit into
networkx:mainfrom
dschult:graphviz_roundtrip
Jun 25, 2025
Merged

rossbar merged 1 commit into
networkx:mainfrom
dschult:graphviz_roundtrip

Conversation

@dschult

@dschult dschult commented Jun 25, 2025

Copy link
Copy Markdown
Member

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?

@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.

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!

@rossbar

rossbar commented Jun 25, 2025

Copy link
Copy Markdown
Contributor

I'm going to go ahead and put this one in to unblock the CI problems!

@rossbar
rossbar merged commit d3eb7b5 into networkx:main Jun 25, 2025
@dschult
dschult deleted the graphviz_roundtrip branch June 26, 2025 01:25
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
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.

2 participants