19
19
from rdflib .namespace import RDF , RDFS , OWL
20
20
from rdflib .plugins .parsers .notation3 import BadSyntax
21
21
import 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
23
23
24
24
_logger = logging .getLogger ("salad" )
25
25
@@ -240,7 +240,7 @@ def add_context(self, newcontext, baseuri=""):
240
240
_logger .debug ("vocab is %s" , self .vocab )
241
241
242
242
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]]
244
244
base_url = base_url or 'file://%s/' % os .path .abspath ('.' )
245
245
246
246
obj = None # type: Dict[str, Any]
@@ -323,7 +323,7 @@ def resolve_ref(self, ref, base_url=None, toplevel=True):
323
323
return obj , metadata
324
324
325
325
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]]
327
327
loader = self
328
328
metadata = {} # type: Dict[str, Any]
329
329
if file_base is None :
@@ -531,8 +531,10 @@ def check_file(self, fn): # type: (Union[str, unicode]) -> bool
531
531
else :
532
532
return False
533
533
534
+ FieldType = TypeVar ('FieldType' , unicode , List [str ], Dict [str , Any ])
535
+
534
536
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
536
538
if field in self .nolinkcheck :
537
539
return link
538
540
if isinstance (link , (str , unicode )):
@@ -546,11 +548,10 @@ def validate_link(self, field, link, docid):
546
548
split = urlparse .urlsplit (docid )
547
549
sp = split .fragment .split ("/" )
548
550
while True :
549
- sp .append (link )
551
+ sp .append (str ( link ) )
550
552
url = urlparse .urlunsplit (
551
553
(split .scheme , split .netloc , split .path , split .query , "/" .join (sp )))
552
554
if url in self .idx :
553
- print link , "is" , url
554
555
return url
555
556
sp .pop ()
556
557
if len (sp ) == 0 :
@@ -578,15 +579,17 @@ def validate_link(self, field, link, docid):
578
579
"list, or a dict." )
579
580
return link
580
581
581
- def getid (self , d ): # type: (Any) -> Union[basestring, None ]
582
+ def getid (self , d ): # type: (Any) -> Union[str, unicode ]
582
583
if isinstance (d , dict ):
583
584
for i in self .identifiers :
584
585
if i in d :
585
- if isinstance (d [i ], basestring ):
586
+ if isinstance (d [i ], ( str , unicode ) ):
586
587
return d [i ]
587
588
return None
588
589
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
590
593
docid = self .getid (document )
591
594
if not docid :
592
595
docid = base_url
@@ -611,7 +614,7 @@ def validate_links(self, document, base_url): # type: (Any) -> None
611
614
612
615
for key , val in iterator :
613
616
try :
614
- document [key ] = self .validate_links (val , docid )
617
+ document [key ] = self .validate_links (val , docid ) # type: ignore
615
618
except validate .ValidationException as v :
616
619
if key not in self .nolinkcheck :
617
620
docid2 = self .getid (val )
0 commit comments