Skip to content

Commit 75fa3e1

Browse files
authored
TST: Use pytest.warns as ignore filters+suppress_warnings interfere (numpy#31623)
1 parent 66f4fdf commit 75fa3e1

1 file changed

Lines changed: 188 additions & 173 deletions

File tree

numpy/testing/tests/test_utils.py

Lines changed: 188 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,211 +1803,226 @@ def test_clear_and_catch_warnings():
18031803
assert_warn_len_equal(my_mod, 0)
18041804

18051805

1806-
@pytest.mark.filterwarnings(
1807-
"ignore:.*NumPy warning suppression and assertion utilities are deprecated"
1808-
".*:DeprecationWarning")
18091806
@pytest.mark.thread_unsafe(reason="checks global module & deprecated warnings")
18101807
def test_suppress_warnings_module():
1811-
# Initial state of module, no warnings
1812-
my_mod = _get_fresh_mod()
1813-
assert_equal(getattr(my_mod, '__warningregistry__', {}), {})
1814-
1815-
def warn_other_module():
1816-
# Apply along axis is implemented in python; stacklevel=2 means
1817-
# we end up inside its module, not ours.
1818-
def warn(arr):
1819-
warnings.warn("Some warning 2", stacklevel=2)
1820-
return arr
1821-
np.apply_along_axis(warn, 0, [0])
1822-
1823-
# Test module based warning suppression:
1824-
assert_warn_len_equal(my_mod, 0)
1825-
with suppress_warnings() as sup:
1826-
sup.record(UserWarning)
1827-
# suppress warning from other module (may have .pyc ending),
1828-
# if apply_along_axis is moved, had to be changed.
1829-
sup.filter(module=np.lib._shape_base_impl)
1830-
warnings.warn("Some warning")
1831-
warn_other_module()
1832-
# Check that the suppression did test the file correctly (this module
1833-
# got filtered)
1834-
assert_equal(len(sup.log), 1)
1835-
assert_equal(sup.log[0].message.args[0], "Some warning")
1836-
assert_warn_len_equal(my_mod, 0)
1837-
sup = suppress_warnings()
1838-
# Will have to be changed if apply_along_axis is moved:
1839-
sup.filter(module=my_mod)
1840-
with sup:
1841-
warnings.warn('Some warning')
1842-
assert_warn_len_equal(my_mod, 0)
1843-
# And test repeat works:
1844-
sup.filter(module=my_mod)
1845-
with sup:
1846-
warnings.warn('Some warning')
1847-
assert_warn_len_equal(my_mod, 0)
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)
18481851

1849-
# Without specified modules
1850-
with suppress_warnings():
1851-
warnings.simplefilter('ignore')
1852-
warnings.warn('Some warning')
1853-
assert_warn_len_equal(my_mod, 0)
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)
18541857

18551858

