utils.pyi 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. import os
  2. import sys
  3. import ast
  4. import types
  5. import warnings
  6. import unittest
  7. import contextlib
  8. from re import Pattern
  9. from collections.abc import Callable, Iterable, Sequence
  10. from typing import (
  11. Literal as L,
  12. Any,
  13. AnyStr,
  14. ClassVar,
  15. NoReturn,
  16. overload,
  17. type_check_only,
  18. TypeVar,
  19. Union,
  20. Final,
  21. SupportsIndex,
  22. )
  23. if sys.version_info >= (3, 10):
  24. from typing import ParamSpec
  25. else:
  26. from typing_extensions import ParamSpec
  27. from numpy import generic, dtype, number, object_, bool_, _FloatValue
  28. from numpy._typing import (
  29. NDArray,
  30. ArrayLike,
  31. DTypeLike,
  32. _ArrayLikeNumber_co,
  33. _ArrayLikeObject_co,
  34. _ArrayLikeTD64_co,
  35. _ArrayLikeDT64_co,
  36. )
  37. from unittest.case import (
  38. SkipTest as SkipTest,
  39. )
  40. _P = ParamSpec("_P")
  41. _T = TypeVar("_T")
  42. _ET = TypeVar("_ET", bound=BaseException)
  43. _FT = TypeVar("_FT", bound=Callable[..., Any])
  44. # Must return a bool or an ndarray/generic type
  45. # that is supported by `np.logical_and.reduce`
  46. _ComparisonFunc = Callable[
  47. [NDArray[Any], NDArray[Any]],
  48. Union[
  49. bool,
  50. bool_,
  51. number[Any],
  52. NDArray[Union[bool_, number[Any], object_]],
  53. ],
  54. ]
  55. __all__: list[str]
  56. class KnownFailureException(Exception): ...
  57. class IgnoreException(Exception): ...
  58. class clear_and_catch_warnings(warnings.catch_warnings):
  59. class_modules: ClassVar[tuple[types.ModuleType, ...]]
  60. modules: set[types.ModuleType]
  61. @overload
  62. def __new__(
  63. cls,
  64. record: L[False] = ...,
  65. modules: Iterable[types.ModuleType] = ...,
  66. ) -> _clear_and_catch_warnings_without_records: ...
  67. @overload
  68. def __new__(
  69. cls,
  70. record: L[True],
  71. modules: Iterable[types.ModuleType] = ...,
  72. ) -> _clear_and_catch_warnings_with_records: ...
  73. @overload
  74. def __new__(
  75. cls,
  76. record: bool,
  77. modules: Iterable[types.ModuleType] = ...,
  78. ) -> clear_and_catch_warnings: ...
  79. def __enter__(self) -> None | list[warnings.WarningMessage]: ...
  80. def __exit__(
  81. self,
  82. __exc_type: None | type[BaseException] = ...,
  83. __exc_val: None | BaseException = ...,
  84. __exc_tb: None | types.TracebackType = ...,
  85. ) -> None: ...
  86. # Type-check only `clear_and_catch_warnings` subclasses for both values of the
  87. # `record` parameter. Copied from the stdlib `warnings` stubs.
  88. @type_check_only
  89. class _clear_and_catch_warnings_with_records(clear_and_catch_warnings):
  90. def __enter__(self) -> list[warnings.WarningMessage]: ...
  91. @type_check_only
  92. class _clear_and_catch_warnings_without_records(clear_and_catch_warnings):
  93. def __enter__(self) -> None: ...
  94. class suppress_warnings:
  95. log: list[warnings.WarningMessage]
  96. def __init__(
  97. self,
  98. forwarding_rule: L["always", "module", "once", "location"] = ...,
  99. ) -> None: ...
  100. def filter(
  101. self,
  102. category: type[Warning] = ...,
  103. message: str = ...,
  104. module: None | types.ModuleType = ...,
  105. ) -> None: ...
  106. def record(
  107. self,
  108. category: type[Warning] = ...,
  109. message: str = ...,
  110. module: None | types.ModuleType = ...,
  111. ) -> list[warnings.WarningMessage]: ...
  112. def __enter__(self: _T) -> _T: ...
  113. def __exit__(
  114. self,
  115. __exc_type: None | type[BaseException] = ...,
  116. __exc_val: None | BaseException = ...,
  117. __exc_tb: None | types.TracebackType = ...,
  118. ) -> None: ...
  119. def __call__(self, func: _FT) -> _FT: ...
  120. verbose: int
  121. IS_PYPY: Final[bool]
  122. IS_PYSTON: Final[bool]
  123. HAS_REFCOUNT: Final[bool]
  124. HAS_LAPACK64: Final[bool]
  125. def assert_(val: object, msg: str | Callable[[], str] = ...) -> None: ...
  126. # Contrary to runtime we can't do `os.name` checks while type checking,
  127. # only `sys.platform` checks
  128. if sys.platform == "win32" or sys.platform == "cygwin":
  129. def memusage(processName: str = ..., instance: int = ...) -> int: ...
  130. elif sys.platform == "linux":
  131. def memusage(_proc_pid_stat: str | bytes | os.PathLike[Any] = ...) -> None | int: ...
  132. else:
  133. def memusage() -> NoReturn: ...
  134. if sys.platform == "linux":
  135. def jiffies(
  136. _proc_pid_stat: str | bytes | os.PathLike[Any] = ...,
  137. _load_time: list[float] = ...,
  138. ) -> int: ...
  139. else:
  140. def jiffies(_load_time: list[float] = ...) -> int: ...
  141. def build_err_msg(
  142. arrays: Iterable[object],
  143. err_msg: str,
  144. header: str = ...,
  145. verbose: bool = ...,
  146. names: Sequence[str] = ...,
  147. precision: None | SupportsIndex = ...,
  148. ) -> str: ...
  149. def assert_equal(
  150. actual: object,
  151. desired: object,
  152. err_msg: str = ...,
  153. verbose: bool = ...,
  154. ) -> None: ...
  155. def print_assert_equal(
  156. test_string: str,
  157. actual: object,
  158. desired: object,
  159. ) -> None: ...
  160. def assert_almost_equal(
  161. actual: _ArrayLikeNumber_co | _ArrayLikeObject_co,
  162. desired: _ArrayLikeNumber_co | _ArrayLikeObject_co,
  163. decimal: int = ...,
  164. err_msg: str = ...,
  165. verbose: bool = ...,
  166. ) -> None: ...
  167. # Anything that can be coerced into `builtins.float`
  168. def assert_approx_equal(
  169. actual: _FloatValue,
  170. desired: _FloatValue,
  171. significant: int = ...,
  172. err_msg: str = ...,
  173. verbose: bool = ...,
  174. ) -> None: ...
  175. def assert_array_compare(
  176. comparison: _ComparisonFunc,
  177. x: ArrayLike,
  178. y: ArrayLike,
  179. err_msg: str = ...,
  180. verbose: bool = ...,
  181. header: str = ...,
  182. precision: SupportsIndex = ...,
  183. equal_nan: bool = ...,
  184. equal_inf: bool = ...,
  185. *,
  186. strict: bool = ...
  187. ) -> None: ...
  188. def assert_array_equal(
  189. x: ArrayLike,
  190. y: ArrayLike,
  191. err_msg: str = ...,
  192. verbose: bool = ...,
  193. *,
  194. strict: bool = ...
  195. ) -> None: ...
  196. def assert_array_almost_equal(
  197. x: _ArrayLikeNumber_co | _ArrayLikeObject_co,
  198. y: _ArrayLikeNumber_co | _ArrayLikeObject_co,
  199. decimal: float = ...,
  200. err_msg: str = ...,
  201. verbose: bool = ...,
  202. ) -> None: ...
  203. @overload
  204. def assert_array_less(
  205. x: _ArrayLikeNumber_co | _ArrayLikeObject_co,
  206. y: _ArrayLikeNumber_co | _ArrayLikeObject_co,
  207. err_msg: str = ...,
  208. verbose: bool = ...,
  209. ) -> None: ...
  210. @overload
  211. def assert_array_less(
  212. x: _ArrayLikeTD64_co,
  213. y: _ArrayLikeTD64_co,
  214. err_msg: str = ...,
  215. verbose: bool = ...,
  216. ) -> None: ...
  217. @overload
  218. def assert_array_less(
  219. x: _ArrayLikeDT64_co,
  220. y: _ArrayLikeDT64_co,
  221. err_msg: str = ...,
  222. verbose: bool = ...,
  223. ) -> None: ...
  224. def runstring(
  225. astr: str | bytes | types.CodeType,
  226. dict: None | dict[str, Any],
  227. ) -> Any: ...
  228. def assert_string_equal(actual: str, desired: str) -> None: ...
  229. def rundocs(
  230. filename: None | str | os.PathLike[str] = ...,
  231. raise_on_error: bool = ...,
  232. ) -> None: ...
  233. def raises(*args: type[BaseException]) -> Callable[[_FT], _FT]: ...
  234. @overload
  235. def assert_raises( # type: ignore
  236. expected_exception: type[BaseException] | tuple[type[BaseException], ...],
  237. callable: Callable[_P, Any],
  238. /,
  239. *args: _P.args,
  240. **kwargs: _P.kwargs,
  241. ) -> None: ...
  242. @overload
  243. def assert_raises(
  244. expected_exception: type[_ET] | tuple[type[_ET], ...],
  245. *,
  246. msg: None | str = ...,
  247. ) -> unittest.case._AssertRaisesContext[_ET]: ...
  248. @overload
  249. def assert_raises_regex(
  250. expected_exception: type[BaseException] | tuple[type[BaseException], ...],
  251. expected_regex: str | bytes | Pattern[Any],
  252. callable: Callable[_P, Any],
  253. /,
  254. *args: _P.args,
  255. **kwargs: _P.kwargs,
  256. ) -> None: ...
  257. @overload
  258. def assert_raises_regex(
  259. expected_exception: type[_ET] | tuple[type[_ET], ...],
  260. expected_regex: str | bytes | Pattern[Any],
  261. *,
  262. msg: None | str = ...,
  263. ) -> unittest.case._AssertRaisesContext[_ET]: ...
  264. def decorate_methods(
  265. cls: type[Any],
  266. decorator: Callable[[Callable[..., Any]], Any],
  267. testmatch: None | str | bytes | Pattern[Any] = ...,
  268. ) -> None: ...
  269. def measure(
  270. code_str: str | bytes | ast.mod | ast.AST,
  271. times: int = ...,
  272. label: None | str = ...,
  273. ) -> float: ...
  274. @overload
  275. def assert_allclose(
  276. actual: _ArrayLikeNumber_co | _ArrayLikeObject_co,
  277. desired: _ArrayLikeNumber_co | _ArrayLikeObject_co,
  278. rtol: float = ...,
  279. atol: float = ...,
  280. equal_nan: bool = ...,
  281. err_msg: str = ...,
  282. verbose: bool = ...,
  283. ) -> None: ...
  284. @overload
  285. def assert_allclose(
  286. actual: _ArrayLikeTD64_co,
  287. desired: _ArrayLikeTD64_co,
  288. rtol: float = ...,
  289. atol: float = ...,
  290. equal_nan: bool = ...,
  291. err_msg: str = ...,
  292. verbose: bool = ...,
  293. ) -> None: ...
  294. def assert_array_almost_equal_nulp(
  295. x: _ArrayLikeNumber_co,
  296. y: _ArrayLikeNumber_co,
  297. nulp: float = ...,
  298. ) -> None: ...
  299. def assert_array_max_ulp(
  300. a: _ArrayLikeNumber_co,
  301. b: _ArrayLikeNumber_co,
  302. maxulp: float = ...,
  303. dtype: DTypeLike = ...,
  304. ) -> NDArray[Any]: ...
  305. @overload
  306. def assert_warns(
  307. warning_class: type[Warning],
  308. ) -> contextlib._GeneratorContextManager[None]: ...
  309. @overload
  310. def assert_warns(
  311. warning_class: type[Warning],
  312. func: Callable[_P, _T],
  313. /,
  314. *args: _P.args,
  315. **kwargs: _P.kwargs,
  316. ) -> _T: ...
  317. @overload
  318. def assert_no_warnings() -> contextlib._GeneratorContextManager[None]: ...
  319. @overload
  320. def assert_no_warnings(
  321. func: Callable[_P, _T],
  322. /,
  323. *args: _P.args,
  324. **kwargs: _P.kwargs,
  325. ) -> _T: ...
  326. @overload
  327. def tempdir(
  328. suffix: None = ...,
  329. prefix: None = ...,
  330. dir: None = ...,
  331. ) -> contextlib._GeneratorContextManager[str]: ...
  332. @overload
  333. def tempdir(
  334. suffix: None | AnyStr = ...,
  335. prefix: None | AnyStr = ...,
  336. dir: None | AnyStr | os.PathLike[AnyStr] = ...,
  337. ) -> contextlib._GeneratorContextManager[AnyStr]: ...
  338. @overload
  339. def temppath(
  340. suffix: None = ...,
  341. prefix: None = ...,
  342. dir: None = ...,
  343. text: bool = ...,
  344. ) -> contextlib._GeneratorContextManager[str]: ...
  345. @overload
  346. def temppath(
  347. suffix: None | AnyStr = ...,
  348. prefix: None | AnyStr = ...,
  349. dir: None | AnyStr | os.PathLike[AnyStr] = ...,
  350. text: bool = ...,
  351. ) -> contextlib._GeneratorContextManager[AnyStr]: ...
  352. @overload
  353. def assert_no_gc_cycles() -> contextlib._GeneratorContextManager[None]: ...
  354. @overload
  355. def assert_no_gc_cycles(
  356. func: Callable[_P, Any],
  357. /,
  358. *args: _P.args,
  359. **kwargs: _P.kwargs,
  360. ) -> None: ...
  361. def break_cycles() -> None: ...