1919from rdflib .namespace import RDF , RDFS , OWL
2020from rdflib .plugins .parsers .notation3 import BadSyntax
2121import xml .sax
22- from typing import cast , Union , Tuple , Dict , Any , Callable , Iterable
22+ from typing import Any , AnyStr , Callable , cast , Dict , List , Iterable , Tuple , TypeVar , Union
2323
2424_logger = logging .getLogger ("salad" )
2525
@@ -240,7 +240,7 @@ def add_context(self, newcontext, baseuri=""):
240240 _logger .debug ("vocab is %s" , self .vocab )
241241
242242 def resolve_ref (self , ref , base_url = None , toplevel = True ):
243- # type: (Union[Dict[str, Any], str, unicode], Union[str, unicode]) -> Tuple[Union[Dict[str, Any], str, unicode], Dict[str, Any]]
243+ # type: (Union[Dict[str, Any], str, unicode], Union[str, unicode], bool ) -> Tuple[Union[Dict[str, Any], str, unicode], Dict[str, Any]]
244244 base_url = base_url or 'file://%s/' % os .path .abspath ('.' )
245245
246246 obj = None # type: Dict[str, Any]
@@ -323,7 +323,7 @@ def resolve_ref(self, ref, base_url=None, toplevel=True):
323323 return obj , metadata
324324
325325 def resolve_all (self , document , base_url , file_base = None , toplevel = True ):
326- # type: (Any, Union[str, unicode], Union[str, unicode]) -> Tuple[Any, Dict[str, Any]]
326+ # type: (Any, Union[str, unicode], Union[str, unicode], bool ) -> Tuple[Any, Dict[str, Any]]
327327 loader = self
328328 metadata = {} # type: Dict[str, Any]
329329 if file_base is None :
@@ -531,8 +531,10 @@ def check_file(self, fn): # type: (Union[str, unicode]) -> bool
531531 else :
532532 return False
533533
534+ FieldType = TypeVar ('FieldType' , unicode , List [str ], Dict [str , Any ])
535+
534536 def validate_link (self , field , link , docid ):
535- # type: (str, Union[str, unicode, List[str], Dict[str, Any]] ) -> bool
537+ # type: (AnyStr, FieldType, AnyStr ) -> FieldType
536538 if field in self .nolinkcheck :
537539 return link
538540 if isinstance (link , (str , unicode )):
@@ -546,11 +548,10 @@ def validate_link(self, field, link, docid):
546548 split = urlparse .urlsplit (docid )
547549 sp = split .fragment .split ("/" )
548550 while True :
549- sp .append (link )
551+ sp .append (str ( link ) )
550552 url = urlparse .urlunsplit (
551553 (split .scheme , split .netloc , split .path , split .query , "/" .join (sp )))
552554 if url in self .idx :
553- print link , "is" , url
554555 return url
555556 sp .pop ()
556557 if len (sp ) == 0 :
@@ -578,15 +579,17 @@ def validate_link(self, field, link, docid):
578579 "list, or a dict." )
579580 return link
580581
581- def getid (self , d ): # type: (Any) -> Union[basestring, None ]
582+ def getid (self , d ): # type: (Any) -> Union[str, unicode ]
582583 if isinstance (d , dict ):
583584 for i in self .identifiers :
584585 if i in d :
585- if isinstance (d [i ], basestring ):
586+ if isinstance (d [i ], ( str , unicode ) ):
586587 return d [i ]
587588 return None
588589
589- def validate_links (self , document , base_url ): # type: (Any) -> None
590+ DocumentType = TypeVar ('DocumentType' )
591+
592+ def validate_links (self , document , base_url ): # type: (DocumentType, Union[str, unicode]) -> DocumentType
590593 docid = self .getid (document )
591594 if not docid :
592595 docid = base_url
@@ -611,7 +614,7 @@ def validate_links(self, document, base_url): # type: (Any) -> None
611614
612615 for key , val in iterator :
613616 try :
614- document [key ] = self .validate_links (val , docid )
617+ document [key ] = self .validate_links (val , docid ) # type: ignore
615618 except validate .ValidationException as v :
616619 if key not in self .nolinkcheck :
617620 docid2 = self .getid (val )
0 commit comments