sorteddict.pyi 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # stub for SortedDict. This is a lightly edited copy of
  2. # https://github.com/grantjenks/python-sortedcontainers/blob/eea42df1f7bad2792e8da77335ff888f04b9e5ae/sortedcontainers/sorteddict.pyi
  3. # (from https://github.com/grantjenks/python-sortedcontainers/pull/107)
  4. from typing import (
  5. Any,
  6. Callable,
  7. Dict,
  8. Hashable,
  9. ItemsView,
  10. Iterable,
  11. Iterator,
  12. KeysView,
  13. List,
  14. Mapping,
  15. Optional,
  16. Sequence,
  17. Tuple,
  18. Type,
  19. TypeVar,
  20. Union,
  21. ValuesView,
  22. overload,
  23. )
  24. _T = TypeVar("_T")
  25. _S = TypeVar("_S")
  26. _T_h = TypeVar("_T_h", bound=Hashable)
  27. _KT = TypeVar("_KT", bound=Hashable) # Key type.
  28. _VT = TypeVar("_VT") # Value type.
  29. _KT_co = TypeVar("_KT_co", covariant=True, bound=Hashable)
  30. _VT_co = TypeVar("_VT_co", covariant=True)
  31. _SD = TypeVar("_SD", bound=SortedDict)
  32. _Key = Callable[[_T], Any]
  33. class SortedDict(Dict[_KT, _VT]):
  34. @overload
  35. def __init__(self, **kwargs: _VT) -> None: ...
  36. @overload
  37. def __init__(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
  38. @overload
  39. def __init__(
  40. self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT
  41. ) -> None: ...
  42. @overload
  43. def __init__(self, __key: _Key[_KT], **kwargs: _VT) -> None: ...
  44. @overload
  45. def __init__(
  46. self, __key: _Key[_KT], __map: Mapping[_KT, _VT], **kwargs: _VT
  47. ) -> None: ...
  48. @overload
  49. def __init__(
  50. self, __key: _Key[_KT], __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT
  51. ) -> None: ...
  52. @property
  53. def key(self) -> Optional[_Key[_KT]]: ...
  54. @property
  55. def iloc(self) -> SortedKeysView[_KT]: ...
  56. def clear(self) -> None: ...
  57. def __delitem__(self, key: _KT) -> None: ...
  58. def __iter__(self) -> Iterator[_KT]: ...
  59. def __reversed__(self) -> Iterator[_KT]: ...
  60. def __setitem__(self, key: _KT, value: _VT) -> None: ...
  61. def _setitem(self, key: _KT, value: _VT) -> None: ...
  62. def copy(self: _SD) -> _SD: ...
  63. def __copy__(self: _SD) -> _SD: ...
  64. @classmethod
  65. @overload
  66. def fromkeys(
  67. cls, seq: Iterable[_T_h], value: None = ...
  68. ) -> SortedDict[_T_h, None]: ...
  69. @classmethod
  70. @overload
  71. def fromkeys(cls, seq: Iterable[_T_h], value: _S) -> SortedDict[_T_h, _S]: ...
  72. # As of Python 3.10, `dict_{keys,items,values}` have an extra `mapping` attribute and so
  73. # `Sorted{Keys,Items,Values}View` are no longer compatible with them.
  74. # See https://github.com/python/typeshed/issues/6837
  75. def keys(self) -> SortedKeysView[_KT]: ... # type: ignore[override]
  76. def items(self) -> SortedItemsView[_KT, _VT]: ... # type: ignore[override]
  77. def values(self) -> SortedValuesView[_VT]: ... # type: ignore[override]
  78. @overload
  79. def pop(self, key: _KT) -> _VT: ...
  80. @overload
  81. def pop(self, key: _KT, default: _T = ...) -> Union[_VT, _T]: ...
  82. def popitem(self, index: int = ...) -> Tuple[_KT, _VT]: ...
  83. def peekitem(self, index: int = ...) -> Tuple[_KT, _VT]: ...
  84. def setdefault(self, key: _KT, default: Optional[_VT] = ...) -> _VT: ...
  85. # Mypy now reports the first overload as an error, because typeshed widened the type
  86. # of `__map` to its internal `_typeshed.SupportsKeysAndGetItem` type in
  87. # https://github.com/python/typeshed/pull/6653
  88. # Since sorteddicts don't change the signature of `update` from that of `dict`, we
  89. # let the stubs for `update` inherit from the stubs for `dict`. (I suspect we could
  90. # do the same for many othe methods.) We leave the stubs commented to better track
  91. # how this file has evolved from the original stubs.
  92. # @overload
  93. # def update(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
  94. # @overload
  95. # def update(self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
  96. # @overload
  97. # def update(self, **kwargs: _VT) -> None: ...
  98. def __reduce__(
  99. self,
  100. ) -> Tuple[
  101. Type[SortedDict[_KT, _VT]],
  102. Tuple[Callable[[_KT], Any], List[Tuple[_KT, _VT]]],
  103. ]: ...
  104. def __repr__(self) -> str: ...
  105. def _check(self) -> None: ...
  106. def islice(
  107. self,
  108. start: Optional[int] = ...,
  109. stop: Optional[int] = ...,
  110. reverse: bool = ...,
  111. ) -> Iterator[_KT]: ...
  112. def bisect_left(self, value: _KT) -> int: ...
  113. def bisect_right(self, value: _KT) -> int: ...
  114. class SortedKeysView(KeysView[_KT_co], Sequence[_KT_co]):
  115. @overload
  116. def __getitem__(self, index: int) -> _KT_co: ...
  117. @overload
  118. def __getitem__(self, index: slice) -> List[_KT_co]: ...
  119. def __delitem__(self, index: Union[int, slice]) -> None: ...
  120. class SortedItemsView(ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]]):
  121. def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ...
  122. @overload
  123. def __getitem__(self, index: int) -> Tuple[_KT_co, _VT_co]: ...
  124. @overload
  125. def __getitem__(self, index: slice) -> List[Tuple[_KT_co, _VT_co]]: ...
  126. def __delitem__(self, index: Union[int, slice]) -> None: ...
  127. class SortedValuesView(ValuesView[_VT_co], Sequence[_VT_co]):
  128. @overload
  129. def __getitem__(self, index: int) -> _VT_co: ...
  130. @overload
  131. def __getitem__(self, index: slice) -> List[_VT_co]: ...
  132. def __delitem__(self, index: Union[int, slice]) -> None: ...