1+ from __future__ import annotations
2+
3+ from typing import TYPE_CHECKING , Iterable , Iterator , List , Optional
4+
15from rdflib .namespace import RDF
2- from rdflib .term import BNode , Literal
6+ from rdflib .term import BNode , Node
7+
8+ if TYPE_CHECKING :
9+ from rdflib import Graph
310
411__all__ = ["Collection" ]
512
@@ -9,6 +16,7 @@ class Collection(object):
916 See "Emulating container types":
1017 https://docs.python.org/reference/datamodel.html#emulating-container-types
1118
19+ >>> from rdflib.term import Literal
1220 >>> from rdflib.graph import Graph
1321 >>> from pprint import pprint
1422 >>> listname = BNode()
@@ -43,13 +51,14 @@ class Collection(object):
4351 True
4452 """
4553
46- def __init__ (self , graph , uri , seq = []):
54+ def __init__ (self , graph : Graph , uri : Node , seq : List [ Node ] = []):
4755 self .graph = graph
4856 self .uri = uri or BNode ()
4957 self += seq
5058
51- def n3 (self ):
59+ def n3 (self ) -> str :
5260 """
61+ >>> from rdflib.term import Literal
5362 >>> from rdflib.graph import Graph
5463 >>> listname = BNode()
5564 >>> g = Graph('Memory')
@@ -73,13 +82,14 @@ def n3(self):
7382 "2"^^<http://www.w3.org/2001/XMLSchema#integer>
7483 "3"^^<http://www.w3.org/2001/XMLSchema#integer> )
7584 """
76- return "( %s )" % (" " .join ([i .n3 () for i in self ]))
85+ # type error: "Node" has no attribute "n3"
86+ return "( %s )" % (" " .join ([i .n3 () for i in self ])) # type: ignore[attr-defined]
7787
78- def _get_container (self , index ) :
88+ def _get_container (self , index : int ) -> Optional [ Node ] :
7989 """Gets the first, rest holding node at index."""
8090 assert isinstance (index , int )
8191 graph = self .graph
82- container = self .uri
92+ container : Optional [ Node ] = self .uri
8393 i = 0
8494 while i < index :
8595 i += 1
@@ -88,11 +98,11 @@ def _get_container(self, index):
8898 break
8999 return container
90100
91- def __len__ (self ):
101+ def __len__ (self ) -> int :
92102 """length of items in collection."""
93103 return len (list (self .graph .items (self .uri )))
94104
95- def index (self , item ) :
105+ def index (self , item : Node ) -> int :
96106 """
97107 Returns the 0-based numerical index of the item in the list
98108 """
@@ -112,7 +122,7 @@ def index(self, item):
112122 assert len (newlink ) == 1 , "Malformed RDF Collection: %s" % self .uri
113123 listname = newlink [0 ]
114124
115- def __getitem__ (self , key ) :
125+ def __getitem__ (self , key : int ) -> Node :
116126 """TODO"""
117127 c = self ._get_container (key )
118128 if c :
@@ -124,15 +134,15 @@ def __getitem__(self, key):
124134 else :
125135 raise IndexError (key )
126136
127- def __setitem__ (self , key , value ) :
137+ def __setitem__ (self , key : int , value : Node ) -> None :
128138 """TODO"""
129139 c = self ._get_container (key )
130140 if c :
131141 self .graph .set ((c , RDF .first , value ))
132142 else :
133143 raise IndexError (key )
134144
135- def __delitem__ (self , key ) :
145+ def __delitem__ (self , key : int ) -> None :
136146 """
137147 >>> from rdflib.namespace import RDF, RDFS
138148 >>> from rdflib import Graph
@@ -184,7 +194,8 @@ def __delitem__(self, key):
184194 elif key == len (self ) - 1 :
185195 # the tail
186196 priorlink = self ._get_container (key - 1 )
187- self .graph .set ((priorlink , RDF .rest , RDF .nil ))
197+ # type error: Argument 1 to "set" of "Graph" has incompatible type "Tuple[Optional[Node], URIRef, URIRef]"; expected "Tuple[Node, Node, Any]"
198+ self .graph .set ((priorlink , RDF .rest , RDF .nil )) # type: ignore[arg-type]
188199 graph .remove ((current , None , None ))
189200 else :
190201 next = self ._get_container (key + 1 )
@@ -193,11 +204,11 @@ def __delitem__(self, key):
193204 graph .remove ((current , None , None ))
194205 graph .set ((prior , RDF .rest , next ))
195206
196- def __iter__ (self ):
207+ def __iter__ (self ) -> Iterator [ Node ] :
197208 """Iterator over items in Collections"""
198209 return self .graph .items (self .uri )
199210
200- def _end (self ):
211+ def _end (self ) -> Node :
201212 # find end of list
202213 container = self .uri
203214 while True :
@@ -207,8 +218,9 @@ def _end(self):
207218 else :
208219 container = rest
209220
210- def append (self , item ) :
221+ def append (self , item : Node ) -> Collection :
211222 """
223+ >>> from rdflib.term import Literal
212224 >>> from rdflib.graph import Graph
213225 >>> listname = BNode()
214226 >>> g = Graph()
@@ -231,7 +243,7 @@ def append(self, item):
231243 self .graph .add ((end , RDF .rest , RDF .nil ))
232244 return self
233245
234- def __iadd__ (self , other ) :
246+ def __iadd__ (self , other : Iterable [ Node ]) -> Collection :
235247
236248 end = self ._end ()
237249 self .graph .remove ((end , RDF .rest , None ))
@@ -247,8 +259,8 @@ def __iadd__(self, other):
247259 self .graph .add ((end , RDF .rest , RDF .nil ))
248260 return self
249261
250- def clear (self ):
251- container = self .uri
262+ def clear (self ) -> Collection :
263+ container : Optional [ Node ] = self .uri
252264 graph = self .graph
253265 while container :
254266 rest = graph .value (container , RDF .rest )
0 commit comments