sorteddict.pyi 5.2 KB

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