Use module level `__getattr__`
+import sys
import warnings
-warnings.warn(
- 'cupy.sparse is deprecated. Use cupyx.scipy.sparse instead.',
- DeprecationWarning)
-
+import cupyx.scipy.sparse
+
+
+# Raise a `DeprecationWarning` for `cupy.sparse` submodule when its functions
+# are called. We could raise the warning on importing the submodule, but we
+# use module level `__getattr__` function here as the submodule is also
+# imported in cupy/__init__.py. Unfortunately, module level `__getattr__` is
+# supported on Python 3.7 and higher, so we need to keep the explicit import
+# list for older Python.
+if (3, 7) <= sys.version_info:
+ def __getattr__(name):
+ if hasattr(cupyx.scipy.sparse, name):
+ msg = 'cupy.sparse is deprecated. Use cupyx.scipy.sparse instead.'
+ warnings.warn(msg, DeprecationWarning)
+ return getattr(cupyx.scipy.sparse, name)
+else:
from cupyx.scipy.sparse.base import issparse # NOQA
from cupyx.scipy.sparse.base import isspmatrix # NOQA
from cupyx.scipy.sparse.base import spmatrix # NOQA
