1
1
# pyright: reportIncompatibleMethodOverride=false
2
2
import abc
3
- import typing
4
3
import collections
4
+ import typing
5
5
6
6
from . import types
7
7
@@ -238,7 +238,7 @@ def __init__(
238
238
def insert (self , index : types .SupportsIndex , value : HT ) -> None :
239
239
if value in self ._set :
240
240
if self .on_duplicate == 'raise' :
241
- raise ValueError ('Duplicate value: %s' % value )
241
+ raise ValueError (f 'Duplicate value: { value } ' )
242
242
else :
243
243
return
244
244
@@ -248,7 +248,7 @@ def insert(self, index: types.SupportsIndex, value: HT) -> None:
248
248
def append (self , value : HT ) -> None :
249
249
if value in self ._set :
250
250
if self .on_duplicate == 'raise' :
251
- raise ValueError ('Duplicate value: %s' % value )
251
+ raise ValueError (f 'Duplicate value: { value } ' )
252
252
else :
253
253
return
254
254
@@ -258,11 +258,11 @@ def append(self, value: HT) -> None:
258
258
def __contains__ (self , item : HT ) -> bool : # type: ignore
259
259
return item in self ._set
260
260
261
- @types .overload
261
+ @typing .overload
262
262
def __setitem__ (self , indices : types .SupportsIndex , values : HT ) -> None :
263
263
...
264
264
265
- @types .overload
265
+ @typing .overload
266
266
def __setitem__ (self , indices : slice , values : types .Iterable [HT ]) -> None :
267
267
...
268
268
@@ -310,12 +310,14 @@ def __delitem__(
310
310
super ().__delitem__ (index )
311
311
312
312
313
+ # Type hinting `collections.deque` does not work consistently between Python
314
+ # runtime, mypy and pyright currently so we have to ignore the errors
313
315
class SlicableDeque (types .Generic [T ], collections .deque ): # type: ignore
314
- @types .overload
316
+ @typing .overload
315
317
def __getitem__ (self , index : types .SupportsIndex ) -> T :
316
318
...
317
319
318
- @types .overload
320
+ @typing .overload
319
321
def __getitem__ (self , index : slice ) -> 'SlicableDeque[T]' :
320
322
...
321
323
@@ -340,6 +342,30 @@ def __getitem__(
340
342
else :
341
343
return types .cast (T , super ().__getitem__ (index ))
342
344
345
+ def __eq__ (self , other : types .Any ) -> bool :
346
+ # Allow for comparison with a list or tuple
347
+ if isinstance (other , list ):
348
+ return list (self ) == other
349
+ elif isinstance (other , tuple ):
350
+ return tuple (self ) == other
351
+ elif isinstance (other , set ):
352
+ return set (self ) == other
353
+ else :
354
+ return super ().__eq__ (other )
355
+
356
+ def pop (self , index : int = - 1 ) -> T :
357
+ # We need to allow for an index but a deque only allows the removal of
358
+ # the first or last item.
359
+ if index == 0 :
360
+ return typing .cast (T , super ().popleft ())
361
+ elif index in {- 1 , len (self ) - 1 }:
362
+ return typing .cast (T , super ().pop ())
363
+ else :
364
+ raise IndexError (
365
+ 'Only index 0 and the last index (`N-1` or `-1`) '
366
+ 'are supported'
367
+ )
368
+
343
369
344
370
if __name__ == '__main__' :
345
371
import doctest
0 commit comments