utils.pyi 9.8 KB

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