MCPcopy Create free account
hub / github.com/numpy/numpy / assert_warns

Function assert_warns

numpy/testing/_private/utils.py:1726–1777  ·  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

1724
1725
1726def assert_warns(warning_class, *args, **kwargs):
1727 """
1728 Fail unless the given callable throws the specified warning.
1729
1730 A warning of class warning_class should be thrown by the callable when
1731 invoked with arguments args and keyword arguments kwargs.
1732 If a different type of warning is thrown, it will not be caught.
1733
1734 If called with all arguments other than the warning class omitted, may be
1735 used as a context manager:
1736
1737 with assert_warns(SomeWarning):
1738 do_something()
1739
1740 The ability to be used as a context manager is new in NumPy v1.11.0.
1741
1742 .. versionadded:: 1.4.0
1743
1744 Parameters
1745 ----------
1746 warning_class : class
1747 The class defining the warning that `func` is expected to throw.
1748 func : callable, optional
1749 Callable to test
1750 *args : Arguments
1751 Arguments for `func`.
1752 **kwargs : Kwargs
1753 Keyword arguments for `func`.
1754
1755 Returns
1756 -------
1757 The value returned by `func`.
1758
1759 Examples
1760 --------
1761 >>> import warnings
1762 >>> def deprecated_func(num):
1763 ... warnings.warn("Please upgrade", DeprecationWarning)
1764 ... return num*num
1765 >>> with np.testing.assert_warns(DeprecationWarning):
1766 ... assert deprecated_func(4) == 16
1767 >>> # or passing a func
1768 >>> ret = np.testing.assert_warns(DeprecationWarning, deprecated_func, 4)
1769 >>> assert ret == 16
1770 """
1771 if not args:
1772 return _assert_warns_context(warning_class)
1773
1774 func = args[0]
1775 args = args[1:]
1776 with _assert_warns_context(warning_class, name=func.__name__):
1777 return func(*args, **kwargs)
1778
1779
1780@contextlib.contextmanager

Calls 2

_assert_warns_contextFunction · 0.85
funcFunction · 0.50