Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: Add additional information to missing scalar AttributeError #22971

Merged
merged 1 commit into from Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 38 additions & 2 deletions numpy/__init__.py
Expand Up @@ -155,6 +155,40 @@
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)")
Expand Down Expand Up @@ -265,8 +299,10 @@ def _expired(*args, **kwds):
# 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
Expand Down
15 changes: 15 additions & 0 deletions numpy/core/tests/test_deprecations.py
Expand Up @@ -1176,3 +1176,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)