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

Function assert_warns

numpy/testing/_private/utils.py:2000–2066  ·  view source on GitHub ↗

Fail unless the given callable throws the specified warning. A warning of class warning_class should be thrown by the callable when invoked with arguments args and keyword arguments kwargs. If a different type of warning is thrown, it will not be caught. If called with all arg

(warning_class, *args, **kwargs)

Source from the content-addressed store, hash-verified

1998
1999
2000def assert_warns(warning_class, *args, **kwargs):
2001 """
2002 Fail unless the given callable throws the specified warning.
2003
2004 A warning of class warning_class should be thrown by the callable when
2005 invoked with arguments args and keyword arguments kwargs.
2006 If a different type of warning is thrown, it will not be caught.
2007
2008 If called with all arguments other than the warning class omitted, may be
2009 used as a context manager::
2010
2011 with assert_warns(SomeWarning):
2012 do_something()
2013
2014 The ability to be used as a context manager is new in NumPy v1.11.0.
2015
2016 .. deprecated:: 2.4
2017
2018 This is deprecated. Use `warnings.catch_warnings` or
2019 ``pytest.warns`` instead.
2020
2021 Parameters
2022 ----------
2023 warning_class : class
2024 The class defining the warning that `func` is expected to throw.
2025 func : callable, optional
2026 Callable to test
2027 *args : Arguments
2028 Arguments for `func`.
2029 **kwargs : Kwargs
2030 Keyword arguments for `func`.
2031
2032 Returns
2033 -------
2034 The value returned by `func`.
2035
2036 Examples
2037 --------
2038 >>> import warnings
2039 >>> def deprecated_func(num):
2040 ... warnings.warn("Please upgrade", DeprecationWarning)
2041 ... return num*num
2042 >>> with np.testing.assert_warns(DeprecationWarning):
2043 ... assert deprecated_func(4) == 16
2044 >>> # or passing a func
2045 >>> ret = np.testing.assert_warns(DeprecationWarning, deprecated_func, 4)
2046 >>> assert ret == 16
2047 """
2048 warnings.warn(
2049 "NumPy warning suppression and assertion utilities are deprecated. "
2050 "Use warnings.catch_warnings, warnings.filterwarnings, pytest.warns, "
2051 "or pytest.filterwarnings instead. (Deprecated NumPy 2.4)",
2052 DeprecationWarning, stacklevel=2)
2053 if not args and not kwargs:
2054 return _assert_warns_context(warning_class)
2055 elif len(args) < 1:
2056 if "match" in kwargs:
2057 raise RuntimeError(

Callers 4

test_warnMethod · 0.90
test_context_managerMethod · 0.90
test_argsMethod · 0.90

Calls 2

_assert_warns_contextFunction · 0.85
funcFunction · 0.50

Tested by 4

test_warnMethod · 0.72
test_context_managerMethod · 0.72
test_argsMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…