fix(context): keep ValueError for non-string causal relationship types (#1184)

Review feedback: normalization must not turn invalid inputs into
AttributeError. Non-string relationship types now raise ValueError before
normalization, matching the pre-change behavior; strings are stripped
before alias lookup.
This commit is contained in:
Aldrin Joseph
2026-08-22 16:40:21 +05:30
parent 283b7ada0c
commit 2d976963ab
2 changed files with 25 additions and 2 deletions
+5 -2
View File
@@ -2764,8 +2764,11 @@ class ContextGraph:
"""
# Normalize so callers may use either vocabulary's spelling
# ("causes" from CausalChainAnalyzer, or "CAUSED" from this module's
# canonical constant); the stored form is always canonical.
relationship_type = _CAUSAL_EDGE_ALIASES.get(relationship_type.upper())
# canonical constant); the stored form is always canonical. Invalid
# inputs keep raising ValueError rather than AttributeError.
if not isinstance(relationship_type, str):
raise ValueError(f"Relationship type must be one of: {_CAUSAL_EDGE_TYPES}")
relationship_type = _CAUSAL_EDGE_ALIASES.get(relationship_type.strip().upper())
if relationship_type is None:
raise ValueError(f"Relationship type must be one of: {_CAUSAL_EDGE_TYPES}")
@@ -7,6 +7,8 @@ extraction found nothing, the chain came back empty even though an explicit
``CAUSED`` edge was stored in the graph.
"""
import pytest
from semantica.context import ContextGraph
from semantica.context.context_graph import ContextEdge
@@ -371,3 +373,21 @@ def test_add_causal_relationship_accepts_any_case_and_stores_canonical():
]
assert edges, "add_causal_relationship must store the edge"
assert edges[0].edge_type == "CAUSED"
def test_add_causal_relationship_rejects_non_string_with_value_error():
"""Invalid relationship types must keep raising ValueError (issue #1184
follow-up): normalization must not turn them into AttributeError."""
graph = ContextGraph(advanced_analytics=True)
cause = graph.record_decision(
category="a", scenario="upstream", reasoning="r",
outcome="x", confidence=0.9,
)
effect = graph.record_decision(
category="b", scenario="downstream", reasoning="r",
outcome="y", confidence=0.9,
)
for bad_type in (None, 42, ["CAUSED"]):
with pytest.raises(ValueError):
graph.add_causal_relationship(cause, effect, relationship_type=bad_type)