__init__.pyi 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # flake8: noqa: E704
  2. # from https://gist.github.com/WuTheFWasThat/091a17d4b5cab597dfd5d4c2d96faf09
  3. # Stubs for pyrsistent (Python 3.6)
  4. from typing import Any
  5. from typing import AnyStr
  6. from typing import Callable
  7. from typing import Iterable
  8. from typing import Iterator
  9. from typing import List
  10. from typing import Optional
  11. from typing import Mapping
  12. from typing import MutableMapping
  13. from typing import Sequence
  14. from typing import Set
  15. from typing import Union
  16. from typing import Tuple
  17. from typing import Type
  18. from typing import TypeVar
  19. from typing import overload
  20. # see commit 08519aa for explanation of the re-export
  21. from pyrsistent.typing import CheckedKeyTypeError as CheckedKeyTypeError
  22. from pyrsistent.typing import CheckedPMap as CheckedPMap
  23. from pyrsistent.typing import CheckedPSet as CheckedPSet
  24. from pyrsistent.typing import CheckedPVector as CheckedPVector
  25. from pyrsistent.typing import CheckedType as CheckedType
  26. from pyrsistent.typing import CheckedValueTypeError as CheckedValueTypeError
  27. from pyrsistent.typing import InvariantException as InvariantException
  28. from pyrsistent.typing import PClass as PClass
  29. from pyrsistent.typing import PBag as PBag
  30. from pyrsistent.typing import PDeque as PDeque
  31. from pyrsistent.typing import PList as PList
  32. from pyrsistent.typing import PMap as PMap
  33. from pyrsistent.typing import PMapEvolver as PMapEvolver
  34. from pyrsistent.typing import PSet as PSet
  35. from pyrsistent.typing import PSetEvolver as PSetEvolver
  36. from pyrsistent.typing import PTypeError as PTypeError
  37. from pyrsistent.typing import PVector as PVector
  38. from pyrsistent.typing import PVectorEvolver as PVectorEvolver
  39. T = TypeVar('T')
  40. KT = TypeVar('KT')
  41. VT = TypeVar('VT')
  42. def pmap(initial: Union[Mapping[KT, VT], Iterable[Tuple[KT, VT]]] = {}, pre_size: int = 0) -> PMap[KT, VT]: ...
  43. def m(**kwargs: VT) -> PMap[str, VT]: ...
  44. def pvector(iterable: Iterable[T] = ...) -> PVector[T]: ...
  45. def v(*iterable: T) -> PVector[T]: ...
  46. def pset(iterable: Iterable[T] = (), pre_size: int = 8) -> PSet[T]: ...
  47. def s(*iterable: T) -> PSet[T]: ...
  48. # see class_test.py for use cases
  49. Invariant = Tuple[bool, Optional[Union[str, Callable[[], str]]]]
  50. @overload
  51. def field(
  52. type: Union[Type[T], Sequence[Type[T]]] = ...,
  53. invariant: Callable[[Any], Union[Invariant, Iterable[Invariant]]] = lambda _: (True, None),
  54. initial: Any = object(),
  55. mandatory: bool = False,
  56. factory: Callable[[Any], T] = lambda x: x,
  57. serializer: Callable[[Any, T], Any] = lambda _, value: value,
  58. ) -> T: ...
  59. # The actual return value (_PField) is irrelevant after a PRecord has been instantiated,
  60. # see https://github.com/tobgu/pyrsistent/blob/master/pyrsistent/_precord.py#L10
  61. @overload
  62. def field(
  63. type: Any = ...,
  64. invariant: Callable[[Any], Union[Invariant, Iterable[Invariant]]] = lambda _: (True, None),
  65. initial: Any = object(),
  66. mandatory: bool = False,
  67. factory: Callable[[Any], Any] = lambda x: x,
  68. serializer: Callable[[Any, Any], Any] = lambda _, value: value,
  69. ) -> Any: ...
  70. # Use precise types for the simplest use cases, but fall back to Any for
  71. # everything else. See record_test.py for the wide range of possible types for
  72. # item_type
  73. @overload
  74. def pset_field(
  75. item_type: Type[T],
  76. optional: bool = False,
  77. initial: Iterable[T] = ...,
  78. ) -> PSet[T]: ...
  79. @overload
  80. def pset_field(
  81. item_type: Any,
  82. optional: bool = False,
  83. initial: Any = (),
  84. ) -> PSet[Any]: ...
  85. @overload
  86. def pmap_field(
  87. key_type: Type[KT],
  88. value_type: Type[VT],
  89. optional: bool = False,
  90. invariant: Callable[[Any], Tuple[bool, Optional[str]]] = lambda _: (True, None),
  91. ) -> PMap[KT, VT]: ...
  92. @overload
  93. def pmap_field(
  94. key_type: Any,
  95. value_type: Any,
  96. optional: bool = False,
  97. invariant: Callable[[Any], Tuple[bool, Optional[str]]] = lambda _: (True, None),
  98. ) -> PMap[Any, Any]: ...
  99. @overload
  100. def pvector_field(
  101. item_type: Type[T],
  102. optional: bool = False,
  103. initial: Iterable[T] = ...,
  104. ) -> PVector[T]: ...
  105. @overload
  106. def pvector_field(
  107. item_type: Any,
  108. optional: bool = False,
  109. initial: Any = (),
  110. ) -> PVector[Any]: ...
  111. def pbag(elements: Iterable[T]) -> PBag[T]: ...
  112. def b(*elements: T) -> PBag[T]: ...
  113. def plist(iterable: Iterable[T] = (), reverse: bool = False) -> PList[T]: ...
  114. def l(*elements: T) -> PList[T]: ...
  115. def pdeque(iterable: Optional[Iterable[T]] = None, maxlen: Optional[int] = None) -> PDeque[T]: ...
  116. def dq(*iterable: T) -> PDeque[T]: ...
  117. @overload
  118. def optional(type: T) -> Tuple[T, Type[None]]: ...
  119. @overload
  120. def optional(*typs: Any) -> Tuple[Any, ...]: ...
  121. T_PRecord = TypeVar('T_PRecord', bound='PRecord')
  122. class PRecord(PMap[AnyStr, Any]):
  123. _precord_fields: Mapping
  124. _precord_initial_values: Mapping
  125. def __hash__(self) -> int: ...
  126. def __init__(self, **kwargs: Any) -> None: ...
  127. def __iter__(self) -> Iterator[Any]: ...
  128. def __len__(self) -> int: ...
  129. @classmethod
  130. def create(
  131. cls: Type[T_PRecord],
  132. kwargs: Mapping,
  133. _factory_fields: Optional[Iterable] = None,
  134. ignore_extra: bool = False,
  135. ) -> T_PRecord: ...
  136. # This is OK because T_PRecord is a concrete type
  137. def discard(self: T_PRecord, key: KT) -> T_PRecord: ...
  138. def remove(self: T_PRecord, key: KT) -> T_PRecord: ...
  139. def serialize(self, format: Optional[Any] = ...) -> MutableMapping: ...
  140. # From pyrsistent documentation:
  141. # This set function differs slightly from that in the PMap
  142. # class. First of all it accepts key-value pairs. Second it accepts multiple key-value
  143. # pairs to perform one, atomic, update of multiple fields.
  144. @overload
  145. def set(self, key: KT, val: VT) -> Any: ...
  146. @overload
  147. def set(self, **kwargs: VT) -> Any: ...
  148. def immutable(
  149. members: Union[str, Iterable[str]] = '',
  150. name: str = 'Immutable',
  151. verbose: bool = False,
  152. ) -> Tuple: ... # actually a namedtuple
  153. # ignore mypy warning "Overloaded function signatures 1 and 5 overlap with
  154. # incompatible return types"
  155. @overload
  156. def freeze(o: Mapping[KT, VT]) -> PMap[KT, VT]: ... # type: ignore
  157. @overload
  158. def freeze(o: List[T]) -> PVector[T]: ... # type: ignore
  159. @overload
  160. def freeze(o: Tuple[T, ...]) -> Tuple[T, ...]: ...
  161. @overload
  162. def freeze(o: Set[T]) -> PSet[T]: ... # type: ignore
  163. @overload
  164. def freeze(o: T) -> T: ...
  165. @overload
  166. def thaw(o: PMap[KT, VT]) -> MutableMapping[KT, VT]: ... # type: ignore
  167. @overload
  168. def thaw(o: PVector[T]) -> List[T]: ... # type: ignore
  169. @overload
  170. def thaw(o: Tuple[T, ...]) -> Tuple[T, ...]: ...
  171. # collections.abc.MutableSet is kind of garbage:
  172. # https://stackoverflow.com/questions/24977898/why-does-collections-mutableset-not-bestow-an-update-method
  173. @overload
  174. def thaw(o: PSet[T]) -> Set[T]: ... # type: ignore
  175. @overload
  176. def thaw(o: T) -> T: ...
  177. def mutant(fn: Callable) -> Callable: ...
  178. def inc(x: int) -> int: ...
  179. @overload
  180. def discard(evolver: PMapEvolver[KT, VT], key: KT) -> None: ...
  181. @overload
  182. def discard(evolver: PVectorEvolver[T], key: int) -> None: ...
  183. @overload
  184. def discard(evolver: PSetEvolver[T], key: T) -> None: ...
  185. def rex(expr: str) -> Callable[[Any], bool]: ...
  186. def ny(_: Any) -> bool: ...
  187. def get_in(keys: Iterable, coll: Mapping, default: Optional[Any] = None, no_default: bool = False) -> Any: ...