1856-
@pytest.mark.filterwarnings(
1857-
"ignore:.*NumPy warning suppression and assertion utilities are deprecated"
1858-
".*:DeprecationWarning")
18591859
@pytest.mark.thread_unsafe(reason="checks global module & deprecated warnings")
18601860
def test_suppress_warnings_type():
1861-
# Initial state of module, no warnings
1862-
my_mod = _get_fresh_mod()
1863-
assert_equal(getattr(my_mod, '__warningregistry__', {}), {})
1864-
1865-
# Test module based warning suppression:
1866-
with suppress_warnings() as sup:
1861+
# NOTE(seberg): We test for the DeprecationWarning mainly because on
1862+
# free-threaded Python an "ignore" warning filters seem to collide with
1863+
# parts of what `suppress_warnings` does (if used more than once?).
1864+
with pytest.warns(
1865+
DeprecationWarning,
1866+
match="suppression and assertion utilities are deprecated"):
1867+
# Initial state of module, no warnings
1868+
my_mod = _get_fresh_mod()
1869+
assert_equal(getattr(my_mod, '__warningregistry__', {}), {})
1870+
1871+
# Test module based warning suppression:
1872+
with suppress_warnings() as sup:
1873+
sup.filter(UserWarning)
1874+
warnings.warn('Some warning')
1875+
assert_warn_len_equal(my_mod, 0)
1876+
sup = suppress_warnings()
18671877
sup.filter(UserWarning)
1868-
warnings.warn('Some warning')
1869-
assert_warn_len_equal(my_mod, 0)
1870-
sup = suppress_warnings()
1871-
sup.filter(UserWarning)
1872-
with sup:
1873-
warnings.warn('Some warning')
1874-
assert_warn_len_equal(my_mod, 0)
1875-
# And test repeat works:
1876-
sup.filter(module=my_mod)
1877-
with sup:
1878-
warnings.warn('Some warning')
1879-
assert_warn_len_equal(my_mod, 0)
1878+
with sup:
1879+
warnings.warn('Some warning')
1880+
assert_warn_len_equal(my_mod, 0)
1881+
# And test repeat works:
1882+
sup.filter(module=my_mod)
1883+
with sup:
1884+
warnings.warn('Some warning')
1885+
assert_warn_len_equal(my_mod, 0)
18801886

1881-
# Without specified modules
1882-
with suppress_warnings():
1883-
warnings.simplefilter('ignore')
1884-
warnings.warn('Some warning')
1885-
assert_warn_len_equal(my_mod, 0)
1887+
# Without specified modules
1888+
with suppress_warnings():
1889+
warnings.simplefilter('ignore')
1890+
warnings.warn('Some warning')
1891+
assert_warn_len_equal(my_mod, 0)
18861892

18871893

1888-
@pytest.mark.filterwarnings(
1889-
"ignore:.*NumPy warning suppression and assertion utilities are deprecated"
1890-
".*:DeprecationWarning")
18911894
@pytest.mark.thread_unsafe(
18921895
reason="uses deprecated thread-unsafe warnings control utilities"
18931896
)
18941897
def test_suppress_warnings_decorate_no_record():
1895-
sup = suppress_warnings()
1896-
sup.filter(UserWarning)
1898+
# NOTE(seberg): We test for the DeprecationWarning mainly because on
1899+
# free-threaded Python an "ignore" warning filters seem to collide with
1900+
# parts of what `suppress_warnings` does (if used more than once?).
1901+
with pytest.warns(
1902+
DeprecationWarning,
1903+
match="suppression and assertion utilities are deprecated"):
1904+
sup = suppress_warnings()
1905+
sup.filter(UserWarning)
18971906

1898-
@sup
1899-
def warn(category):
1900-
warnings.warn('Some warning', category)
1907+
@sup
1908+
def warn(category):
1909+
warnings.warn('Some warning', category)
19011910

1902-
with warnings.catch_warnings(record=True) as w:
1903-
warnings.simplefilter("always")
1904-
warn(UserWarning) # should be suppressed
1905-
warn(RuntimeWarning)
1906-
assert_equal(len(w), 1)
1911+
with warnings.catch_warnings(record=True) as w:
1912+
warnings.simplefilter("always")
1913+
warn(UserWarning) # should be suppressed
1914+
warn(RuntimeWarning)
1915+
assert_equal(len(w), 1)
19071916

19081917

1909-
@pytest.mark.filterwarnings(
1910-
"ignore:.*NumPy warning suppression and assertion utilities are deprecated"
1911-
".*:DeprecationWarning")
19121918
@pytest.mark.thread_unsafe(
19131919
reason="uses deprecated thread-unsafe warnings control utilities"
19141920
)
19151921
def test_suppress_warnings_record():
1916-
sup = suppress_warnings()
1917-
log1 = sup.record()
1922+
# NOTE(seberg): We test for the DeprecationWarning mainly because on
1923+
# free-threaded Python an "ignore" warning filters seem to collide with
1924+
# parts of what `suppress_warnings` does (if used more than once?).
1925+
with pytest.warns(
1926+
DeprecationWarning,
1927+
match="suppression and assertion utilities are deprecated"):
1928+
sup = suppress_warnings()
1929+
log1 = sup.record()
19181930

