Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Python PEP 440 Versioning](https://www.python.org/d
### Fixed
- SPARQL constraint validation no longer misidentifies `VALUES` in property paths or predicate names.
- Fixes #301
- Validation report text output is now deterministic.
- Fixes #304

## [0.30.1] - 2025-03-15

Expand Down
6 changes: 4 additions & 2 deletions pyshacl/constraints/constraint_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ def make_v_result_description(
sc_text = stringify_node(sg, source_constraint)
desc += "\tSource Constraint: {}\n".format(sc_text)
if extra_messages:
for m in iter(extra_messages):
sorted_extra_messages = sorted(extra_messages, key=lambda m: str(m))
for m in iter(sorted_extra_messages):
if m in messages:
continue
if isinstance(m, Literal):
Expand All @@ -206,7 +207,8 @@ def make_v_result_description(
desc += "\tMessage: {}\n".format(msg)
else: # pragma: no cover
desc += "\tMessage: {}\n".format(str(m))
for m in messages:
sorted_messages = sorted(messages, key=lambda m: str(m))
for m in sorted_messages:
if isinstance(m, Literal):
msg = str(m.value)
if bound_vars is not None:
Expand Down
5 changes: 4 additions & 1 deletion pyshacl/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ def create_validation_report(cls, sg, conforms: bool, results: List[Tuple]):
vg.add((vr, RDF_type, SH_ValidationReport))
vg.add((vr, SH_conforms, Literal(conforms)))
cloned_nodes: Dict[Tuple[GraphLike, str], Union[BNode, URIRef]] = {}
for result in iter(results):
text_results = sorted(results, key=lambda r: r[0])
for result in iter(text_results):
_d, _bn, _tr = result
v_text += _d
for result in iter(results):
_d, _bn, _tr = result
vg.add((vr, SH_result, _bn))
for tr in iter(_tr):
s, p, o = tr
Expand Down
114 changes: 114 additions & 0 deletions test/issues/test_304.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# -*- coding: utf-8 -*-
#
"""
https://github.com/RDFLib/pySHACL/issues/304
"""
import os
import subprocess
import sys
import textwrap


DATA_TTL = """\
@prefix ex: <http://example.org/> .

ex:node1 a ex:Thing ;
ex:list ( "a" "b" ) ;
ex:code "X-1" ;
ex:flag true .

ex:node2 a ex:Thing ;
ex:list ( "c" "d" ) ;
ex:code "BAD" .

ex:node3 a ex:Thing ;
ex:list ( "e" ) .

ex:node4 a ex:Other ;
ex:label 42 ;
ex:ref ex:node1 .

ex:node5 a ex:Other ;
ex:label "ok" ;
ex:ref ex:node2 ;
ex:ref ex:node3 .
"""

SHAPES_TTL = """\
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix ex: <http://example.org/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:ThingShape
a sh:NodeShape ;
sh:targetClass ex:Thing ;
sh:property [
sh:path ex:list ;
sh:datatype xsd:integer ;
sh:message "List items must be integers." ;
] ;
sh:property [
sh:path ex:code ;
sh:pattern "^X-" ;
sh:message "Code must start with X-" ;
] ;
sh:property [
sh:path ex:flag ;
sh:minCount 1 ;
sh:message "Flag is required." ;
] .

ex:OtherShape
a sh:NodeShape ;
sh:targetClass ex:Other ;
sh:property [
sh:path ex:label ;
sh:datatype xsd:string ;
sh:message "Label must be a string." ;
] ;
sh:property [
sh:path ex:ref ;
sh:maxCount 1 ;
sh:message "Only one ref allowed." ;
] .
"""


def _run_validate_with_seed(seed: str) -> str:
script = textwrap.dedent(
f"""
from rdflib import Graph
from pyshacl import validate

data = {DATA_TTL!r}
shapes = {SHAPES_TTL!r}
data_g = Graph().parse(data=data, format="turtle")
shapes_g = Graph().parse(data=shapes, format="turtle")
conforms, report_graph, results_text = validate(
data_g,
shacl_graph=shapes_g,
advanced=False,
debug=False,
)
print(results_text)
"""
)
env = os.environ.copy()
env["PYTHONHASHSEED"] = seed
result = subprocess.run(
[sys.executable, "-c", script],
capture_output=True,
text=True,
check=True,
env=env,
)
return result.stdout


def test_304():
out_a = _run_validate_with_seed("1")
out_b = _run_validate_with_seed("2")
out_c = _run_validate_with_seed("3")
assert "Results (" in out_a
assert out_a == out_b
assert out_a == out_c
Loading