Skip to content

Commit 5942bb5

Browse files
committed
improved assertion error messages
1 parent dba564a commit 5942bb5

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

graph_pattern.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,17 @@ def to_nx_graph_as_bipartite_hypergraph_equivalent(gp):
9292

9393

9494
def canonicalize_gp_to_rdf_graph(gp, fixed_vars=None):
95-
assert isinstance(gp, Iterable)
95+
assert isinstance(gp, Iterable), "gp not iterable: %r" % gp
9696
if fixed_vars is None:
9797
fixed_vars = set()
9898

9999
triple_bnodes = set()
100100
g = Graph()
101101
for t in gp:
102102
triple_bnode = BNode()
103-
assert triple_bnode not in triple_bnodes
103+
assert triple_bnode not in triple_bnodes, \
104+
"%r triple_bnode %r not meant to be in triple_bnodes %r" % (
105+
gp, triple_bnode, triple_bnodes)
104106
s, p, o = [
105107
BNode(i) if isinstance(i, Variable) and i not in fixed_vars else i
106108
for i in t
@@ -116,7 +118,8 @@ def canonicalize_gp_to_rdf_graph(gp, fixed_vars=None):
116118
def canonicalize_rdf_cg_to_gp(cg):
117119
cgp = []
118120
for triple_bnode in cg.subjects(RDF['type'], RDF['Statement']):
119-
assert isinstance(triple_bnode, BNode)
121+
assert isinstance(triple_bnode, BNode), \
122+
"expected BNode, got %r in %r" % (triple_bnode, list(cg))
120123
t = [
121124
cg.value(triple_bnode, p)
122125
for p in [RDF['subject'], RDF['predicate'], RDF['object']]
@@ -335,8 +338,10 @@ def __new__(
335338
notably rdflib.Variables) or None. The given mappings are
336339
applied during creation for the new GraphPattern.
337340
"""
338-
assert mapping is None or isinstance(mapping, dict)
339-
assert isinstance(triples, Iterable)
341+
assert mapping is None or isinstance(mapping, dict), \
342+
'mapping should be a dict: %r' % mapping
343+
assert isinstance(triples, Iterable), \
344+
"triples not iterable: %r" % triples
340345
triples = set(triples)
341346
assert not triples or isinstance(next(iter(triples)), tuple)
342347
mapping = mapping.copy() if mapping else {}
@@ -782,12 +787,16 @@ def diameter(self):
782787
return nx.diameter(g)
783788

784789
def __add__(self, other):
785-
assert isinstance(other, Iterable)
790+
assert isinstance(other, Iterable), \
791+
"self: %s, other not iterable: %r" % (self, other)
786792
if __debug__ and not isinstance(other, GraphPattern):
787793
try:
788794
it = iter(other)
789795
peek = next(it)
790-
assert isinstance(peek, tuple)
796+
assert isinstance(peek, tuple), \
797+
"self: %sother first element not a tuple %r, other: %r" % (
798+
self, peek, other
799+
)
791800
other = chain((peek,), it)
792801
except StopIteration:
793802
pass
@@ -797,7 +806,8 @@ def __sub__(self, other):
797806
return GraphPattern(set(self) - set(other))
798807

799808
def flip_edge(self, edge_idx):
800-
assert edge_idx < len(self)
809+
assert edge_idx < len(self), \
810+
"edge_idx %d out of bounds: %s" % (edge_idx, self)
801811
e = self[edge_idx]
802812
return GraphPattern(self[:edge_idx] + (e[::-1],) + self[edge_idx + 1:])
803813

@@ -856,9 +866,10 @@ def __init__(self):
856866
self.gt_pairs = set()
857867

858868
def add_graph_pattern(self, gp, stimulus, response):
859-
assert isinstance(gp, GraphPattern)
869+
assert isinstance(gp, GraphPattern), "%r not a GraphPattern" % gp
860870
gtp = (stimulus, response)
861-
assert gtp not in self.gt_pairs
871+
assert gtp not in self.gt_pairs, \
872+
"gtp %r not in gt_pairs: %r" % (gtp, self.gt_pairs)
862873
self.gt_pairs.add(gtp)
863874
identifiers = gp.identifier_counts(exclude_vars=True)
864875
self.identifier_gt_pair_count.update(identifiers.keys())

tests/test_gp_query_online.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_variable_substitution_query():
6767
t, res = variable_substitution_query(
6868
sparql, timeout, gp, Variable('edge'), source_target_pairs, limit)
6969
logger.debug(res.most_common())
70-
assert res.most_common()[0][0] == wpl, 2
70+
assert res and res.most_common()[0][0] == wpl
7171

7272
gp = GraphPattern([
7373
(Variable('var'), wpl, SOURCE_VAR),

0 commit comments

Comments
 (0)