Skip to content
85 changes: 78 additions & 7 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,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 @@ -1239,7 +1239,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 @@ -1251,7 +1251,7 @@ def serialize(
# no destination and None encoding
@overload
def serialize(
self,
self: _GraphT,
destination: None = ...,
format: str = ...,
base: Optional[str] = ...,
Expand All @@ -1262,24 +1262,24 @@ 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(
self: _GraphT,
Expand Down Expand Up @@ -2216,6 +2216,77 @@ def context_id(self, uri: str, context_id: Optional[str] = None) -> URIRef:
context_id = "#context"
return URIRef(context_id, base=uri)

# no destination and non-None positional encoding
@overload
def serialize(
self: _ConjunctiveGraphT,
destination: None,
format: str,
base: Optional[str],
encoding: str,
**args: Any,
) -> bytes:
...

# no destination and non-None keyword encoding
@overload
def serialize(
self: _ConjunctiveGraphT,
destination: None = ...,
format: str = ...,
base: Optional[str] = ...,
*,
encoding: str,
**args: Any,
) -> bytes:
...

# no destination and None encoding
@overload
def serialize(
self: _ConjunctiveGraphT,
destination: None = ...,
format: str = ...,
base: Optional[str] = ...,
encoding: None = ...,
**args: Any,
) -> str:
...

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

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

def serialize(
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