11# pyright: reportIncompatibleMethodOverride=false
22import abc
33import typing
4+ import collections
45
56from . import types
67
1920VT_cast = types .Optional [types .Callable [..., VT ]]
2021#: A type alias for the hashable values of the `UniqueList`
2122HT = types .TypeVar ('HT' , bound = types .Hashable )
23+ #: A type alias for a regular generic type
24+ T = types .TypeVar ('T' )
2225
2326# Using types.Union instead of | since Python 3.7 doesn't fully support it
2427DictUpdateArgs = types .Union [
2831 '_typeshed.SupportsKeysAndGetItem[KT, VT]' ,
2932]
3033
34+ OnDuplicate = types .Literal ['ignore' , 'raise' ]
35+
3136
3237class CastedDictBase (types .Dict [KT , VT ], abc .ABC ):
3338 _key_cast : KT_cast [KT ]
@@ -222,7 +227,7 @@ class UniqueList(types.List[HT]):
222227 def __init__ (
223228 self ,
224229 * args : HT ,
225- on_duplicate : types . Literal [ 'raise' , 'ignore' ] = 'ignore' ,
230+ on_duplicate : OnDuplicate = 'ignore' ,
226231 ):
227232 self .on_duplicate = on_duplicate
228233 self ._set = set ()
@@ -305,9 +310,19 @@ def __delitem__(
305310 super ().__delitem__ (index )
306311
307312
308- class SlicableDeque (Generic [T ], deque ):
309- def __getitem__ (self , index : Union [int , slice ]) -> Union [T , 'SlicableDeque[T]' ]:
310- """
313+ class SlicableDeque (types .Generic [T ], collections .deque ): # type: ignore
314+ @types .overload
315+ def __getitem__ (self , index : types .SupportsIndex ) -> T :
316+ ...
317+
318+ @types .overload
319+ def __getitem__ (self , index : slice ) -> 'SlicableDeque[T]' :
320+ ...
321+
322+ def __getitem__ (
323+ self , index : types .Union [types .SupportsIndex , slice ]
324+ ) -> types .Union [T , 'SlicableDeque[T]' ]:
325+ '''
311326 Return the item or slice at the given index.
312327
313328 >>> d = SlicableDeque[int]([1, 2, 3, 4, 5])
@@ -318,23 +333,12 @@ def __getitem__(self, index: Union[int, slice]) -> Union[T, 'SlicableDeque[T]']:
318333 >>> d[-2:]
319334 SlicableDeque(['b', 'c'])
320335
321- """
336+ '''
322337 if isinstance (index , slice ):
323338 start , stop , step = index .indices (len (self ))
324339 return self .__class__ (self [i ] for i in range (start , stop , step ))
325340 else :
326- return super ().__getitem__ (index )
327-
328- def pop (self ) -> T :
329- """
330- Remove and return the rightmost element.
331-
332- >>> d = SlicableDeque[float]([1.5, 2.5, 3.5])
333- >>> d.pop()
334- 3.5
335-
336- """
337- return super ().pop ()
341+ return types .cast (T , super ().__getitem__ (index ))
338342
339343
340344if __name__ == '__main__' :
0 commit comments