1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING , Iterable , Iterator , List , Optional
4
+
1
5
from 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
3
10
4
11
__all__ = ["Collection" ]
5
12
@@ -9,6 +16,7 @@ class Collection(object):
9
16
See "Emulating container types":
10
17
https://docs.python.org/reference/datamodel.html#emulating-container-types
11
18
19
+ >>> from rdflib.term import Literal
12
20
>>> from rdflib.graph import Graph
13
21
>>> from pprint import pprint
14
22
>>> listname = BNode()
@@ -43,13 +51,14 @@ class Collection(object):
43
51
True
44
52
"""
45
53
46
- def __init__ (self , graph , uri , seq = []):
54
+ def __init__ (self , graph : Graph , uri : Node , seq : List [ Node ] = []):
47
55
self .graph = graph
48
56
self .uri = uri or BNode ()
49
57
self += seq
50
58
51
- def n3 (self ):
59
+ def n3 (self ) -> str :
52
60
"""
61
+ >>> from rdflib.term import Literal
53
62
>>> from rdflib.graph import Graph
54
63
>>> listname = BNode()
55
64
>>> g = Graph('Memory')
@@ -73,13 +82,14 @@ def n3(self):
73
82
"2"^^<http://www.w3.org/2001/XMLSchema#integer>
74
83
"3"^^<http://www.w3.org/2001/XMLSchema#integer> )
75
84
"""
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]
77
87
78
- def _get_container (self , index ) :
88
+ def _get_container (self , index : int ) -> Optional [ Node ] :
79
89
"""Gets the first, rest holding node at index."""
80
90
assert isinstance (index , int )
81
91
graph = self .graph
82
- container = self .uri
92
+ container : Optional [ Node ] = self .uri
83
93
i = 0
84
94
while i < index :
85
95
i += 1
@@ -88,11 +98,11 @@ def _get_container(self, index):
88
98
break
89
99
return container
90
100
91
- def __len__ (self ):
101
+ def __len__ (self ) -> int :
92
102
"""length of items in collection."""
93
103
return len (list (self .graph .items (self .uri )))
94
104
95
- def index (self , item ) :
105
+ def index (self , item : Node ) -> int :
96
106
"""
97
107
Returns the 0-based numerical index of the item in the list
98
108
"""
@@ -112,7 +122,7 @@ def index(self, item):
112
122
assert len (newlink ) == 1 , "Malformed RDF Collection: %s" % self .uri
113
123
listname = newlink [0 ]
114
124
115
- def __getitem__ (self , key ) :
125
+ def __getitem__ (self , key : int ) -> Node :
116
126
"""TODO"""
117
127
c = self ._get_container (key )
118
128
if c :
@@ -124,15 +134,15 @@ def __getitem__(self, key):
124
134
else :
125
135
raise IndexError (key )
126
136
127
- def __setitem__ (self , key , value ) :
137
+ def __setitem__ (self , key : int , value : Node ) -> None :
128
138
"""TODO"""
129
139
c = self ._get_container (key )
130
140
if c :
131
141
self .graph .set ((c , RDF .first , value ))
132
142
else :
133
143
raise IndexError (key )
134
144
135
- def __delitem__ (self , key ) :
145
+ def __delitem__ (self , key : int ) -> None :
136
146
"""
137
147
>>> from rdflib.namespace import RDF, RDFS
138
148
>>> from rdflib import Graph
@@ -184,7 +194,8 @@ def __delitem__(self, key):
184
194
elif key == len (self ) - 1 :
185
195
# the tail
186
196
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]
188
199
graph .remove ((current , None , None ))
189
200
else :
190
201
next = self ._get_container (key + 1 )
@@ -193,11 +204,11 @@ def __delitem__(self, key):
193
204
graph .remove ((current , None , None ))
194
205
graph .set ((prior , RDF .rest , next ))
195
206
196
- def __iter__ (self ):
207
+ def __iter__ (self ) -> Iterator [ Node ] :
197
208
"""Iterator over items in Collections"""
198
209
return self .graph .items (self .uri )
199
210
200
- def _end (self ):
211
+ def _end (self ) -> Node :
201
212
# find end of list
202
213
container = self .uri
203
214
while True :
@@ -207,8 +218,9 @@ def _end(self):
207
218
else :
208
219
container = rest
209
220
210
- def append (self , item ) :
221
+ def append (self , item : Node ) -> Collection :
211
222
"""
223
+ >>> from rdflib.term import Literal
212
224
>>> from rdflib.graph import Graph
213
225
>>> listname = BNode()
214
226
>>> g = Graph()
@@ -231,7 +243,7 @@ def append(self, item):
231
243
self .graph .add ((end , RDF .rest , RDF .nil ))
232
244
return self
233
245
234
- def __iadd__ (self , other ) :
246
+ def __iadd__ (self , other : Iterable [ Node ]) -> Collection :
235
247
236
248
end = self ._end ()
237
249
self .graph .remove ((end , RDF .rest , None ))
@@ -247,8 +259,8 @@ def __iadd__(self, other):
247
259
self .graph .add ((end , RDF .rest , RDF .nil ))
248
260
return self
249
261
250
- def clear (self ):
251
- container = self .uri
262
+ def clear (self ) -> Collection :
263
+ container : Optional [ Node ] = self .uri
252
264
graph = self .graph
253
265
while container :
254
266
rest = graph .value (container , RDF .rest )
0 commit comments