diff options
author | Ralf Gommers <ralf.gommers@gmail.com> | 2022-11-21 10:05:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-21 10:05:31 +0100 |
commit | 742545f1336e7cd12cdf656792c44b90b9f0f4b9 (patch) | |
tree | 3ebfee7206e45d4b06c6eb33fb45d9dca69b6c54 /numpy | |
parent | 5f0b8466b20642dd4d80fe79ce6d08331c34cc3b (diff) | |
parent | d41658143d15dfe2f17ad7fd11467dcdba9c3070 (diff) | |
download | numpy-742545f1336e7cd12cdf656792c44b90b9f0f4b9.tar.gz |
Merge pull request #22607 from seberg/scalar-aliases
DEP: Next step in scalar type alias deprecations/futurewarnings
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/__init__.py | 88 | ||||
-rw-r--r-- | numpy/__init__.pyi | 10 | ||||
-rw-r--r-- | numpy/core/_type_aliases.py | 12 | ||||
-rw-r--r-- | numpy/core/numerictypes.py | 4 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 48 | ||||
-rw-r--r-- | numpy/core/tests/test_numerictypes.py | 5 | ||||
-rw-r--r-- | numpy/typing/tests/data/pass/scalars.py | 7 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/scalars.pyi | 8 |
8 files changed, 67 insertions, 115 deletions
diff --git a/numpy/__init__.py b/numpy/__init__.py index 22c90677e..bdea595ed 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -155,70 +155,34 @@ else: from . import matrixlib as _mat from .matrixlib import * - # Deprecations introduced in NumPy 1.20.0, 2020-06-06 - import builtins as _builtins - + # Future warning introduced in NumPy 1.24.0, 2022-11-17 _msg = ( - "`np.{n}` is a deprecated alias for the builtin `{n}`. " - "To silence this warning, use `{n}` by itself. Doing this will not " - "modify any behavior and is safe. {extended_msg}\n" - "Deprecated in NumPy 1.20; for more details and guidance: " - "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.") + "`np.{n}` is a deprecated alias for `{an}`. (Deprecated NumPy 1.24)") + # Some of these are awkward (since `np.str` may be preferable in the long + # term), but overall the names ending in 0 seem undesireable _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"))] + ("bool8", bool_, "np.bool_"), + ("int0", intp, "np.intp"), + ("uint0", uintp, "np.uintp"), + ("str0", str_, "np.str_"), + ("bytes0", bytes_, "np.bytes_"), + ("void0", void, "np.void"), + ("object0", object_, + "`np.object0` is a deprecated alias for `np.object_`. " + "`object` can be used instead. (Deprecated NumPy 1.24)")] + + # Some of these could be defined right away, but most were aliases to + # the Python objects and only removed in NumPy 1.24. Defining them should + # probably wait for NumPy 1.26 or 2.0. + # When defined, these should possibly not be added to `__all__` to avoid + # import with `from numpy import *`. + __future_scalars__ = {"bool", "long", "ulong", "str", "bytes", "object"} __deprecated_attrs__.update({ - n: (getattr(_builtins, n), _msg.format(n=n, extended_msg=extended_msg)) - for n, extended_msg in _type_info - }) - - # Numpy 1.20.0, 2020-10-19 - __deprecated_attrs__["typeDict"] = ( - core.numerictypes.typeDict, - "`np.typeDict` is a deprecated alias for `np.sctypeDict`." - ) - - # NumPy 1.22, 2021-10-20 - __deprecated_attrs__["MachAr"] = ( - core._machar.MachAr, - "`np.MachAr` is deprecated (NumPy 1.22)." - ) + n: (alias, _msg.format(n=n, an=an)) for n, alias, an in _type_info}) - _msg = ( - "`np.{n}` is a deprecated alias for `np.compat.{n}`. " - "To silence this warning, use `np.compat.{n}` by itself, or " - "the builtin `{n2}` for which `np.compat.{n}` is itself an " - "alias. Doing this will not modify any behaviour and is safe. " - "{extended_msg}\n" - "Deprecated in NumPy 1.20; for more details and guidance: " - "https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations") - - __deprecated_attrs__["long"] = ( - getattr(compat, "long"), - _msg.format(n="long", n2="int", - extended_msg=_int_extended_msg.format("long"))) - - __deprecated_attrs__["unicode"] = ( - getattr(compat, "unicode"), - _msg.format(n="unicode", n2="str", - extended_msg=_specific_msg.format("str_"))) - - del _msg, _specific_msg, _int_extended_msg, _type_info, _builtins + del _msg, _type_info from .core import round, abs, max, min # now that numpy modules are imported, can initialize limits @@ -296,6 +260,14 @@ else: warnings.warn(msg, DeprecationWarning, stacklevel=2) return val + if attr in __future_scalars__: + # And future warnings for those that will change, but also give + # 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) + # 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 # here to save on the order of 10 ms of import time for most users diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 9ee634742..0a3bbcdb3 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -2744,8 +2744,6 @@ class bool_(generic): __gt__: _ComparisonOp[_NumberLike_co, _ArrayLikeNumber_co] __ge__: _ComparisonOp[_NumberLike_co, _ArrayLikeNumber_co] -bool8 = bool_ - class object_(generic): def __init__(self, value: object = ..., /) -> None: ... @property @@ -2758,8 +2756,6 @@ class object_(generic): def __float__(self) -> float: ... def __complex__(self) -> complex: ... -object0 = object_ - # The `datetime64` constructors requires an object with the three attributes below, # and thus supports datetime duck typing class _DatetimeScalar(Protocol): @@ -2882,7 +2878,6 @@ byte = signedinteger[_NBitByte] short = signedinteger[_NBitShort] intc = signedinteger[_NBitIntC] intp = signedinteger[_NBitIntP] -int0 = signedinteger[_NBitIntP] int_ = signedinteger[_NBitInt] longlong = signedinteger[_NBitLongLong] @@ -2964,7 +2959,6 @@ ubyte = unsignedinteger[_NBitByte] ushort = unsignedinteger[_NBitShort] uintc = unsignedinteger[_NBitIntC] uintp = unsignedinteger[_NBitIntP] -uint0 = unsignedinteger[_NBitIntP] uint = unsignedinteger[_NBitInt] ulonglong = unsignedinteger[_NBitLongLong] @@ -3089,8 +3083,6 @@ class void(flexible): value: ArrayLike, ) -> None: ... -void0 = void - class character(flexible): # type: ignore def __int__(self) -> int: ... def __float__(self) -> float: ... @@ -3111,7 +3103,6 @@ class bytes_(character, bytes): def tolist(self) -> bytes: ... string_ = bytes_ -bytes0 = bytes_ class str_(character, str): @overload @@ -3126,7 +3117,6 @@ class str_(character, str): def tolist(self) -> str: ... unicode_ = str_ -str0 = str_ # # Constants diff --git a/numpy/core/_type_aliases.py b/numpy/core/_type_aliases.py index 9b1dcb8cf..5adabfb03 100644 --- a/numpy/core/_type_aliases.py +++ b/numpy/core/_type_aliases.py @@ -107,14 +107,16 @@ def _add_aliases(): if name in ('longdouble', 'clongdouble') and myname in allTypes: continue - allTypes[myname] = info.type - - # add mapping for both the bit name and the numarray name - sctypeDict[myname] = info.type + # Add to the main namespace if desired: + if bit != 0 and base != "bool": + allTypes[myname] = info.type # add forward, reverse, and string mapping to numarray sctypeDict[char] = info.type + # add mapping for both the bit name + sctypeDict[myname] = info.type + _add_aliases() @@ -148,8 +150,6 @@ void = allTypes['void'] # def _set_up_aliases(): type_pairs = [('complex_', 'cdouble'), - ('int0', 'intp'), - ('uint0', 'uintp'), ('single', 'float'), ('csingle', 'cfloat'), ('singlecomplex', 'cfloat'), diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 3d1cb6fd1..21a00ac6f 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -47,14 +47,14 @@ Exported symbols include: | | | byte | | | short | | | intc - | | | intp int0 + | | | intp | | | int_ | | | longlong | | \\-> unsignedinteger (uintxx) (kind=u) | | ubyte | | ushort | | uintc - | | uintp uint0 + | | uintp | | uint_ | | ulonglong | +-> inexact diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 3ac7f0319..dba301418 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -583,25 +583,6 @@ class TestNonExactMatchDeprecation(_DeprecationTestCase): self.assert_deprecated(lambda: np.searchsorted(arr[0], 4, side='Random')) -class TestDeprecatedGlobals(_DeprecationTestCase): - # 2020-06-06 - def test_type_aliases(self): - # from builtins - self.assert_deprecated(lambda: np.bool(True)) - self.assert_deprecated(lambda: np.int(1)) - self.assert_deprecated(lambda: np.float(1)) - self.assert_deprecated(lambda: np.complex(1)) - self.assert_deprecated(lambda: np.object()) - self.assert_deprecated(lambda: np.str('abc')) - - # from np.compat - self.assert_deprecated(lambda: np.long(1)) - self.assert_deprecated(lambda: np.unicode('abc')) - - # from np.core.numerictypes - self.assert_deprecated(lambda: np.typeDict) - - class TestMatrixInOuter(_DeprecationTestCase): # 2020-05-13 NumPy 1.20.0 message = (r"add.outer\(\) was passed a numpy matrix as " @@ -1046,9 +1027,6 @@ class TestMachAr(_DeprecationTestCase): # Deprecated 2021-10-19, NumPy 1.22 warning_cls = DeprecationWarning - def test_deprecated(self): - self.assert_deprecated(lambda: np.MachAr) - def test_deprecated_module(self): self.assert_deprecated(lambda: getattr(np.core, "machar")) @@ -1172,3 +1150,29 @@ class TestPyIntConversion(_DeprecationTestCase): lambda: creation_func(info.max + 1, dtype)) except OverflowError: pass # OverflowErrors always happened also before and are OK. + + +class TestDeprecatedGlobals(_DeprecationTestCase): + # Deprecated 2022-11-17, NumPy 1.24 + def test_type_aliases(self): + # from builtins + self.assert_deprecated(lambda: np.bool8) + self.assert_deprecated(lambda: np.int0) + self.assert_deprecated(lambda: np.uint0) + self.assert_deprecated(lambda: np.bytes0) + self.assert_deprecated(lambda: np.str0) + self.assert_deprecated(lambda: np.object0) + + +@pytest.mark.parametrize("name", + ["bool", "long", "ulong", "str", "bytes", "object"]) +def test_future_scalar_attributes(name): + # FutureWarning added 2022-11-17, NumPy 1.24, + assert name not in dir(np) # we may want to not add them + with pytest.warns(FutureWarning, + match=f"In the future .*{name}"): + assert not hasattr(np, name) + + # Unfortunately, they are currently still valid via `np.dtype()` + np.dtype(name) + name in np.sctypeDict diff --git a/numpy/core/tests/test_numerictypes.py b/numpy/core/tests/test_numerictypes.py index ffe8716e7..072cd65fe 100644 --- a/numpy/core/tests/test_numerictypes.py +++ b/numpy/core/tests/test_numerictypes.py @@ -443,8 +443,9 @@ class TestSctypeDict: # alias for np.int_, but np.long is not supported for historical # reasons (gh-21063) assert_(np.sctypeDict['ulong'] is np.uint) - assert_(not hasattr(np, 'ulong')) - + with pytest.warns(FutureWarning): + # We will probably allow this in the future: + assert not hasattr(np, 'ulong') class TestBitName: def test_abstract(self): diff --git a/numpy/typing/tests/data/pass/scalars.py b/numpy/typing/tests/data/pass/scalars.py index 684d41fad..124681bcb 100644 --- a/numpy/typing/tests/data/pass/scalars.py +++ b/numpy/typing/tests/data/pass/scalars.py @@ -173,18 +173,12 @@ c16.byteswap() c16.transpose() # Aliases -np.str0() -np.bool8() -np.bytes0() np.string_() -np.object0() -np.void0(0) np.byte() np.short() np.intc() np.intp() -np.int0() np.int_() np.longlong() @@ -192,7 +186,6 @@ np.ubyte() np.ushort() np.uintc() np.uintp() -np.uint0() np.uint() np.ulonglong() diff --git a/numpy/typing/tests/data/reveal/scalars.pyi b/numpy/typing/tests/data/reveal/scalars.pyi index 383e40ef0..b7fc75acc 100644 --- a/numpy/typing/tests/data/reveal/scalars.pyi +++ b/numpy/typing/tests/data/reveal/scalars.pyi @@ -35,7 +35,6 @@ reveal_type(c8.real) # E: {float32} reveal_type(c16.imag) # E: {float64} reveal_type(np.unicode_('foo')) # E: str_ -reveal_type(np.str0('foo')) # E: str_ reveal_type(V[0]) # E: Any reveal_type(V["field1"]) # E: Any @@ -44,18 +43,12 @@ V[0] = 5 # Aliases reveal_type(np.unicode_()) # E: str_ -reveal_type(np.str0()) # E: str_ -reveal_type(np.bool8()) # E: bool_ -reveal_type(np.bytes0()) # E: bytes_ reveal_type(np.string_()) # E: bytes_ -reveal_type(np.object0()) # E: object_ -reveal_type(np.void0(0)) # E: void reveal_type(np.byte()) # E: {byte} reveal_type(np.short()) # E: {short} reveal_type(np.intc()) # E: {intc} reveal_type(np.intp()) # E: {intp} -reveal_type(np.int0()) # E: {intp} reveal_type(np.int_()) # E: {int_} reveal_type(np.longlong()) # E: {longlong} @@ -63,7 +56,6 @@ reveal_type(np.ubyte()) # E: {ubyte} reveal_type(np.ushort()) # E: {ushort} reveal_type(np.uintc()) # E: {uintc} reveal_type(np.uintp()) # E: {uintp} -reveal_type(np.uint0()) # E: {uintp} reveal_type(np.uint()) # E: {uint} reveal_type(np.ulonglong()) # E: {ulonglong} |