MCPcopy Index your code
hub / github.com/numpy/numpy / test_suppress_warnings_module

Function test_suppress_warnings_module

numpy/testing/tests/test_utils.py:1807–1856  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1805
1806@pytest.mark.thread_unsafe(reason="checks global module & deprecated warnings")
1807def test_suppress_warnings_module():
1808 # NOTE(seberg): We test for the DeprecationWarning mainly because on
1809 # free-threaded Python an "ignore" warning filters seem to collide with
1810 # parts of what `suppress_warnings` does (if used more than once?).
1811 with pytest.warns(
1812 DeprecationWarning,
1813 match="suppression and assertion utilities are deprecated"):
1814 # Initial state of module, no warnings
1815 my_mod = _get_fresh_mod()
1816 assert_equal(getattr(my_mod, '__warningregistry__', {}), {})
1817
1818 def warn_other_module():
1819 # Apply along axis is implemented in python; stacklevel=2 means
1820 # we end up inside its module, not ours.
1821 def warn(arr):
1822 warnings.warn("Some warning 2", stacklevel=2)
1823 return arr
1824 np.apply_along_axis(warn, 0, [0])
1825
1826 # Test module based warning suppression:
1827 assert_warn_len_equal(my_mod, 0)
1828 with suppress_warnings() as sup:
1829 sup.record(UserWarning)
1830 # suppress warning from other module (may have .pyc ending),
1831 # if apply_along_axis is moved, had to be changed.
1832 sup.filter(module=np.lib._shape_base_impl)
1833 warnings.warn("Some warning")
1834 warn_other_module()
1835 # Check that the suppression did test the file correctly (this module
1836 # got filtered)
1837 assert_equal(len(sup.log), 1)
1838 assert_equal(sup.log[0].message.args[0], "Some warning")
1839 assert_warn_len_equal(my_mod, 0)
1840 sup = suppress_warnings()
1841 # Will have to be changed if apply_along_axis is moved:
1842 sup.filter(module=my_mod)
1843 with sup:
1844 warnings.warn('Some warning')
1845 assert_warn_len_equal(my_mod, 0)
1846 # And test repeat works:
1847 sup.filter(module=my_mod)
1848 with sup:
1849 warnings.warn('Some warning')
1850 assert_warn_len_equal(my_mod, 0)
1851
1852 # Without specified modules
1853 with suppress_warnings():
1854 warnings.simplefilter('ignore')
1855 warnings.warn('Some warning')
1856 assert_warn_len_equal(my_mod, 0)
1857
1858
1859@pytest.mark.thread_unsafe(reason="checks global module & deprecated warnings")

Callers

nothing calls this directly

Calls 7

assert_equalFunction · 0.90
suppress_warningsClass · 0.90
_get_fresh_modFunction · 0.85
assert_warn_len_equalFunction · 0.85
warn_other_moduleFunction · 0.85
recordMethod · 0.80
filterMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…