Skip to content

fix namespace prefixes in longturtle serialization #3106

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions rdflib/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,24 +442,24 @@ def _traces(
experimental = self._experimental_path(coloring_copy)
experimental_score = set([c.key() for c in experimental])
if last_coloring:
generator = self._create_generator( # type: ignore[unreachable]
generator = self._create_generator(
[last_coloring, experimental], generator
)
last_coloring = experimental
if best_score is None or best_score < color_score: # type: ignore[unreachable]
if best_score is None or best_score < color_score:
best = [refined_coloring]
best_score = color_score
best_experimental_score = experimental_score
elif best_score > color_score: # type: ignore[unreachable]
elif best_score > color_score:
# prune this branch.
if stats is not None:
stats["prunings"] += 1
stats["prunings"] += 1 # type: ignore[operator]
elif experimental_score != best_experimental_score:
best.append(refined_coloring)
else:
# prune this branch.
if stats is not None:
stats["prunings"] += 1
stats["prunings"] += 1 # type: ignore[operator]
discrete: list[list[Color]] = [x for x in best if self._discrete(x)]
if len(discrete) == 0:
best_score = None
Expand All @@ -468,7 +468,7 @@ def _traces(
d = [depth[0]]
new_color = self._traces(coloring, stats=stats, depth=d)
color_score = tuple([c.key() for c in refined_coloring])
if best_score is None or color_score > best_score: # type: ignore[unreachable]
if best_score is None or color_score > best_score:
discrete = [new_color]
best_score = color_score
best_depth = d[0]
Expand Down
3 changes: 1 addition & 2 deletions rdflib/plugins/parsers/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,7 @@ def _add_list(
continue

if rest:
# type error: Statement is unreachable
graph.add((subj, RDF.rest, rest)) # type: ignore[unreachable]
graph.add((subj, RDF.rest, rest))
subj = rest

obj = self._to_object(dataset, graph, context, term, node, inlist=True)
Expand Down
2 changes: 2 additions & 0 deletions rdflib/plugins/serializers/longturtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class LongTurtleSerializer(RecursiveSerializer):

def __init__(self, store):
self._ns_rewrite = {}
namespace_manager = store.namespace_manager
store = to_canonical_graph(store)
content = store.serialize(format="application/n-triples")
lines = content.split("\n")
Expand All @@ -53,6 +54,7 @@ def __init__(self, store):
data="\n".join(lines), format="application/n-triples", skolemize=True
)
graph = graph.de_skolemize()
graph.namespace_manager = namespace_manager
super(LongTurtleSerializer, self).__init__(graph)
self.keywords = {RDF.type: "a"}
self.reset()
Expand Down
8 changes: 2 additions & 6 deletions rdflib/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ def register(self, object: Any, id: str) -> None:

def loads(self, s: bytes) -> Node:
up = Unpickler(BytesIO(s))
# NOTE on type error: https://github.com/python/mypy/issues/2427
# type error: Cannot assign to a method
up.persistent_load = self._get_object # type: ignore[assignment]
up.persistent_load = self._get_object
try:
return up.load()
except KeyError as e:
Expand All @@ -132,9 +130,7 @@ def loads(self, s: bytes) -> Node:
def dumps(self, obj: Node, protocol: Any | None = None, bin: Any | None = None):
src = BytesIO()
p = Pickler(src)
# NOTE on type error: https://github.com/python/mypy/issues/2427
# type error: Cannot assign to a method
p.persistent_id = self._get_ids # type: ignore[assignment]
p.persistent_id = self._get_ids
p.dump(obj)
return src.getvalue()

Expand Down
64 changes: 33 additions & 31 deletions test/data/longturtle/longturtle-target.ttl
Original file line number Diff line number Diff line change
@@ -1,72 +1,74 @@
PREFIX cn: <https://linked.data.gov.au/def/cn/>
PREFIX ex: <http://example.com/>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX schema: <https://schema.org/>
PREFIX sdo: <https://schema.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

<http://example.com/nicholas>
a schema:Person ;
schema:age 41 ;
schema:alternateName
ex:nicholas
a sdo:Person ;
sdo:age 41 ;
sdo:alternateName
[
schema:name "Dr N.J. Car" ;
sdo:name "Dr N.J. Car" ;
] ,
"N.J. Car" ,
"Nick Car" ;
schema:name
sdo:name
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
schema:hasPart
a cn:CompoundName ;
sdo:hasPart
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
schema:hasPart
a cn:CompoundName ;
sdo:hasPart
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
a cn:CompoundName ;
rdf:value "Car" ;
] ,
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
a cn:CompoundName ;
rdf:value "Maxov" ;
] ;
] ,
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
a cn:CompoundName ;
rdf:value "Nicholas" ;
] ,
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
a cn:CompoundName ;
rdf:value "John" ;
] ;
] ;
schema:worksFor <https://kurrawong.ai> ;
sdo:worksFor <https://kurrawong.ai> ;
.

