Skip to content

Commit

Permalink
Add documentation and type hints for rdflib.query.Result. Update docu…
Browse files Browse the repository at this point in the history
…mentation and type hints for rdflib.graph.Graph.
  • Loading branch information
edmondchuc committed Dec 8, 2020
1 parent c5ff127 commit 2077524
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
10 changes: 5 additions & 5 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,13 +1130,13 @@ def load(self, source, publicID=None, format="xml"):
def query(
self,
query_object,
processor="sparql",
result="sparql",
processor: str = "sparql",
result: str = "sparql",
initNs=None,
initBindings=None,
use_store_provided=True,
use_store_provided: bool = True,
**kwargs
):
) -> query.Result:
"""
Query this graph.
Expand All @@ -1147,7 +1147,7 @@ def query(
if none are given, the namespaces from the graph's namespace manager
are used.
:returntype: rdflib.query.QueryResult
:returntype: rdflib.query.Result
"""

Expand Down
26 changes: 24 additions & 2 deletions rdflib/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tempfile
import warnings
import types
from typing import Optional

from io import BytesIO

Expand Down Expand Up @@ -205,8 +206,29 @@ def parse(source=None, format=None, content_type=None, **kwargs):

return parser.parse(source, content_type=content_type, **kwargs)

def serialize(self, destination=None, encoding="utf-8", format="xml", **args):

def serialize(
self,
destination: Optional[str] = None,
encoding: str = "utf-8",
format: str = "xml",
**args,
) -> Optional[bytes]:
"""
Serialize the query result.
The :code:`format` argument determines the Serializer class to use.
- csv: :class:`~rdflib.plugins.sparql.results.csvresults.CSVResultSerializer`
- json: :class:`~rdflib.plugins.sparql.results.jsonresults.JSONResultSerializer`
- txt: :class:`~rdflib.plugins.sparql.results.txtresults.TXTResultSerializer`
- xml: :class:`~rdflib.plugins.sparql.results.xmlresults.XMLResultSerializer`
:param destination: Path of file output.
:param encoding: Encoding of output.
:param format: One of ['csv', 'json', 'txt', xml']
:param args:
:return: bytes
"""
if self.type in ("CONSTRUCT", "DESCRIBE"):
return self.graph.serialize(
destination, encoding=encoding, format=format, **args
Expand Down

0 comments on commit 2077524

Please sign in to comment.