fix(build): coerce numeric hyperedge members under the members/node_ids aliases
_coerce_non_string_ids (#2326) str-coerces numeric node ids, edge
endpoints including the legacy from/to aliases, and only the canonical
`nodes` hyperedge member list. A hyperedge that spells its members with a
tolerated alias (#1561) kept bare ints, while its nodes became "1"/"2"/"3".
_normalize_hyperedge_members then folded the alias onto `nodes`, every
member matched no built node, and the whole hyperedge was dropped. The
same input keyed `nodes` survives. Coerce the alias lists too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 6af5f5a75c)
This commit is contained in:
1 parent
4c7d710f75
commit
dfd4ea95ec
2 files changed
+40
-3
No files matched your search
+7
-3
@@ -648,9 +648,13 @@ def _coerce_non_string_ids(extraction: dict) -> None:
|
||||
for he in extraction.get("hyperedges") or ():
|
||||
if not isinstance(he, dict):
|
||||
continue
|
||||
members = he.get("nodes")
|
||||
if isinstance(members, list):
|
||||
he["nodes"] = [_coerce_id(ref) for ref in members]
|
||||
# The `members`/`node_ids` aliases (#1561) are coerced too, for the
|
||||
# same reason as `from`/`to` above: they are folded onto `nodes` only
|
||||
# later, by which time the node ids they name have already become str.
|
||||
for key in ("nodes", *_HE_MEMBER_ALIASES):
|
||||
members = he.get(key)
|
||||
if isinstance(members, list):
|
||||
he[key] = [_coerce_id(ref) for ref in members]
|
||||
|
||||
|
||||
def _norm_source_file(p: str | None, root: str | None = None) -> str | None:
|
||||
|
||||
@@ -91,3 +91,36 @@ def test_sanitize_fragment_coerces_dict_members_to_strings(capsys):
|
||||
members = out["hyperedges"][0]["nodes"]
|
||||
assert members == ["x", "y", "3"], "dict members collapse to their id"
|
||||
assert all(isinstance(m, str) for m in members)
|
||||
|
||||
|
||||
def test_numeric_members_coerced_via_aliases(capsys):
|
||||
"""A tolerated member alias must coerce numeric ids like the canonical key.
|
||||
|
||||
``_coerce_non_string_ids`` (#2326) str-coerces numeric node ids and only the
|
||||
``nodes`` member list, so a hyperedge that spells its members ``members`` or
|
||||
``node_ids`` (#1561) kept bare ints while its nodes became ``"1"``/``"2"``.
|
||||
Every member then matched no built node and the whole hyperedge was dropped,
|
||||
while the same input under ``nodes`` survives.
|
||||
"""
|
||||
for alias in ("nodes", "members", "node_ids"):
|
||||
extraction = {
|
||||
"nodes": [
|
||||
{"id": 1, "label": "Alpha", "file_type": "concept", "source_file": "a.md"},
|
||||
{"id": 2, "label": "Beta", "file_type": "concept", "source_file": "a.md"},
|
||||
{"id": 3, "label": "Gamma", "file_type": "concept", "source_file": "a.md"},
|
||||
],
|
||||
"edges": [],
|
||||
"hyperedges": [
|
||||
{"id": "h_num", "relation": "collab", alias: [1, 2, 3],
|
||||
"source_file": "a.md"},
|
||||
],
|
||||
}
|
||||
G = build_from_json(extraction, directed=True, dedup=False)
|
||||
assert sorted(G.nodes()) == ["1", "2", "3"]
|
||||
hyperedges = G.graph["hyperedges"]
|
||||
assert [he["id"] for he in hyperedges] == ["h_num"], (
|
||||
f"hyperedge dropped when members were keyed {alias!r}"
|
||||
)
|
||||
assert hyperedges[0]["nodes"] == ["1", "2", "3"], (
|
||||
f"numeric members not coerced when keyed {alias!r}"
|
||||
)
|
||||
Reference in new issue
Block a user