Repository: scipy/scipy

Issue:
DeprecationWarnings in stats on python 3.5
```
/home/br/repos/scipy/build/testenv/lib/python3.5/site-packages/scipy/stats/tests/test_stats.py:101: DeprecationWarning: Please use assertRaisesRegex instead.
```

Apparently, `assertRaisesRegexp` was renamed to `assertRaisesRegex`:  https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaisesRegexp



Discussion:
Would it be better to check the version of python and then call the new function ?

`np.testing.assert_raises_regex` seems to fit the bill (I did not check anything other than `from numpy.testing import assert<TAB>` in IPython)

Thanks, Indeed the function `np.testing.assert_raises_regex` seems to handle this deprecation change across python versions.

@maniteja123 Can you help me understand the problem here? I didn't quite get it. I wish to fix this.

Hi, the function `assertRaisesRegexo` was renamed to `AssertRaisesRegex` in Python 3.5 and raises a DeprecationWarning when used. The numpy util function `np.testing.assert_raises_regex` seemed to be checking for version of python and call the appropriate function. So if we use this function [here](https://github.com/scipy/scipy/blob/master/scipy/stats/tests/test_stats.py#L100) the DeprecationWarning will be silenced when the tests are run. Hope it helps !


Resolving patch:
diff --git a/scipy/_lib/_numpy_compat.py b/scipy/_lib/_numpy_compat.py
--- a/scipy/_lib/_numpy_compat.py
+++ b/scipy/_lib/_numpy_compat.py
@@ -4,8 +4,10 @@
 from __future__ import division, print_function, absolute_import
 
 import warnings
+import sys
 
 import numpy as np
+from numpy.testing.nosetester import import_nose
 
 from scipy._lib._version import NumpyVersion
 
@@ -45,3 +47,28 @@ def _assert_warns(warning_class, func, *args, **kw):
                 raise AssertionError("First warning for %s is not a "
                         "%s( is %s)" % (func.__name__, warning_class, l[0]))
         return result
+
+
+def assert_raises_regex(exception_class, expected_regexp,
+                        callable_obj=None, *args, **kwargs):
+    """
+    Fail unless an exception of class exception_class and with message that
+    matches expected_regexp is thrown by callable when invoked with arguments
+    args and keyword arguments kwargs.
+    Name of this function adheres to Python 3.2+ reference, but should work in
+    all versions down to 2.6.
+    Notes
+    -----
+    .. versionadded:: 1.8.0
+    """
+    __tracebackhide__ = True  # Hide traceback for py.test
+    nose = import_nose()
+
+    if sys.version_info.major >= 3:
+        funcname = nose.tools.assert_raises_regex
+    else:
+        # Only present in Python 2.7, missing from unittest in 2.6
+            funcname = nose.tools.assert_raises_regexp
+
+    return funcname(exception_class, expected_regexp, callable_obj,
+                    *args, **kwargs)


Test patch:
diff --git a/scipy/stats/tests/test_stats.py b/scipy/stats/tests/test_stats.py
--- a/scipy/stats/tests/test_stats.py
+++ b/scipy/stats/tests/test_stats.py
@@ -18,6 +18,7 @@
                            assert_array_equal, assert_approx_equal,
                            assert_raises, run_module_suite, assert_allclose,
                            dec)
+from scipy._lib._numpy_compat import assert_raises_regex
 import numpy.ma.testutils as mat
 from numpy import array, arange, float32, float64, power
 import numpy as np
@@ -96,10 +97,11 @@ def test_tmin(self):
         assert_equal(stats.tmin(x, nan_policy='omit'), 0.)
         assert_raises(ValueError, stats.tmin, x, nan_policy='raise')
         assert_raises(ValueError, stats.tmin, x, nan_policy='foobar')
-
-        with self.assertRaisesRegexp(ValueError,
-                                     "'propagate', 'raise', 'omit'"):
-            stats.tmin(x, nan_policy='foo')
+        assert_raises_regex(ValueError,
+                            "'propagate', 'raise', 'omit'",
+                            stats.tmin,
+                            x,
+                            nan_policy='foo')
 
     def test_tmax(self):
         assert_equal(stats.tmax(4), 4)