conftest.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. """
  2. Pytest configuration and fixtures for the Numpy test suite.
  3. """
  4. import os
  5. import string
  6. import sys
  7. import tempfile
  8. import warnings
  9. from contextlib import contextmanager
  10. import hypothesis
  11. import pytest
  12. import numpy
  13. import numpy as np
  14. from numpy._core._multiarray_tests import get_fpu_mode
  15. from numpy._core.tests._natype import get_stringdtype_dtype, pd_NA
  16. from numpy.testing._private.utils import NOGIL_BUILD
  17. try:
  18. from scipy_doctest.conftest import dt_config
  19. HAVE_SCPDT = True
  20. except ModuleNotFoundError:
  21. HAVE_SCPDT = False
  22. _old_fpu_mode = None
  23. _collect_results = {}
  24. # Use a known and persistent tmpdir for hypothesis' caches, which
  25. # can be automatically cleared by the OS or user.
  26. hypothesis.configuration.set_hypothesis_home_dir(
  27. os.path.join(tempfile.gettempdir(), ".hypothesis")
  28. )
  29. # We register two custom profiles for Numpy - for details see
  30. # https://hypothesis.readthedocs.io/en/latest/settings.html
  31. # The first is designed for our own CI runs; the latter also
  32. # forces determinism and is designed for use via np.test()
  33. hypothesis.settings.register_profile(
  34. name="numpy-profile", deadline=None, print_blob=True,
  35. )
  36. hypothesis.settings.register_profile(
  37. name="np.test() profile",
  38. deadline=None, print_blob=True, database=None, derandomize=True,
  39. suppress_health_check=list(hypothesis.HealthCheck),
  40. )
  41. # Note that the default profile is chosen based on the presence
  42. # of pytest.ini, but can be overridden by passing the
  43. # --hypothesis-profile=NAME argument to pytest.
  44. _pytest_ini = os.path.join(os.path.dirname(__file__), "..", "pytest.ini")
  45. hypothesis.settings.load_profile(
  46. "numpy-profile" if os.path.isfile(_pytest_ini) else "np.test() profile"
  47. )
  48. # The experimentalAPI is used in _umath_tests
  49. os.environ["NUMPY_EXPERIMENTAL_DTYPE_API"] = "1"
  50. def pytest_configure(config):
  51. config.addinivalue_line("markers",
  52. "valgrind_error: Tests that are known to error under valgrind.")
  53. config.addinivalue_line("markers",
  54. "leaks_references: Tests that are known to leak references.")
  55. config.addinivalue_line("markers",
  56. "slow: Tests that are very slow.")
  57. config.addinivalue_line("markers",
  58. "slow_pypy: Tests that are very slow on pypy.")
  59. def pytest_addoption(parser):
  60. parser.addoption("--available-memory", action="store", default=None,
  61. help=("Set amount of memory available for running the "
  62. "test suite. This can result to tests requiring "
  63. "especially large amounts of memory to be skipped. "
  64. "Equivalent to setting environment variable "
  65. "NPY_AVAILABLE_MEM. Default: determined"
  66. "automatically."))
  67. gil_enabled_at_start = True
  68. if NOGIL_BUILD:
  69. gil_enabled_at_start = sys._is_gil_enabled()
  70. def pytest_sessionstart(session):
  71. available_mem = session.config.getoption('available_memory')
  72. if available_mem is not None:
  73. os.environ['NPY_AVAILABLE_MEM'] = available_mem
  74. def pytest_terminal_summary(terminalreporter, exitstatus, config):
  75. if NOGIL_BUILD and not gil_enabled_at_start and sys._is_gil_enabled():
  76. tr = terminalreporter
  77. tr.ensure_newline()
  78. tr.section("GIL re-enabled", sep="=", red=True, bold=True)
  79. tr.line("The GIL was re-enabled at runtime during the tests.")
  80. tr.line("This can happen with no test failures if the RuntimeWarning")
  81. tr.line("raised by Python when this happens is filtered by a test.")
  82. tr.line("")
  83. tr.line("Please ensure all new C modules declare support for running")
  84. tr.line("without the GIL. Any new tests that intentionally imports ")
  85. tr.line("code that re-enables the GIL should do so in a subprocess.")
  86. pytest.exit("GIL re-enabled during tests", returncode=1)
  87. # FIXME when yield tests are gone.
  88. @pytest.hookimpl()
  89. def pytest_itemcollected(item):
  90. """
  91. Check FPU precision mode was not changed during test collection.
  92. The clumsy way we do it here is mainly necessary because numpy
  93. still uses yield tests, which can execute code at test collection
  94. time.
  95. """
  96. global _old_fpu_mode
  97. mode = get_fpu_mode()
  98. if _old_fpu_mode is None:
  99. _old_fpu_mode = mode
  100. elif mode != _old_fpu_mode:
  101. _collect_results[item] = (_old_fpu_mode, mode)
  102. _old_fpu_mode = mode
  103. @pytest.fixture(scope="function", autouse=True)
  104. def check_fpu_mode(request):
  105. """
  106. Check FPU precision mode was not changed during the test.
  107. """
  108. old_mode = get_fpu_mode()
  109. yield
  110. new_mode = get_fpu_mode()
  111. if old_mode != new_mode:
  112. raise AssertionError(f"FPU precision mode changed from {old_mode:#x} to "
  113. f"{new_mode:#x} during the test")
  114. collect_result = _collect_results.get(request.node)
  115. if collect_result is not None:
  116. old_mode, new_mode = collect_result
  117. raise AssertionError(f"FPU precision mode changed from {old_mode:#x} to "
  118. f"{new_mode:#x} when collecting the test")
  119. @pytest.fixture(autouse=True)
  120. def add_np(doctest_namespace):
  121. doctest_namespace['np'] = numpy
  122. @pytest.fixture(autouse=True)
  123. def env_setup(monkeypatch):
  124. monkeypatch.setenv('PYTHONHASHSEED', '0')
  125. if HAVE_SCPDT:
  126. @contextmanager
  127. def warnings_errors_and_rng(test=None):
  128. """Filter out the wall of DeprecationWarnings.
  129. """
  130. msgs = ["The numpy.linalg.linalg",
  131. "The numpy.fft.helper",
  132. "dep_util",
  133. "pkg_resources",
  134. "numpy.core.umath",
  135. "msvccompiler",
  136. "Deprecated call",
  137. "numpy.core",
  138. "Importing from numpy.matlib",
  139. "This function is deprecated.", # random_integers
  140. "Data type alias 'a'", # numpy.rec.fromfile
  141. "Arrays of 2-dimensional vectors", # matlib.cross
  142. "`in1d` is deprecated", ]
  143. msg = "|".join(msgs)
  144. msgs_r = [
  145. "invalid value encountered",
  146. "divide by zero encountered"
  147. ]
  148. msg_r = "|".join(msgs_r)
  149. with warnings.catch_warnings():
  150. warnings.filterwarnings(
  151. 'ignore', category=DeprecationWarning, message=msg
  152. )
  153. warnings.filterwarnings(
  154. 'ignore', category=RuntimeWarning, message=msg_r
  155. )
  156. yield
  157. # find and check doctests under this context manager
  158. dt_config.user_context_mgr = warnings_errors_and_rng
  159. # numpy specific tweaks from refguide-check
  160. dt_config.rndm_markers.add('#uninitialized')
  161. dt_config.rndm_markers.add('# uninitialized')
  162. # make the checker pick on mismatched dtypes
  163. dt_config.strict_check = True
  164. import doctest
  165. dt_config.optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
  166. # recognize the StringDType repr
  167. dt_config.check_namespace['StringDType'] = numpy.dtypes.StringDType
  168. # temporary skips
  169. dt_config.skiplist = {
  170. 'numpy.savez', # unclosed file
  171. 'numpy.matlib.savez',
  172. 'numpy.__array_namespace_info__',
  173. 'numpy.matlib.__array_namespace_info__',
  174. }
  175. # xfail problematic tutorials
  176. dt_config.pytest_extra_xfail = {
  177. 'how-to-verify-bug.rst': '',
  178. 'c-info.ufunc-tutorial.rst': '',
  179. 'basics.interoperability.rst': 'needs pandas',
  180. 'basics.dispatch.rst': 'errors out in /testing/overrides.py',
  181. 'basics.subclassing.rst': '.. testcode:: admonitions not understood',
  182. 'misc.rst': 'manipulates warnings',
  183. }
  184. # ignores are for things fail doctest collection (optionals etc)
  185. dt_config.pytest_extra_ignore = [
  186. 'numpy/distutils',
  187. 'numpy/_core/cversions.py',
  188. 'numpy/_pyinstaller',
  189. 'numpy/random/_examples',
  190. 'numpy/f2py/_backends/_distutils.py',
  191. ]
  192. @pytest.fixture
  193. def random_string_list():
  194. chars = list(string.ascii_letters + string.digits)
  195. chars = np.array(chars, dtype="U1")
  196. ret = np.random.choice(chars, size=100 * 10, replace=True)
  197. return ret.view("U100")
  198. @pytest.fixture(params=[True, False])
  199. def coerce(request):
  200. return request.param
  201. @pytest.fixture(
  202. params=["unset", None, pd_NA, np.nan, float("nan"), "__nan__"],
  203. ids=["unset", "None", "pandas.NA", "np.nan", "float('nan')", "string nan"],
  204. )
  205. def na_object(request):
  206. return request.param
  207. @pytest.fixture()
  208. def dtype(na_object, coerce):
  209. return get_stringdtype_dtype(na_object, coerce)