Scipy: PendingDeprecationWarning for np.matrix with pytest

Created on 31 Jan 2019  路  4Comments  路  Source: scipy/scipy

Pending Deprecation warnings about using the np.matrix class in scipy.sparse have been filtered out in https://github.com/scipy/scipy/pull/8887. However, when running pytest, these warnings still show up:

from scipy.sparse import csr_matrix
import numpy as np

def test_blah():
    m = np.arange(100).reshape(10, 10)
    m = csr_matrix(m)
    m.sum(axis=1)

test_blah()

Result:

no warning with python but pytest warns.

Would you have any suggestion / fix? We're having tons of warnings in the scikit-learn test suite (https://github.com/scikit-learn/scikit-learn/issues/12327).

Scipy/Numpy/Python version information:

>>> import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)
1.2.0 1.16.0 sys.version_info(major=3, minor=6, micro=6, releaselevel='final', serial=0)

pytest
Version: 4.2.0

Most helpful comment

Thanks @larsoner !

For scikit-learn directly yes, it could be a solution but I was more wondering about the wider issue.

A significant part of the scientific python stack depends on scipy (sparse is imported in other scipy modules as well I think) and expecting all of these packages to explicitly filter out DeprecationWarnings for pytest due to use of matrix (that would not only apply to scipy.sparse but also other uses of np.matrix) kind of defeats the purpose of this warning in the first place (and would also be a bit tedious). I personally encountered this in several projects I work with.

So I was more generally wondering if there was a way to make pytest respect the filterwarning in scipy.sparse by some change in scipy. Or alternatively if some other way of fixing this on the scipy or numpy side.

All 4 comments

So this only affects usage with pytest (not interactive usage). In https://github.com/scipy/scipy/pull/8887 such warnings are filtered out for the scipy test suite in pytest.ini but that still leaves all downstream projects to deal with these irrelevant warnings. We just wondering if anything could be done about it in scipy (or numpy).

At some point the np.matrix deprecation will be dealt with, but I'm not aware of any plan to hide the warnings in the meantime, though. I agree it would be nice to do it.

In case people are looking for a workaround in the meantime, in the downstream libs I manage I've added the following to setup.cfg:

[tool:pytest]
...
filterwarnings =
    ...
    ignore:the matrix subclass:PendingDeprecationWarning
    ...

@rth that might just be what we need right? If so, I can submit a PR

Thanks @larsoner !

For scikit-learn directly yes, it could be a solution but I was more wondering about the wider issue.

A significant part of the scientific python stack depends on scipy (sparse is imported in other scipy modules as well I think) and expecting all of these packages to explicitly filter out DeprecationWarnings for pytest due to use of matrix (that would not only apply to scipy.sparse but also other uses of np.matrix) kind of defeats the purpose of this warning in the first place (and would also be a bit tedious). I personally encountered this in several projects I work with.

So I was more generally wondering if there was a way to make pytest respect the filterwarning in scipy.sparse by some change in scipy. Or alternatively if some other way of fixing this on the scipy or numpy side.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JJLWHarrison picture JJLWHarrison  路  4Comments

alonemanuel picture alonemanuel  路  4Comments

leynier picture leynier  路  3Comments

gvr37leo picture gvr37leo  路  3Comments

asteppke picture asteppke  路  5Comments