Skip to content

Commit

Permalink
fix: supercharged ConjunctiveGraph.serialize to format as TriG by def…
Browse files Browse the repository at this point in the history
…ault (#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.
  • Loading branch information
WhiteGobo committed Apr 28, 2023
1 parent cbd6151 commit fa3327c
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ def absolutize(self, uri: str, defrag: int = 1) -> URIRef:
# no destination and non-None positional encoding
@overload
def serialize(
self,
self: _GraphT,
destination: None,
format: str,
base: Optional[str],
Expand All @@ -1237,7 +1237,7 @@ def serialize(
# no destination and non-None keyword encoding
@overload
def serialize(
self,
self: _GraphT,
destination: None = ...,
format: str = ...,
base: Optional[str] = ...,
Expand All @@ -1250,7 +1250,7 @@ def serialize(
# no destination and None encoding
@overload
def serialize(
self,
self: _GraphT,
destination: None = ...,
format: str = ...,
base: Optional[str] = ...,
Expand All @@ -1262,25 +1262,25 @@ def serialize(
# non-None destination
@overload
def serialize(
self,
self: _GraphT,
destination: Union[str, pathlib.PurePath, IO[bytes]],
format: str = ...,
base: Optional[str] = ...,
encoding: Optional[str] = ...,
**args: Any,
) -> "Graph":
) -> _GraphT:
...

# fallback
@overload
def serialize(
self,
self: _GraphT,
destination: Optional[Union[str, pathlib.PurePath, IO[bytes]]] = ...,
format: str = ...,
base: Optional[str] = ...,
encoding: Optional[str] = ...,
**args: Any,
) -> Union[bytes, str, "Graph"]:
) -> Union[bytes, str, _GraphT]:
...

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

def serialize( # type: ignore [override]
self: _ConjunctiveGraphT,
destination: Optional[Union[str, pathlib.PurePath, IO[bytes]]] = None,
format: str = "trig",
base: Optional[str] = None,
encoding: Optional[str] = None,
**args: Any,
) -> Union[bytes, str, _ConjunctiveGraphT]:
return super().serialize(destination, format, base, encoding, **args)

def parse(
self,
source: Optional[
Expand Down

0 comments on commit fa3327c

Please sign in to comment.