11# pyright: reportIncompatibleMethodOverride=false
22import abc
3- import typing
43import collections
4+ import typing
55
66from . import types
77
@@ -238,7 +238,7 @@ def __init__(
238238 def insert (self , index : types .SupportsIndex , value : HT ) -> None :
239239 if value in self ._set :
240240 if self .on_duplicate == 'raise' :
241- raise ValueError ('Duplicate value: %s' % value )
241+ raise ValueError (f 'Duplicate value: { value } ' )
242242 else :
243243 return
244244
@@ -248,7 +248,7 @@ def insert(self, index: types.SupportsIndex, value: HT) -> None:
248248 def append (self , value : HT ) -> None :
249249 if value in self ._set :
250250 if self .on_duplicate == 'raise' :
251- raise ValueError ('Duplicate value: %s' % value )
251+ raise ValueError (f 'Duplicate value: { value } ' )
252252 else :
253253 return
254254
@@ -258,11 +258,11 @@ def append(self, value: HT) -> None:
258258 def __contains__ (self , item : HT ) -> bool : # type: ignore
259259 return item in self ._set
260260
261- @types .overload
261+ @typing .overload
262262 def __setitem__ (self , indices : types .SupportsIndex , values : HT ) -> None :
263263 ...
264264
265- @types .overload
265+ @typing .overload
266266 def __setitem__ (self , indices : slice , values : types .Iterable [HT ]) -> None :
267267 ...
268268
@@ -310,12 +310,14 @@ def __delitem__(
310310 super ().__delitem__ (index )
311311
312312
313+ # Type hinting `collections.deque` does not work consistently between Python
314+ # runtime, mypy and pyright currently so we have to ignore the errors
313315class SlicableDeque (types .Generic [T ], collections .deque ): # type: ignore
314- @types .overload
316+ @typing .overload
315317 def __getitem__ (self , index : types .SupportsIndex ) -> T :
316318 ...
317319
318- @types .overload
320+ @typing .overload
319321 def __getitem__ (self , index : slice ) -> 'SlicableDeque[T]' :
320322 ...
321323
@@ -340,6 +342,30 @@ def __getitem__(
340342 else :
341343 return types .cast (T , super ().__getitem__ (index ))
342344
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+
343369
344370if __name__ == '__main__' :
345371 import doctest
0 commit comments