<https://kurrawong.ai>
a schema:Organization ;
schema:location <https://kurrawong.ai/hq> ;
a sdo:Organization ;
sdo:location <https://kurrawong.ai/hq> ;
.

<https://kurrawong.ai/hq>
a schema:Place ;
schema:address
a sdo:Place ;
sdo:address
[
a schema:PostalAddress ;
schema:addressCountry
a sdo:PostalAddress ;
sdo:addressCountry
[
schema:identifier "au" ;
schema:name "Australia" ;
sdo:identifier "au" ;
sdo:name "Australia" ;
] ;
schema:addressLocality "Shorncliffe" ;
schema:addressRegion "QLD" ;
schema:postalCode 4017 ;
schema:streetAddress (
sdo:addressLocality "Shorncliffe" ;
sdo:addressRegion "QLD" ;
sdo:postalCode 4017 ;
sdo:streetAddress (
72
"Yundah"
"Street"
) ;
] ;
schema:geo
sdo:geo
[
schema:polygon "POLYGON((153.082403 -27.325801, 153.08241 -27.32582, 153.082943 -27.325612, 153.083010 -27.325742, 153.083543 -27.325521, 153.083456 -27.325365, 153.082403 -27.325801))"^^geo:wktLiteral ;
sdo:polygon "POLYGON((153.082403 -27.325801, 153.08241 -27.32582, 153.082943 -27.325612, 153.083010 -27.325742, 153.083543 -27.325521, 153.083456 -27.325365, 153.082403 -27.325801))"^^geo:wktLiteral ;
] ;
schema:name "KurrawongAI HQ" ;
sdo:name "KurrawongAI HQ" ;
.
28 changes: 14 additions & 14 deletions test/test_serializers/test_serializer_longturtle_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,48 +66,48 @@ def test_sort_semiblank_graph() -> None:
serialization_counter[graph_text] += 1

expected_serialization = """\
PREFIX ns1: <http://example.org/ex/>
PREFIX ex: <http://example.org/ex/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

ns1:A
ex:A
rdfs:comment "Thing A" ;
.

ns1:C
ex:C
rdfs:comment "Thing C" ;
.

ns1:B
ex:B
rdfs:comment "Thing B" ;
.

[] ns1:has
[] ex:has
[
rdfs:seeAlso ns1:A ;
rdfs:seeAlso ex:A ;
] ;
.

[] rdfs:seeAlso ns1:B ;
[] rdfs:seeAlso ex:B ;
.

[] ns1:has
[] ex:has
[
rdfs:seeAlso ns1:C ;
rdfs:seeAlso ex:C ;
] ;
.

[] rdfs:seeAlso ns1:A ;
[] rdfs:seeAlso ex:A ;
.

[] rdfs:seeAlso ns1:C ;
[] rdfs:seeAlso ex:C ;
.

[] rdfs:seeAlso ns1:B ;
[] rdfs:seeAlso ex:B ;
.

[] ns1:has
[] ex:has
[
rdfs:seeAlso ns1:B ;
rdfs:seeAlso ex:B ;
] ;
.

Expand Down
Loading