Skip to content

Commit 5e88541

Browse files
fix namespace prefixes in longturtle serialization (#3106)
* fix namespace prefixes in longturtle serialization * fix: mypy errors --------- Co-authored-by: Edmond <[email protected]>
1 parent a314899 commit 5e88541

File tree

6 files changed

+58
-59
lines changed

6 files changed

+58
-59
lines changed

rdflib/compare.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,24 +442,24 @@ def _traces(
442442
experimental = self._experimental_path(coloring_copy)
443443
experimental_score = set([c.key() for c in experimental])
444444
if last_coloring:
445-
generator = self._create_generator( # type: ignore[unreachable]
445+
generator = self._create_generator(
446446
[last_coloring, experimental], generator
447447
)
448448
last_coloring = experimental
449-
if best_score is None or best_score < color_score: # type: ignore[unreachable]
449+
if best_score is None or best_score < color_score:
450450
best = [refined_coloring]
451451
best_score = color_score
452452
best_experimental_score = experimental_score
453-
elif best_score > color_score: # type: ignore[unreachable]
453+
elif best_score > color_score:
454454
# prune this branch.
455455
if stats is not None:
456-
stats["prunings"] += 1
456+
stats["prunings"] += 1 # type: ignore[operator]
457457
elif experimental_score != best_experimental_score:
458458
best.append(refined_coloring)
459459
else:
460460
# prune this branch.
461461
if stats is not None:
462-
stats["prunings"] += 1
462+
stats["prunings"] += 1 # type: ignore[operator]
463463
discrete: list[list[Color]] = [x for x in best if self._discrete(x)]
464464
if len(discrete) == 0:
465465
best_score = None
@@ -468,7 +468,7 @@ def _traces(
468468
d = [depth[0]]
469469
new_color = self._traces(coloring, stats=stats, depth=d)
470470
color_score = tuple([c.key() for c in refined_coloring])
471-
if best_score is None or color_score > best_score: # type: ignore[unreachable]
471+
if best_score is None or color_score > best_score:
472472
discrete = [new_color]
473473
best_score = color_score
474474
best_depth = d[0]

rdflib/plugins/parsers/jsonld.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,7 @@ def _add_list(
662662
continue
663663

664664
if rest:
665-
# type error: Statement is unreachable
666-
graph.add((subj, RDF.rest, rest)) # type: ignore[unreachable]
665+
graph.add((subj, RDF.rest, rest))
667666
subj = rest
668667

669668
obj = self._to_object(dataset, graph, context, term, node, inlist=True)

rdflib/plugins/serializers/longturtle.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class LongTurtleSerializer(RecursiveSerializer):
4444

4545
def __init__(self, store):
4646
self._ns_rewrite = {}
47+
namespace_manager = store.namespace_manager
4748
store = to_canonical_graph(store)
4849
content = store.serialize(format="application/n-triples")
4950
lines = content.split("\n")
@@ -53,6 +54,7 @@ def __init__(self, store):
5354
data="\n".join(lines), format="application/n-triples", skolemize=True
5455
)
5556
graph = graph.de_skolemize()
57+
graph.namespace_manager = namespace_manager
5658
super(LongTurtleSerializer, self).__init__(graph)
5759
self.keywords = {RDF.type: "a"}
5860
self.reset()

rdflib/store.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ def register(self, object: Any, id: str) -> None:
121121

122122
def loads(self, s: bytes) -> Node:
123123
up = Unpickler(BytesIO(s))
124-
# NOTE on type error: https://github.com/python/mypy/issues/2427
125-
# type error: Cannot assign to a method
126-
up.persistent_load = self._get_object # type: ignore[assignment]
124+
up.persistent_load = self._get_object
127125
try:
128126
return up.load()
129127
except KeyError as e:
@@ -132,9 +130,7 @@ def loads(self, s: bytes) -> Node:
132130
def dumps(self, obj: Node, protocol: Any | None = None, bin: Any | None = None):
133131
src = BytesIO()
134132
p = Pickler(src)
135-
# NOTE on type error: https://github.com/python/mypy/issues/2427
136-
# type error: Cannot assign to a method
137-
p.persistent_id = self._get_ids # type: ignore[assignment]
133+
p.persistent_id = self._get_ids
138134
p.dump(obj)
139135
return src.getvalue()
140136

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,74 @@
1+
PREFIX cn: <https://linked.data.gov.au/def/cn/>
2+
PREFIX ex: <http://example.com/>
13
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
24
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
3-
PREFIX schema: <https://schema.org/>
5+
PREFIX sdo: <https://schema.org/>
46
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
57

6-
<http://example.com/nicholas>
7-
a schema:Person ;
8-
schema:age 41 ;
9-
schema:alternateName
8+
ex:nicholas
9+
a sdo:Person ;
10+
sdo:age 41 ;
11+
sdo:alternateName
1012
[
11-
schema:name "Dr N.J. Car" ;
13+
sdo:name "Dr N.J. Car" ;
1214
] ,
1315
"N.J. Car" ,
1416
"Nick Car" ;
15-
schema:name
17+
sdo:name
1618
[
17-
a <https://linked.data.gov.au/def/cn/CompoundName> ;
18-
schema:hasPart
19+
a cn:CompoundName ;
20+
sdo:hasPart
1921
[
20-
a <https://linked.data.gov.au/def/cn/CompoundName> ;
21-
schema:hasPart
22+
a cn:CompoundName ;
23+
sdo:hasPart
2224
[
23-
a <https://linked.data.gov.au/def/cn/CompoundName> ;
25+
a cn:CompoundName ;
2426
rdf:value "Car" ;
2527
] ,
2628
[
27-
a <https://linked.data.gov.au/def/cn/CompoundName> ;
29+
a cn:CompoundName ;
2830
rdf:value "Maxov" ;
2931
] ;
3032
] ,
3133
[
32-
a <https://linked.data.gov.au/def/cn/CompoundName> ;
34+
a cn:CompoundName ;
3335
rdf:value "Nicholas" ;
3436
] ,
3537
[
36-
a <https://linked.data.gov.au/def/cn/CompoundName> ;
38+
a cn:CompoundName ;
3739
rdf:value "John" ;
3840
] ;
3941
] ;
40-
schema:worksFor <https://kurrawong.ai> ;
42+
sdo:worksFor <https://kurrawong.ai> ;
4143
.
4244

4345
<https://kurrawong.ai>
44-
a schema:Organization ;
45-
schema:location <https://kurrawong.ai/hq> ;
46+
a sdo:Organization ;
47+
sdo:location <https://kurrawong.ai/hq> ;
4648
.
4749

4850
<https://kurrawong.ai/hq>
49-
a schema:Place ;
50-
schema:address
51+
a sdo:Place ;
52+
sdo:address
5153
[
52-
a schema:PostalAddress ;
53-
schema:addressCountry
54+
a sdo:PostalAddress ;
55+
sdo:addressCountry
5456
[
55-
schema:identifier "au" ;
56-
schema:name "Australia" ;
57+
sdo:identifier "au" ;
58+
sdo:name "Australia" ;
5759
] ;
58-
schema:addressLocality "Shorncliffe" ;
59-
schema:addressRegion "QLD" ;
60-
schema:postalCode 4017 ;
61-
schema:streetAddress (
60+
sdo:addressLocality "Shorncliffe" ;
61+
sdo:addressRegion "QLD" ;
62+
sdo:postalCode 4017 ;
63+
sdo:streetAddress (
6264
72
6365
"Yundah"
6466
"Street"
6567
) ;
6668
] ;
67-
schema:geo
69+
sdo:geo
6870
[
69-
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 ;
71+
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 ;
7072
] ;
71-
schema:name "KurrawongAI HQ" ;
73+
sdo:name "KurrawongAI HQ" ;
7274
.

test/test_serializers/test_serializer_longturtle_sort.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,48 +66,48 @@ def test_sort_semiblank_graph() -> None:
6666
serialization_counter[graph_text] += 1
6767

6868
expected_serialization = """\
69-
PREFIX ns1: <http://example.org/ex/>
69+
PREFIX ex: <http://example.org/ex/>
7070
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
7171
72-
ns1:A
72+
ex:A
7373
rdfs:comment "Thing A" ;
7474
.
7575
76-
ns1:C
76+
ex:C
7777
rdfs:comment "Thing C" ;
7878
.
7979
80-
ns1:B
80+
ex:B
8181
rdfs:comment "Thing B" ;
8282
.
8383
84-
[] ns1:has
84+
[] ex:has
8585
[
86-
rdfs:seeAlso ns1:A ;
86+
rdfs:seeAlso ex:A ;
8787
] ;
8888
.
8989
90-
[] rdfs:seeAlso ns1:B ;
90+
[] rdfs:seeAlso ex:B ;
9191
.
9292
93-
[] ns1:has
93+
[] ex:has
9494
[
95-
rdfs:seeAlso ns1:C ;
95+
rdfs:seeAlso ex:C ;
9696
] ;
9797
.
9898
99-
[] rdfs:seeAlso ns1:A ;
99+
[] rdfs:seeAlso ex:A ;
100100
.
101101
102-
[] rdfs:seeAlso ns1:C ;
102+
[] rdfs:seeAlso ex:C ;
103103
.
104104
105-
[] rdfs:seeAlso ns1:B ;
105+
[] rdfs:seeAlso ex:B ;
106106
.
107107
108-
[] ns1:has
108+
[] ex:has
109109
[
110-
rdfs:seeAlso ns1:B ;
110+
rdfs:seeAlso ex:B ;
111111
] ;
112112
.
113113

0 commit comments

Comments
 (0)