1919-
with sup:
1920-
log2 = sup.record(message='Some other warning 2')
1921-
sup.filter(message='Some warning')
1922-
warnings.warn('Some warning')
1923-
warnings.warn('Some other warning')
1924-
warnings.warn('Some other warning 2')
1925-
1926-
assert_equal(len(sup.log), 2)
1927-
assert_equal(len(log1), 1)
1928-
assert_equal(len(log2), 1)
1929-
assert_equal(log2[0].message.args[0], 'Some other warning 2')
1930-
1931-
# Do it again, with the same context to see if some warnings survived:
1932-
with sup:
1933-
log2 = sup.record(message='Some other warning 2')
1934-
sup.filter(message='Some warning')
1935-
warnings.warn('Some warning')
1936-
warnings.warn('Some other warning')
1937-
warnings.warn('Some other warning 2')
1938-
1939-
assert_equal(len(sup.log), 2)
1940-
assert_equal(len(log1), 1)
1941-
assert_equal(len(log2), 1)
1942-
assert_equal(log2[0].message.args[0], 'Some other warning 2')
1943-
1944-
# Test nested:
1945-
with suppress_warnings() as sup:
1946-
sup.record()
1947-
with suppress_warnings() as sup2:
1948-
sup2.record(message='Some warning')
1931+
with sup:
1932+
log2 = sup.record(message='Some other warning 2')
1933+
sup.filter(message='Some warning')
19491934
warnings.warn('Some warning')
19501935
warnings.warn('Some other warning')
1951-
assert_equal(len(sup2.log), 1)
1952-
# includes a DeprecationWarning for suppress_warnings
1953-
assert_equal(len(sup.log), 2)
1936+
warnings.warn('Some other warning 2')
1937+
1938+
assert_equal(len(sup.log), 2)
1939+
assert_equal(len(log1), 1)
1940+
assert_equal(len(log2), 1)
1941+
assert_equal(log2[0].message.args[0], 'Some other warning 2')
1942+
1943+
# Do it again, with the same context to see if some warnings survived:
1944+
with sup:
1945+
log2 = sup.record(message='Some other warning 2')
1946+
sup.filter(message='Some warning')
1947+
warnings.warn('Some warning')
1948+
warnings.warn('Some other warning')
1949+
warnings.warn('Some other warning 2')
1950+
1951+
assert_equal(len(sup.log), 2)
1952+
assert_equal(len(log1), 1)
1953+
assert_equal(len(log2), 1)
1954+
assert_equal(log2[0].message.args[0], 'Some other warning 2')
1955+
1956+
# Test nested:
1957+
with suppress_warnings() as sup:
1958+
sup.record()
1959+
with suppress_warnings() as sup2:
1960+
sup2.record(message='Some warning')
1961+
warnings.warn('Some warning')
1962+
warnings.warn('Some other warning')
1963+
assert_equal(len(sup2.log), 1)
1964+
# includes a DeprecationWarning for suppress_warnings
1965+
assert_equal(len(sup.log), 2)
19541966

19551967

