Skip to content

Conversation

@ngoldbaum
Copy link
Member

@ngoldbaum ngoldbaum commented Aug 12, 2025

fixes #29293

Deprecates the following public API:

  • np.testing.assert_warns
  • np.testing.suppress_warnings

These solve problems that no longer exist in modern Python versions. As of Python 3.14, where warnings can be made thread-safe, they're actively harmful because the implementation is not thread-safe. We should deprecate these helpers and point people at the standard library or pytest.

assert_warns and suppress_warnings used to be used heavily in NumPy and SciPy, but #29322 cleaned up NumPy and scipy/scipy#23275 cleaned up SciPy. I don't think there are any substantial uses in active open source codebases besides NumPy and SciPy, judging by Github code searches.

I marked this for 2.5 but maybe we should backport it to the 2.4 branch?

I had to adjust some of the tests for suppress_warnings slightly, because now a new warning is getting generated.

I also used the skip_file_prefixes keyword for warnings.warn, which is only available on 3.12 and newer. Do I need to care about 3.11 support to make the warning context nice? There's no single stacklevel I can set.

An earlier version of this PR also deprecated clear_and_suppress_warnings and assert_no_warnings, but I ran into some issues getting the tests working again with the deprecation and I don't actually need to deprecate them, so I punted.

@ngoldbaum ngoldbaum added 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703) labels Aug 12, 2025
@ngoldbaum ngoldbaum removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Aug 12, 2025
with pytest.warns(DeprecationWarning):
ctypes_obj.get_strides() # type: ignore[deprecated] # pyright: ignore[reportDeprecated]
with np.testing.assert_warns(DeprecationWarning):
with pytest.warns(DeprecationWarning):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's also with pytest.deprecated_call(), probably doesn't matter

Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks. Yeah, I hope both isn't used too much, with pytest being long there.

I remember seeing suppress_warnings() in the wild in places where I didn't want to see it, but I suspect that is largely fixed (and I never wanted to see it there ;)).

But no backporting. This clearly isn't a bug fix, adding deprecation warnings and thus breaking someones CI in a bug fix release isn't acceptable (at least not without a clear argument that this is such a big deal that breaking peoples CI when they don't expect it is clearly worth it.).

@ngoldbaum ngoldbaum force-pushed the deprecate-warning-utils branch from 543bc5e to 55ff472 Compare August 14, 2025 01:06
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, good Chuck noticed it said 2.5. Would be nice to also include the version in the message.
And thanks for fixing the stacklevel, that new file skipping feature is neat, but since it works stacklevel seems nicer!

Otherwise, please just go ahead and self-merge.

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
@ngoldbaum
Copy link
Member Author

Test failures are transient network issues. Merging because the last commit just changed some error messages and is syntactically correct.

@ngoldbaum ngoldbaum merged commit 71a1b5c into numpy:main Aug 18, 2025
68 of 77 checks passed
MaanasArora pushed a commit to MaanasArora/numpy that referenced this pull request Aug 19, 2025
* DEP: Deprecate NumPy warning control utilities

* TST: fix mypy tests

* DEP: un-deprecate assert_no_warnings, which is thread-safe

* DEP: un-deprecate clear_and_catch_warnings

* TYP: update type stubs

* STY: appease ruff

* TST: update doctest deprecation filters

* TST: fix warning test

* DOC: add release note

* Update numpy/tests/test_warnings.py

* MNT: use stacklevel instead

* TST: use pytest.deprecated_call()

* MNT: fix deprecated version

* Apply suggestions from code review

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>

---------

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
* DEP: Deprecate NumPy warning control utilities

* TST: fix mypy tests

* DEP: un-deprecate assert_no_warnings, which is thread-safe

* DEP: un-deprecate clear_and_catch_warnings

* TYP: update type stubs

* STY: appease ruff

* TST: update doctest deprecation filters

* TST: fix warning test

* DOC: add release note

* Update numpy/tests/test_warnings.py

* MNT: use stacklevel instead

* TST: use pytest.deprecated_call()

* MNT: fix deprecated version

* Apply suggestions from code review

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>

---------

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

07 - Deprecation 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DEP: deprecate np.testing.suppress_warnings

6 participants