testing.pyi 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import re
  2. import sys
  3. import warnings
  4. import types
  5. import unittest
  6. import contextlib
  7. from collections.abc import Callable
  8. from typing import Any, TypeVar
  9. from pathlib import Path
  10. import numpy as np
  11. import numpy.typing as npt
  12. from typing_extensions import assert_type
  13. AR_f8: npt.NDArray[np.float64]
  14. AR_i8: npt.NDArray[np.int64]
  15. bool_obj: bool
  16. suppress_obj: np.testing.suppress_warnings
  17. FT = TypeVar("FT", bound=Callable[..., Any])
  18. def func() -> int: ...
  19. def func2(
  20. x: npt.NDArray[np.number[Any]],
  21. y: npt.NDArray[np.number[Any]],
  22. ) -> npt.NDArray[np.bool]: ...
  23. assert_type(np.testing.KnownFailureException(), np.testing.KnownFailureException)
  24. assert_type(np.testing.IgnoreException(), np.testing.IgnoreException)
  25. assert_type(
  26. np.testing.clear_and_catch_warnings(modules=[np.testing]),
  27. np.testing.clear_and_catch_warnings[None],
  28. )
  29. assert_type(
  30. np.testing.clear_and_catch_warnings(True),
  31. np.testing.clear_and_catch_warnings[list[warnings.WarningMessage]],
  32. )
  33. assert_type(
  34. np.testing.clear_and_catch_warnings(False),
  35. np.testing.clear_and_catch_warnings[None],
  36. )
  37. assert_type(
  38. np.testing.clear_and_catch_warnings(bool_obj),
  39. np.testing.clear_and_catch_warnings,
  40. )
  41. assert_type(
  42. np.testing.clear_and_catch_warnings.class_modules,
  43. tuple[types.ModuleType, ...],
  44. )
  45. assert_type(
  46. np.testing.clear_and_catch_warnings.modules,
  47. set[types.ModuleType],
  48. )
  49. with np.testing.clear_and_catch_warnings(True) as c1:
  50. assert_type(c1, list[warnings.WarningMessage])
  51. with np.testing.clear_and_catch_warnings() as c2:
  52. assert_type(c2, None)
  53. assert_type(np.testing.suppress_warnings("once"), np.testing.suppress_warnings)
  54. assert_type(np.testing.suppress_warnings()(func), Callable[[], int])
  55. assert_type(suppress_obj.filter(RuntimeWarning), None)
  56. assert_type(suppress_obj.record(RuntimeWarning), list[warnings.WarningMessage])
  57. with suppress_obj as c3:
  58. assert_type(c3, np.testing.suppress_warnings)
  59. assert_type(np.testing.verbose, int)
  60. assert_type(np.testing.IS_PYPY, bool)
  61. assert_type(np.testing.HAS_REFCOUNT, bool)
  62. assert_type(np.testing.HAS_LAPACK64, bool)
  63. assert_type(np.testing.assert_(1, msg="test"), None)
  64. assert_type(np.testing.assert_(2, msg=lambda: "test"), None)
  65. if sys.platform == "win32" or sys.platform == "cygwin":
  66. assert_type(np.testing.memusage(), int)
  67. elif sys.platform == "linux":
  68. assert_type(np.testing.memusage(), None | int)
  69. assert_type(np.testing.jiffies(), int)
  70. assert_type(np.testing.build_err_msg([0, 1, 2], "test"), str)
  71. assert_type(np.testing.build_err_msg(range(2), "test", header="header"), str)
  72. assert_type(np.testing.build_err_msg(np.arange(9).reshape(3, 3), "test", verbose=False), str)
  73. assert_type(np.testing.build_err_msg("abc", "test", names=["x", "y"]), str)
  74. assert_type(np.testing.build_err_msg([1.0, 2.0], "test", precision=5), str)
  75. assert_type(np.testing.assert_equal({1}, {1}), None)
  76. assert_type(np.testing.assert_equal([1, 2, 3], [1, 2, 3], err_msg="fail"), None)
  77. assert_type(np.testing.assert_equal(1, 1.0, verbose=True), None)
  78. assert_type(np.testing.print_assert_equal('Test XYZ of func xyz', [0, 1], [0, 1]), None)
  79. assert_type(np.testing.assert_almost_equal(1.0, 1.1), None)
  80. assert_type(np.testing.assert_almost_equal([1, 2, 3], [1, 2, 3], err_msg="fail"), None)
  81. assert_type(np.testing.assert_almost_equal(1, 1.0, verbose=True), None)
  82. assert_type(np.testing.assert_almost_equal(1, 1.0001, decimal=2), None)
  83. assert_type(np.testing.assert_approx_equal(1.0, 1.1), None)
  84. assert_type(np.testing.assert_approx_equal("1", "2", err_msg="fail"), None)
  85. assert_type(np.testing.assert_approx_equal(1, 1.0, verbose=True), None)
  86. assert_type(np.testing.assert_approx_equal(1, 1.0001, significant=2), None)
  87. assert_type(np.testing.assert_array_compare(func2, AR_i8, AR_f8, err_msg="test"), None)
  88. assert_type(np.testing.assert_array_compare(func2, AR_i8, AR_f8, verbose=True), None)
  89. assert_type(np.testing.assert_array_compare(func2, AR_i8, AR_f8, header="header"), None)
  90. assert_type(np.testing.assert_array_compare(func2, AR_i8, AR_f8, precision=np.int64()), None)
  91. assert_type(np.testing.assert_array_compare(func2, AR_i8, AR_f8, equal_nan=False), None)
  92. assert_type(np.testing.assert_array_compare(func2, AR_i8, AR_f8, equal_inf=True), None)
  93. assert_type(np.testing.assert_array_equal(AR_i8, AR_f8), None)
  94. assert_type(np.testing.assert_array_equal(AR_i8, AR_f8, err_msg="test"), None)
  95. assert_type(np.testing.assert_array_equal(AR_i8, AR_f8, verbose=True), None)
  96. assert_type(np.testing.assert_array_almost_equal(AR_i8, AR_f8), None)
  97. assert_type(np.testing.assert_array_almost_equal(AR_i8, AR_f8, err_msg="test"), None)
  98. assert_type(np.testing.assert_array_almost_equal(AR_i8, AR_f8, verbose=True), None)
  99. assert_type(np.testing.assert_array_almost_equal(AR_i8, AR_f8, decimal=1), None)
  100. assert_type(np.testing.assert_array_less(AR_i8, AR_f8), None)
  101. assert_type(np.testing.assert_array_less(AR_i8, AR_f8, err_msg="test"), None)
  102. assert_type(np.testing.assert_array_less(AR_i8, AR_f8, verbose=True), None)
  103. assert_type(np.testing.runstring("1 + 1", {}), Any)
  104. assert_type(np.testing.runstring("int64() + 1", {"int64": np.int64}), Any)
  105. assert_type(np.testing.assert_string_equal("1", "1"), None)
  106. assert_type(np.testing.rundocs(), None)
  107. assert_type(np.testing.rundocs("test.py"), None)
  108. assert_type(np.testing.rundocs(Path("test.py"), raise_on_error=True), None)
  109. def func3(a: int) -> bool: ...
  110. assert_type(
  111. np.testing.assert_raises(RuntimeWarning),
  112. unittest.case._AssertRaisesContext[RuntimeWarning],
  113. )
  114. assert_type(np.testing.assert_raises(RuntimeWarning, func3, 5), None)
  115. assert_type(
  116. np.testing.assert_raises_regex(RuntimeWarning, r"test"),
  117. unittest.case._AssertRaisesContext[RuntimeWarning],
  118. )
  119. assert_type(np.testing.assert_raises_regex(RuntimeWarning, b"test", func3, 5), None)
  120. assert_type(np.testing.assert_raises_regex(RuntimeWarning, re.compile(b"test"), func3, 5), None)
  121. class Test: ...
  122. def decorate(a: FT) -> FT:
  123. return a
  124. assert_type(np.testing.decorate_methods(Test, decorate), None)
  125. assert_type(np.testing.decorate_methods(Test, decorate, None), None)
  126. assert_type(np.testing.decorate_methods(Test, decorate, "test"), None)
  127. assert_type(np.testing.decorate_methods(Test, decorate, b"test"), None)
  128. assert_type(np.testing.decorate_methods(Test, decorate, re.compile("test")), None)
  129. assert_type(np.testing.measure("for i in range(1000): np.sqrt(i**2)"), float)
  130. assert_type(np.testing.measure(b"for i in range(1000): np.sqrt(i**2)", times=5), float)
  131. assert_type(np.testing.assert_allclose(AR_i8, AR_f8), None)
  132. assert_type(np.testing.assert_allclose(AR_i8, AR_f8, rtol=0.005), None)
  133. assert_type(np.testing.assert_allclose(AR_i8, AR_f8, atol=1), None)
  134. assert_type(np.testing.assert_allclose(AR_i8, AR_f8, equal_nan=True), None)
  135. assert_type(np.testing.assert_allclose(AR_i8, AR_f8, err_msg="err"), None)
  136. assert_type(np.testing.assert_allclose(AR_i8, AR_f8, verbose=False), None)
  137. assert_type(np.testing.assert_array_almost_equal_nulp(AR_i8, AR_f8, nulp=2), None)
  138. assert_type(np.testing.assert_array_max_ulp(AR_i8, AR_f8, maxulp=2), npt.NDArray[Any])
  139. assert_type(np.testing.assert_array_max_ulp(AR_i8, AR_f8, dtype=np.float32), npt.NDArray[Any])
  140. assert_type(np.testing.assert_warns(RuntimeWarning), contextlib._GeneratorContextManager[None])
  141. assert_type(np.testing.assert_warns(RuntimeWarning, func3, 5), bool)
  142. def func4(a: int, b: str) -> bool: ...
  143. assert_type(np.testing.assert_no_warnings(), contextlib._GeneratorContextManager[None])
  144. assert_type(np.testing.assert_no_warnings(func3, 5), bool)
  145. assert_type(np.testing.assert_no_warnings(func4, a=1, b="test"), bool)
  146. assert_type(np.testing.assert_no_warnings(func4, 1, "test"), bool)
  147. assert_type(np.testing.tempdir("test_dir"), contextlib._GeneratorContextManager[str])
  148. assert_type(np.testing.tempdir(prefix=b"test"), contextlib._GeneratorContextManager[bytes])
  149. assert_type(np.testing.tempdir("test_dir", dir=Path("here")), contextlib._GeneratorContextManager[str])
  150. assert_type(np.testing.temppath("test_dir", text=True), contextlib._GeneratorContextManager[str])
  151. assert_type(np.testing.temppath(prefix=b"test"), contextlib._GeneratorContextManager[bytes])
  152. assert_type(np.testing.temppath("test_dir", dir=Path("here")), contextlib._GeneratorContextManager[str])
  153. assert_type(np.testing.assert_no_gc_cycles(), contextlib._GeneratorContextManager[None])
  154. assert_type(np.testing.assert_no_gc_cycles(func3, 5), None)
  155. assert_type(np.testing.break_cycles(), None)
  156. assert_type(np.testing.TestCase(), unittest.case.TestCase)