1956-
@pytest.mark.filterwarnings(
1957-
"ignore:.*NumPy warning suppression and assertion utilities are deprecated"
1958-
".*:DeprecationWarning")
19591968
@pytest.mark.thread_unsafe(
19601969
reason="uses deprecated thread-unsafe warnings control utilities"
19611970
)
19621971
def test_suppress_warnings_forwarding():
1963-
def warn_other_module():
1964-
# Apply along axis is implemented in python; stacklevel=2 means
1965-
# we end up inside its module, not ours.
1966-
def warn(arr):
1967-
warnings.warn("Some warning", stacklevel=2)
1968-
return arr
1969-
np.apply_along_axis(warn, 0, [0])
1970-
1971-
with suppress_warnings() as sup:
1972-
sup.record()
1973-
with suppress_warnings("always"):
1974-
for i in range(2):
1975-
warnings.warn("Some warning")
1976-
1977-
# includes a DeprecationWarning for suppress_warnings
1978-
assert_equal(len(sup.log), 3)
1979-
1980-
with suppress_warnings() as sup:
1981-
sup.record()
1982-
with suppress_warnings("location"):
1983-
for i in range(2):
1984-
warnings.warn("Some warning")
1985-
warnings.warn("Some warning")
1986-
1987-
# includes a DeprecationWarning for suppress_warnings
1988-
assert_equal(len(sup.log), 3)
1989-
1990-
with suppress_warnings() as sup:
1991-
sup.record()
1992-
with suppress_warnings("module"):
1993-
for i in range(2):
1994-
warnings.warn("Some warning")
1995-
warnings.warn("Some warning")
1996-
warn_other_module()
1997-
1998-
# includes a DeprecationWarning for suppress_warnings
1999-
assert_equal(len(sup.log), 3)
2000-
2001-
with suppress_warnings() as sup:
2002-
sup.record()
2003-
with suppress_warnings("once"):
2004-
for i in range(2):
2005-
warnings.warn("Some warning")
2006-
warnings.warn("Some other warning")
2007-
warn_other_module()
2008-
2009-
# includes a DeprecationWarning for suppress_warnings
2010-
assert_equal(len(sup.log), 3)
1972+
# NOTE(seberg): We test for the DeprecationWarning mainly because on
1973+
# free-threaded Python an "ignore" warning filters seem to collide with
1974+
# parts of what `suppress_warnings` does (if used more than once?).
1975+
with pytest.warns(
1976+
DeprecationWarning,
1977+
match="suppression and assertion utilities are deprecated"):
1978+
def warn_other_module():
1979+
# Apply along axis is implemented in python; stacklevel=2 means
1980+
# we end up inside its module, not ours.
1981+
def warn(arr):
1982+
warnings.warn("Some warning", stacklevel=2)
1983+
return arr
1984+
np.apply_along_axis(warn, 0, [0])
1985+
1986+
with suppress_warnings() as sup:
1987+
sup.record()
1988+
with suppress_warnings("always"):
1989+
for i in range(2):
1990+
warnings.warn("Some warning")
1991+
1992+
# includes a DeprecationWarning for suppress_warnings
1993+
assert_equal(len(sup.log), 3)
1994+
1995+
with suppress_warnings() as sup:
1996+
sup.record()
1997+
with suppress_warnings("location"):
1998+
for i in range(2):
1999+
warnings.warn("Some warning")
2000+
warnings.warn("Some warning")
2001+
2002+
# includes a DeprecationWarning for suppress_warnings
2003+
assert_equal(len(sup.log), 3)
2004+
2005+
with suppress_warnings() as sup:
2006+
sup.record()
2007+
with suppress_warnings("module"):
2008+
for i in range(2):
2009+
warnings.warn("Some warning")
2010+
warnings.warn("Some warning")
2011+
warn_other_module()
2012+
2013+
# includes a DeprecationWarning for suppress_warnings
2014+
assert_equal(len(sup.log), 3)
2015+
2016+
with suppress_warnings() as sup:
2017+
sup.record()
2018+
with suppress_warnings("once"):
2019+
for i in range(2):
2020+
warnings.warn("Some warning")
2021+
warnings.warn("Some other warning")
2022+
warn_other_module()
2023+
2024+
# includes a DeprecationWarning for suppress_warnings
2025+
assert_equal(len(sup.log), 3)
20112026

20122027

20132028
def test_tempdir():

0 commit comments

Comments
 (0)