From 06bd927eb0d0a4cad6c77c16abd576cc07bdf721 Mon Sep 17 00:00:00 2001 From: Alex Nelson Date: Mon, 4 Nov 2024 15:49:23 -0500 Subject: [PATCH] Annotate Serializer.serialize and descendants (#2970) 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: * https://github.com/RDFLib/rdflib/issues/1890#issuecomment-1144208982 Signed-off-by: Alex Nelson --- rdflib/plugins/serializers/hext.py | 4 ++-- rdflib/plugins/serializers/jsonld.py | 4 ++-- rdflib/plugins/serializers/longturtle.py | 15 +++++++++++++-- rdflib/plugins/serializers/nquads.py | 6 +++--- rdflib/plugins/serializers/nt.py | 4 ++-- rdflib/plugins/serializers/patch.py | 6 +++--- rdflib/plugins/serializers/rdfxml.py | 16 ++++++++-------- rdflib/plugins/serializers/trig.py | 6 +++--- rdflib/plugins/serializers/trix.py | 6 +++--- rdflib/plugins/serializers/turtle.py | 2 +- 10 files changed, 40 insertions(+), 29 deletions(-) diff --git a/rdflib/plugins/serializers/hext.py b/rdflib/plugins/serializers/hext.py index 3f102395b..250e96cb9 100644 --- a/rdflib/plugins/serializers/hext.py +++ b/rdflib/plugins/serializers/hext.py @@ -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. " diff --git a/rdflib/plugins/serializers/jsonld.py b/rdflib/plugins/serializers/jsonld.py index ecf43f365..a259b09b7 100644 --- a/rdflib/plugins/serializers/jsonld.py +++ b/rdflib/plugins/serializers/jsonld.py @@ -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"): diff --git a/rdflib/plugins/serializers/longturtle.py b/rdflib/plugins/serializers/longturtle.py index e886574f3..29e282d78 100644 --- a/rdflib/plugins/serializers/longturtle.py +++ b/rdflib/plugins/serializers/longturtle.py @@ -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 @@ -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 _ @@ -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 diff --git a/rdflib/plugins/serializers/nquads.py b/rdflib/plugins/serializers/nquads.py index 3c8d02ccc..b74b9cab5 100644 --- a/rdflib/plugins/serializers/nquads.py +++ b/rdflib/plugins/serializers/nquads.py @@ -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 @@ -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(): diff --git a/rdflib/plugins/serializers/nt.py b/rdflib/plugins/serializers/nt.py index f756b5068..8e11fe66e 100644 --- a/rdflib/plugins/serializers/nt.py +++ b/rdflib/plugins/serializers/nt.py @@ -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 @@ -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.") diff --git a/rdflib/plugins/serializers/patch.py b/rdflib/plugins/serializers/patch.py index 3a5d37215..1bc5ff41f 100644 --- a/rdflib/plugins/serializers/patch.py +++ b/rdflib/plugins/serializers/patch.py @@ -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 @@ -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. diff --git a/rdflib/plugins/serializers/rdfxml.py b/rdflib/plugins/serializers/rdfxml.py index 7001bf1c6..e122c1398 100644 --- a/rdflib/plugins/serializers/rdfxml.py +++ b/rdflib/plugins/serializers/rdfxml.py @@ -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 @@ -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: @@ -69,8 +69,8 @@ def serialize( write(" None: self.__serialized: dict[IdentifiedNode | Literal, int] = {} store = self.store @@ -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 @@ -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) diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py index c072fc8fa..38cc31591 100644 --- a/rdflib/plugins/serializers/trig.py +++ b/rdflib/plugins/serializers/trig.py @@ -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 @@ -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 diff --git a/rdflib/plugins/serializers/trix.py b/rdflib/plugins/serializers/trix.py index 008360e6b..95730e8fb 100644 --- a/rdflib/plugins/serializers/trix.py +++ b/rdflib/plugins/serializers/trix.py @@ -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 @@ -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}) diff --git a/rdflib/plugins/serializers/turtle.py b/rdflib/plugins/serializers/turtle.py index d31ba9a22..fba8600df 100644 --- a/rdflib/plugins/serializers/turtle.py +++ b/rdflib/plugins/serializers/turtle.py @@ -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