Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty GraphML attribute is not parsed #7319

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion networkx/readwrite/graphml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,9 +1010,10 @@ def decode_data_elements(self, graphml_keys, obj_xml):
edge_label = data_element.find(f"{pref}EdgeLabel")
if edge_label is not None:
break

if edge_label is not None:
data["label"] = edge_label.text
elif text is None:
data[data_name] = ""
return data

def find_graphml_keys(self, graph_element):
Expand Down
24 changes: 24 additions & 0 deletions networkx/readwrite/tests/test_graphml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,3 +1505,27 @@ def test_exception_for_unsupported_datatype_graph_attr():
fh = io.BytesIO()
with pytest.raises(TypeError, match="GraphML does not support"):
nx.write_graphml(G, fh)


def test_empty_attribute():
"""Tests that a GraphML string with an empty attribute can be parsed
correctly."""
s = """<?xml version='1.0' encoding='utf-8'?>
<graphml>
<key id="d1" for="node" attr.name="foo" attr.type="string"/>
<key id="d2" for="node" attr.name="bar" attr.type="string"/>
<graph>
<node id="0">
<data key="d1">aaa</data>
<data key="d2">bbb</data>
</node>
<node id="1">
<data key="d1">ccc</data>
<data key="d2"></data>
</node>
</graph>
</graphml>"""
fh = io.BytesIO(s.encode("UTF-8"))
G = nx.read_graphml(fh)
assert G.nodes["0"] == {"foo": "aaa", "bar": "bbb"}
assert G.nodes["1"] == {"foo": "ccc", "bar": ""}