Skip to content

Commit

Permalink
Annotate Serializer.serialize and descendants
Browse files Browse the repository at this point in the history
This patch aligns the type signatures on `Serializer` subclasses,
including renaming the arbitrary-keywords dictionary to always be
`**kwargs`.  This is in part to prepare for the possibility of adding
`*args` as a positional-argument delimiter.

References:
* RDFLib#1890 (comment)

Signed-off-by: Alex Nelson <[email protected]>
  • Loading branch information
ajnelson-nist committed Nov 4, 2024
1 parent 086a4f7 commit 2dfdfc0
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 29 deletions.
4 changes: 2 additions & 2 deletions rdflib/plugins/serializers/hext.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def serialize(
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = "utf-8",
**kwargs,
):
**kwargs: Any,
) -> None:
if base is not None:
warnings.warn(
"base has no meaning for Hextuples serialization. "
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/serializers/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def serialize(
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = None,
**kwargs,
):
**kwargs: Any,
) -> None:
# TODO: docstring w. args and return value
encoding = encoding or "utf-8"
if encoding not in ("utf-8", "utf-16"):
Expand Down
15 changes: 13 additions & 2 deletions rdflib/plugins/serializers/longturtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
- Nicholas Car, 2023
"""

from __future__ import annotations

from typing import IO, Any, Optional

from rdflib.exceptions import Error
from rdflib.namespace import RDF
from rdflib.term import BNode, Literal, URIRef
Expand All @@ -42,7 +46,7 @@ def __init__(self, store):
self.keywords = {RDF.type: "a"}
self.reset()
self.stream = None
self._spacious = _SPACIOUS_OUTPUT
self._spacious: bool = _SPACIOUS_OUTPUT

def addNamespace(self, prefix, namespace):
# Turtle does not support prefixes that start with _
Expand Down Expand Up @@ -74,7 +78,14 @@ def reset(self):
self._started = False
self._ns_rewrite = {}

def serialize(self, stream, base=None, encoding=None, spacious=None, **args):
def serialize(
self,
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = None,
spacious: Optional[bool] = None,
**kwargs: Any,
) -> None:
self.reset()
self.stream = stream
# if base is given here, use, if not and a base is set for the graph use that
Expand Down
6 changes: 3 additions & 3 deletions rdflib/plugins/serializers/nquads.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import warnings
from typing import IO, Optional
from typing import IO, Any, Optional

from rdflib.graph import ConjunctiveGraph, Graph
from rdflib.plugins.serializers.nt import _quoteLiteral
Expand All @@ -26,8 +26,8 @@ def serialize(
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = None,
**args,
):
**kwargs: Any,
) -> None:
if base is not None:
warnings.warn("NQuadsSerializer does not support base.")
if encoding is not None and encoding.lower() != self.encoding.lower():
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/serializers/nt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import codecs
import warnings
from typing import IO, TYPE_CHECKING, Optional
from typing import IO, TYPE_CHECKING, Any, Optional

from rdflib.graph import Graph
from rdflib.serializer import Serializer
Expand Down Expand Up @@ -33,7 +33,7 @@ def serialize(
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = "utf-8",
**args,
**kwargs: Any,
) -> None:
if base is not None:
warnings.warn("NTSerializer does not support base.")
Expand Down
6 changes: 3 additions & 3 deletions rdflib/plugins/serializers/patch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import warnings
from typing import IO, Optional
from typing import IO, Any, Optional
from uuid import uuid4

from rdflib import Dataset
Expand Down Expand Up @@ -32,8 +32,8 @@ def serialize(
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = None,
**kwargs,
):
**kwargs: Any,
) -> None:
"""
Serialize the store to the given stream.
:param stream: The stream to serialize to.
Expand Down
16 changes: 8 additions & 8 deletions rdflib/plugins/serializers/rdfxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import xml.dom.minidom
from collections.abc import Generator
from typing import IO, TYPE_CHECKING, Optional
from typing import IO, TYPE_CHECKING, Any, Optional
from xml.sax.saxutils import escape, quoteattr

from rdflib.collection import Collection
Expand Down Expand Up @@ -50,7 +50,7 @@ def serialize(
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = None,
**args,
**kwargs: Any,
) -> None:
# if base is given here, use that, if not and a base is set for the graph use that
if base is not None:
Expand All @@ -69,8 +69,8 @@ def serialize(
write("<rdf:RDF\n")

# If provided, write xml:base attribute for the RDF
if "xml_base" in args:
write(' xml:base="%s"\n' % args["xml_base"])
if "xml_base" in kwargs:
write(' xml:base="%s"\n' % kwargs["xml_base"])
elif self.base:
write(' xml:base="%s"\n' % self.base)
# TODO:
Expand Down Expand Up @@ -176,7 +176,7 @@ def serialize(
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = None,
**args,
**kwargs: Any,
) -> None:
self.__serialized: dict[IdentifiedNode | Literal, int] = {}
store = self.store
Expand All @@ -185,7 +185,7 @@ def serialize(
self.base = base
elif store.base is not None:
self.base = store.base
self.max_depth = args.get("max_depth", 3)
self.max_depth = kwargs.get("max_depth", 3)
assert self.max_depth > 0, "max_depth must be greater than 0"

self.nm = nm = store.namespace_manager
Expand All @@ -205,8 +205,8 @@ def serialize(

writer.push(RDFVOC.RDF)

if "xml_base" in args:
writer.attribute(XMLBASE, args["xml_base"])
if "xml_base" in kwargs:
writer.attribute(XMLBASE, kwargs["xml_base"])
elif self.base:
writer.attribute(XMLBASE, self.base)

Expand Down
6 changes: 3 additions & 3 deletions rdflib/plugins/serializers/trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from __future__ import annotations

from typing import IO, TYPE_CHECKING, Optional, Union
from typing import IO, TYPE_CHECKING, Any, Optional, Union

from rdflib.graph import ConjunctiveGraph, Graph
from rdflib.plugins.serializers.turtle import TurtleSerializer
Expand Down Expand Up @@ -67,8 +67,8 @@ def serialize(
base: Optional[str] = None,
encoding: Optional[str] = None,
spacious: Optional[bool] = None,
**args,
):
**kwargs: Any,
) -> None:
self.reset()
self.stream = stream
# if base is given here, use that, if not and a base is set for the graph use that
Expand Down
6 changes: 3 additions & 3 deletions rdflib/plugins/serializers/trix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import IO, Optional
from typing import IO, Any, Optional

from rdflib.graph import ConjunctiveGraph, Graph
from rdflib.namespace import Namespace
Expand Down Expand Up @@ -28,8 +28,8 @@ def serialize(
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = None,
**args,
):
**kwargs: Any,
) -> None:
nm = self.store.namespace_manager

self.writer = XMLWriter(stream, nm, encoding, extra_ns={"": TRIXNS})
Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/serializers/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def serialize(
base: Optional[str] = None,
encoding: Optional[str] = None,
spacious: Optional[bool] = None,
**args: Any,
**kwargs: Any,
) -> None:
self.reset()
self.stream = stream
Expand Down

0 comments on commit 2dfdfc0

Please sign in to comment.