summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/__init__.py40
-rw-r--r--numpy/core/tests/test_deprecations.py15
2 files changed, 53 insertions, 2 deletions
diff --git a/numpy/__init__.py b/numpy/__init__.py
index 1b5d90674..ea2b98163 100644
--- a/numpy/__init__.py
+++ b/numpy/__init__.py
@@ -158,6 +158,40 @@ else:
from . import matrixlib as _mat
from .matrixlib import *
+ # Deprecations introduced in NumPy 1.20.0, 2020-06-06
+ import builtins as _builtins
+
+ _msg = (
+ "module 'numpy' has no attribute '{n}'.\n"
+ "`np.{n}` was a deprecated alias for the builtin `{n}`. "
+ "To avoid this error in existing code, use `{n}` by itself. "
+ "Doing this will not modify any behavior and is safe. {extended_msg}\n"
+ "The aliases was originally deprecated in NumPy 1.20; for more "
+ "details and guidance see the original release note at:\n"
+ " https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations")
+
+ _specific_msg = (
+ "If you specifically wanted the numpy scalar type, use `np.{}` here.")
+
+ _int_extended_msg = (
+ "When replacing `np.{}`, you may wish to use e.g. `np.int64` "
+ "or `np.int32` to specify the precision. If you wish to review "
+ "your current use, check the release note link for "
+ "additional information.")
+
+ _type_info = [
+ ("object", ""), # The NumPy scalar only exists by name.
+ ("bool", _specific_msg.format("bool_")),
+ ("float", _specific_msg.format("float64")),
+ ("complex", _specific_msg.format("complex128")),
+ ("str", _specific_msg.format("str_")),
+ ("int", _int_extended_msg.format("int"))]
+
+ __former_attrs__ = {
+ n: _msg.format(n=n, extended_msg=extended_msg)
+ for n, extended_msg in _type_info
+ }
+
# Future warning introduced in NumPy 1.24.0, 2022-11-17
_msg = (
"`np.{n}` is a deprecated alias for `{an}`. (Deprecated NumPy 1.24)")
@@ -268,8 +302,10 @@ else:
# the AttributeError
warnings.warn(
f"In the future `np.{attr}` will be defined as the "
- "corresponding NumPy scalar. (This may have returned Python "
- "scalars in past versions.", FutureWarning, stacklevel=2)
+ "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
+
+ if attr in __former_attrs__:
+ raise AttributeError(__former_attrs__[attr])
# Importing Tester requires importing all of UnitTest which is not a
# cheap import Since it is mainly used in test suits, we lazy import it
diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py
index 4ec1f83d4..5c94f2fee 100644
--- a/numpy/core/tests/test_deprecations.py
+++ b/numpy/core/tests/test_deprecations.py
@@ -1098,3 +1098,18 @@ def test_future_scalar_attributes(name):
# Unfortunately, they are currently still valid via `np.dtype()`
np.dtype(name)
name in np.sctypeDict
+
+
+# Ignore the above future attribute warning for this test.
+@pytest.mark.filterwarnings("ignore:In the future:FutureWarning")
+class TestRemovedGlobals:
+ # Removed 2023-01-12, NumPy 1.24.0
+ # Not a deprecation, but the large error was added to aid those who missed
+ # the previous deprecation, and should be removed similarly to one
+ # (or faster).
+ @pytest.mark.parametrize("name",
+ ["object", "bool", "float", "complex", "str", "int"])
+ def test_attributeerror_includes_info(self, name):
+ msg = f".*\n`np.{name}` was a deprecated alias for the builtin"
+ with pytest.raises(AttributeError, match=msg):
+ getattr(np, name)