conftest.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. from contextlib import contextmanager
  9. import warnings
  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 pd_NA
  16. from numpy.testing._private.utils import NOGIL_BUILD, get_stringdtype_dtype
  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("FPU precision mode changed from {0:#x} to {1:#x}"
  113. " during the test".format(old_mode, new_mode))
  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("FPU precision mode changed from {0:#x} to {1:#x}"
  118. " when collecting the test".format(old_mode,
  119. new_mode))
  120. @pytest.fixture(autouse=True)
  121. def add_np(doctest_namespace):
  122. doctest_namespace['np'] = numpy
  123. @pytest.fixture(autouse=True)
  124. def env_setup(monkeypatch):
  125. monkeypatch.setenv('PYTHONHASHSEED', '0')
  126. if HAVE_SCPDT:
  127. @contextmanager
  128. def warnings_errors_and_rng(test=None):
  129. """Filter out the wall of DeprecationWarnings.
  130. """
  131. msgs = ["The numpy.linalg.linalg",
  132. "The numpy.fft.helper",
  133. "dep_util",
  134. "pkg_resources",
  135. "numpy.core.umath",
  136. "msvccompiler",
  137. "Deprecated call",
  138. "numpy.core",
  139. "`np.compat`",
  140. "Importing from numpy.matlib",
  141. "This function is deprecated.", # random_integers
  142. "Data type alias 'a'", # numpy.rec.fromfile
  143. "Arrays of 2-dimensional vectors", # matlib.cross
  144. "`in1d` is deprecated", ]
  145. msg = "|".join(msgs)
  146. msgs_r = [
  147. "invalid value encountered",
  148. "divide by zero encountered"
  149. ]
  150. msg_r = "|".join(msgs_r)
  151. with warnings.catch_warnings():
  152. warnings.filterwarnings(
  153. 'ignore', category=DeprecationWarning, message=msg
  154. )
  155. warnings.filterwarnings(
  156. 'ignore', category=RuntimeWarning, message=msg_r
  157. )
  158. yield
  159. # find and check doctests under this context manager
  160. dt_config.user_context_mgr = warnings_errors_and_rng
  161. # numpy specific tweaks from refguide-check
  162. dt_config.rndm_markers.add('#uninitialized')
  163. dt_config.rndm_markers.add('# uninitialized')
  164. # make the checker pick on mismatched dtypes
  165. dt_config.strict_check = True
  166. import doctest
  167. dt_config.optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
  168. # recognize the StringDType repr
  169. dt_config.check_namespace['StringDType'] = numpy.dtypes.StringDType
  170. # temporary skips
  171. dt_config.skiplist = {
  172. 'numpy.savez', # unclosed file
  173. 'numpy.matlib.savez',
  174. 'numpy.__array_namespace_info__',
  175. 'numpy.matlib.__array_namespace_info__',
  176. }
  177. # xfail problematic tutorials
  178. dt_config.pytest_extra_xfail = {
  179. 'how-to-verify-bug.rst': '',
  180. 'c-info.ufunc-tutorial.rst': '',
  181. 'basics.interoperability.rst': 'needs pandas',
  182. 'basics.dispatch.rst': 'errors out in /testing/overrides.py',
  183. 'basics.subclassing.rst': '.. testcode:: admonitions not understood',
  184. 'misc.rst': 'manipulates warnings',
  185. }
  186. # ignores are for things fail doctest collection (optionals etc)
  187. dt_config.pytest_extra_ignore = [
  188. 'numpy/distutils',
  189. 'numpy/_core/cversions.py',
  190. 'numpy/_pyinstaller',
  191. 'numpy/random/_examples',
  192. 'numpy/compat',
  193. 'numpy/f2py/_backends/_distutils.py',
  194. ]
  195. @pytest.fixture
  196. def random_string_list():
  197. chars = list(string.ascii_letters + string.digits)
  198. chars = np.array(chars, dtype="U1")
  199. ret = np.random.choice(chars, size=100 * 10, replace=True)
  200. return ret.view("U100")
  201. @pytest.fixture(params=[True, False])
  202. def coerce(request):
  203. return request.param
  204. @pytest.fixture(
  205. params=["unset", None, pd_NA, np.nan, float("nan"), "__nan__"],
  206. ids=["unset", "None", "pandas.NA", "np.nan", "float('nan')", "string nan"],
  207. )
  208. def na_object(request):
  209. return request.param
  210. @pytest.fixture()
  211. def dtype(na_object, coerce):
  212. return get_stringdtype_dtype(na_object, coerce)