3737from __future__ import annotations
3838
3939import warnings
40- from typing import IO , Optional
40+ from typing import IO , TYPE_CHECKING , Any , Dict , List , Optional
4141
4242from rdflib .graph import Graph
4343from rdflib .namespace import RDF , XSD
4444from rdflib .serializer import Serializer
45- from rdflib .term import BNode , Literal , URIRef
45+ from rdflib .term import BNode , IdentifiedNode , Identifier , Literal , URIRef
4646
4747from ..shared .jsonld .context import UNDEF , Context
4848from ..shared .jsonld .keys import CONTEXT , GRAPH , ID , LANG , LIST , SET , VOCAB
4949from ..shared .jsonld .util import json
5050
51+ if TYPE_CHECKING :
52+ pass
53+
5154__all__ = ["JsonLDSerializer" , "from_rdf" ]
5255
5356
@@ -140,12 +143,12 @@ def from_rdf(
140143
141144
142145class Converter :
143- def __init__ (self , context , use_native_types , use_rdf_type ):
146+ def __init__ (self , context : Context , use_native_types : bool , use_rdf_type : bool ):
144147 self .context = context
145148 self .use_native_types = context .active or use_native_types
146149 self .use_rdf_type = use_rdf_type
147150
148- def convert (self , graph ):
151+ def convert (self , graph : Graph ):
149152 # TODO: bug in rdflib dataset parsing (nquads et al):
150153 # plain triples end up in separate unnamed graphs (rdflib issue #436)
151154 if graph .context_aware :
@@ -161,7 +164,7 @@ def convert(self, graph):
161164
162165 context = self .context
163166
164- objs = []
167+ objs : List [ Any ] = []
165168 for g in graphs :
166169 obj = {}
167170 graphname = None
@@ -194,8 +197,8 @@ def convert(self, graph):
194197
195198 return objs
196199
197- def from_graph (self , graph ):
198- nodemap = {}
200+ def from_graph (self , graph : Graph ):
201+ nodemap : Dict [ Any , Any ] = {}
199202
200203 for s in set (graph .subjects ()):
201204 ## only iri:s and unreferenced (rest will be promoted to top if needed)
@@ -206,12 +209,13 @@ def from_graph(self, graph):
206209
207210 return list (nodemap .values ())
208211
209- def process_subject (self , graph , s , nodemap ):
212+ def process_subject (self , graph : Graph , s : IdentifiedNode , nodemap ):
210213 if isinstance (s , URIRef ):
211214 node_id = self .context .shrink_iri (s )
212215 elif isinstance (s , BNode ):
213216 node_id = s .n3 ()
214217 else :
218+ # This does not seem right, this probably should be an error.
215219 node_id = None
216220
217221 # used_as_object = any(graph.subjects(None, s))
@@ -227,7 +231,15 @@ def process_subject(self, graph, s, nodemap):
227231
228232 return node
229233
230- def add_to_node (self , graph , s , p , o , s_node , nodemap ):
234+ def add_to_node (
235+ self ,
236+ graph : Graph ,
237+ s : IdentifiedNode ,
238+ p : IdentifiedNode ,
239+ o : Identifier ,
240+ s_node : Dict [str , Any ],
241+ nodemap ,
242+ ):
231243 context = self .context
232244
233245 if isinstance (o , Literal ):
@@ -302,7 +314,7 @@ def add_to_node(self, graph, s, p, o, s_node, nodemap):
302314 value = node
303315 s_node [p_key ] = value
304316
305- def type_coerce (self , o , coerce_type ):
317+ def type_coerce (self , o : IdentifiedNode , coerce_type : str ):
306318 if coerce_type == ID :
307319 if isinstance (o , URIRef ):
308320 return self .context .shrink_iri (o )
@@ -317,7 +329,9 @@ def type_coerce(self, o, coerce_type):
317329 else :
318330 return None
319331
320- def to_raw_value (self , graph , s , o , nodemap ):
332+ def to_raw_value (
333+ self , graph : Graph , s : IdentifiedNode , o : Identifier , nodemap : Dict [str , Any ]
334+ ):
321335 context = self .context
322336 coll = self .to_collection (graph , o )
323337 if coll is not None :
@@ -364,7 +378,7 @@ def to_raw_value(self, graph, s, o, nodemap):
364378 else :
365379 return v
366380
367- def to_collection (self , graph , l_ ):
381+ def to_collection (self , graph : Graph , l_ : Identifier ):
368382 if l_ != RDF .nil and not graph .value (l_ , RDF .first ):
369383 return None
370384 list_nodes = []
0 commit comments