1+ from __future__ import annotations
2+
13import json
2- from typing import IO , Any , Dict
4+ from typing import IO , Any , Dict , Mapping , MutableSequence , Optional
35
4- from rdflib import BNode , Literal , URIRef , Variable
56from rdflib .query import Result , ResultException , ResultParser , ResultSerializer
7+ from rdflib .term import BNode , Identifier , Literal , URIRef , Variable
68
79"""A Serializer for SPARQL results in JSON:
810
1719
1820
1921class JSONResultParser (ResultParser ):
20- def parse (self , source , content_type = None ):
22+ # type error: Signature of "parse" incompatible with supertype "ResultParser"
23+ def parse (self , source : IO , content_type : Optional [str ] = None ) -> Result : # type: ignore[override]
2124 inp = source .read ()
2225 if isinstance (inp , bytes ):
2326 inp = inp .decode ("utf-8" )
2427 return JSONResult (json .loads (inp ))
2528
2629
2730class JSONResultSerializer (ResultSerializer ):
28- def __init__ (self , result ):
31+ def __init__ (self , result : Result ):
2932 ResultSerializer .__init__ (self , result )
3033
31- def serialize (self , stream : IO , encoding : str = None ): # type: ignore[override]
34+ # type error: Signature of "serialize" incompatible with supertype "ResultSerializer"
35+ def serialize (self , stream : IO , encoding : str = None ) -> None : # type: ignore[override]
3236
3337 res : Dict [str , Any ] = {}
3438 if self .result .type == "ASK" :
@@ -49,7 +53,7 @@ def serialize(self, stream: IO, encoding: str = None): # type: ignore[override]
4953 else :
5054 stream .write (r )
5155
52- def _bindingToJSON (self , b ) :
56+ def _bindingToJSON (self , b : Mapping [ Variable , Identifier ]) -> Dict [ Variable , Any ] :
5357 res = {}
5458 for var in b :
5559 j = termToJSON (self , b [var ])
@@ -59,7 +63,7 @@ def _bindingToJSON(self, b):
5963
6064
6165class JSONResult (Result ):
62- def __init__ (self , json ):
66+ def __init__ (self , json : Dict [ str , Any ] ):
6367 self .json = json
6468 if "boolean" in json :
6569 type_ = "ASK"
@@ -76,17 +80,17 @@ def __init__(self, json):
7680 self .bindings = self ._get_bindings ()
7781 self .vars = [Variable (x ) for x in json ["head" ]["vars" ]]
7882
79- def _get_bindings (self ):
80- ret = []
83+ def _get_bindings (self ) -> MutableSequence [ Mapping [ Variable , Identifier ]] :
84+ ret : MutableSequence [ Mapping [ Variable , Identifier ]] = []
8185 for row in self .json ["results" ]["bindings" ]:
82- outRow = {}
86+ outRow : Dict [ Variable , Identifier ] = {}
8387 for k , v in row .items ():
8488 outRow [Variable (k )] = parseJsonTerm (v )
8589 ret .append (outRow )
8690 return ret
8791
8892
89- def parseJsonTerm (d ) :
93+ def parseJsonTerm (d : Dict [ str , str ]) -> Identifier :
9094 """rdflib object (Literal, URIRef, BNode) for the given json-format dict.
9195
9296 input is like:
@@ -107,7 +111,9 @@ def parseJsonTerm(d):
107111 raise NotImplementedError ("json term type %r" % t )
108112
109113
110- def termToJSON (self , term ):
114+ def termToJSON (
115+ self : JSONResultSerializer , term : Optional [Identifier ]
116+ ) -> Optional [Dict [str , str ]]:
111117 if isinstance (term , URIRef ):
112118 return {"type" : "uri" , "value" : str (term )}
113119 elif isinstance (term , Literal ):
0 commit comments