Skip to content

Commit fa3327c

Browse files
author
WhiteGobo
committed
fix: supercharged ConjunctiveGraph.serialize to format as TriG by default (#1674)
Created method ConjunctiveGraph.serialize(..., format='trig', ...). Because of typing problems I replaced some annotations in the overloaded methods of rdflib.Graph.serialize, so that in subclasses serialize knows, that it returns type(self) and not Graph. Because Graph.serialize annotations are still incompatible with ConjunctiveGraph.serialize added type: ignore [overriden] to let mypy skip that method.
1 parent cbd6151 commit fa3327c

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

rdflib/graph.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ def absolutize(self, uri: str, defrag: int = 1) -> URIRef:
12251225
# no destination and non-None positional encoding
12261226
@overload
12271227
def serialize(
1228-
self,
1228+
self: _GraphT,
12291229
destination: None,
12301230
format: str,
12311231
base: Optional[str],
@@ -1237,7 +1237,7 @@ def serialize(
12371237
# no destination and non-None keyword encoding
12381238
@overload
12391239
def serialize(
1240-
self,
1240+
self: _GraphT,
12411241
destination: None = ...,
12421242
format: str = ...,
12431243
base: Optional[str] = ...,
@@ -1250,7 +1250,7 @@ def serialize(
12501250
# no destination and None encoding
12511251
@overload
12521252
def serialize(
1253-
self,
1253+
self: _GraphT,
12541254
destination: None = ...,
12551255
format: str = ...,
12561256
base: Optional[str] = ...,
@@ -1262,25 +1262,25 @@ def serialize(
12621262
# non-None destination
12631263
@overload
12641264
def serialize(
1265-
self,
1265+
self: _GraphT,
12661266
destination: Union[str, pathlib.PurePath, IO[bytes]],
12671267
format: str = ...,
12681268
base: Optional[str] = ...,
12691269
encoding: Optional[str] = ...,
12701270
**args: Any,
1271-
) -> "Graph":
1271+
) -> _GraphT:
12721272
...
12731273

12741274
# fallback
12751275
@overload
12761276
def serialize(
1277-
self,
1277+
self: _GraphT,
12781278
destination: Optional[Union[str, pathlib.PurePath, IO[bytes]]] = ...,
12791279
format: str = ...,
12801280
base: Optional[str] = ...,
12811281
encoding: Optional[str] = ...,
12821282
**args: Any,
1283-
) -> Union[bytes, str, "Graph"]:
1283+
) -> Union[bytes, str, _GraphT]:
12841284
...
12851285

12861286
def serialize(
@@ -2193,6 +2193,16 @@ def context_id(self, uri: str, context_id: Optional[str] = None) -> URIRef:
21932193
context_id = "#context"
21942194
return URIRef(context_id, base=uri)
21952195

2196+
def serialize( # type: ignore [override]
2197+
self: _ConjunctiveGraphT,
2198+
destination: Optional[Union[str, pathlib.PurePath, IO[bytes]]] = None,
2199+
format: str = "trig",
2200+
base: Optional[str] = None,
2201+
encoding: Optional[str] = None,
2202+
**args: Any,
2203+
) -> Union[bytes, str, _ConjunctiveGraphT]:
2204+
return super().serialize(destination, format, base, encoding, **args)
2205+
21962206
def parse(
21972207
self,
21982208
source: Optional[

0 commit comments

Comments
 (0)