Skip to content

Commit

Permalink
Merge pull request #18237 from charris/backport-18230
Browse files Browse the repository at this point in the history
DOC: Clarify the type alias deprecation message
  • Loading branch information
charris committed Jan 26, 2021
2 parents ee51e13 + cc2b7db commit da175ab
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 47 deletions.
70 changes: 48 additions & 22 deletions doc/source/release/1.20.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,60 @@ Using the aliases of builtin types like ``np.int`` is deprecated
----------------------------------------------------------------

For a long time, ``np.int`` has been an alias of the builtin ``int``. This is
repeatedly a cause of confusion for newcomers, and is also simply not useful.
repeatedly a cause of confusion for newcomers, and existed mainly for historic
reasons.

These aliases have been deprecated. The table below shows the full list of
deprecated aliases, along with their exact meaning. Replacing uses of items in
the first column with the contents of the second column will work identically
and silence the deprecation warning.

In many cases, it may have been intended to use the types from the third column.
Be aware that use of these types may result in subtle but desirable behavior
changes.

================== ================================= ==================================================================
Deprecated name Identical to Possibly intended numpy type
================== ================================= ==================================================================
``numpy.bool`` ``bool`` `numpy.bool_`
``numpy.int`` ``int`` `numpy.int_` (default int dtype), `numpy.cint` (C ``int``)
``numpy.float`` ``float`` `numpy.float_`, `numpy.double` (equivalent)
``numpy.complex`` ``complex`` `numpy.complex_`, `numpy.cdouble` (equivalent)
``numpy.object`` ``object`` `numpy.object_`
``numpy.str`` ``str`` `numpy.str_`
``numpy.long`` ``int`` (``long`` on Python 2) `numpy.int_` (C ``long``), `numpy.longlong` (largest integer type)
``numpy.unicode`` ``str`` (``unicode`` on Python 2) `numpy.unicode_`
================== ================================= ==================================================================

Note that for technical reasons these deprecation warnings will only be emitted
on Python 3.7 and above.
The third column lists alternative NumPy names which may occasionally be
preferential. See also :ref:`basics.types` for additional details.

================= ============ ==================================================================
Deprecated name Identical to NumPy scalar type names
================= ============ ==================================================================
``numpy.bool`` ``bool`` `numpy.bool_`
``numpy.int`` ``int`` `numpy.int_` (default), ``numpy.int64``, or ``numpy.int32``
``numpy.float`` ``float`` `numpy.float64`, `numpy.float_`, `numpy.double` (equivalent)
``numpy.complex`` ``complex`` `numpy.complex128`, `numpy.complex_`, `numpy.cdouble` (equivalent)
``numpy.object`` ``object`` `numpy.object_`
``numpy.str`` ``str`` `numpy.str_`
``numpy.long`` ``int`` `numpy.int_` (C ``long``), `numpy.longlong` (largest integer type)
``numpy.unicode`` ``str`` `numpy.unicode_`
================= ============ ==================================================================

To give a clear guideline for the vast majority of cases, for the types
``bool``, ``object``, ``str`` (and ``unicode``) using the plain version
is shorter and clear, and generally a good replacement.
For ``float`` and ``complex`` you can use ``float64`` and ``complex128``
if you wish to be more explicit about the precision.

For ``np.int`` a direct replacement with ``np.int_`` or ``int`` is also
good and will not change behavior, but the precision will continue to depend
on the computer and operating system.
If you want to be more explicit and review the current use, you have the
following alternatives:

* ``np.int64`` or ``np.int32`` to specify the precision exactly.
This ensures that results cannot depend on the computer or operating system.
* ``np.int_`` or ``int`` (the default), but be aware that it depends on
the computer and operating system.
* The C types: ``np.cint`` (int), ``np.int_`` (long), ``np.longlong``.
* ``np.intp`` which is 32bit on 32bit machines 64bit on 64bit machines.
This can be the best type to use for indexing.

When used with ``np.dtype(...)`` or ``dtype=...`` changing it to the
NumPy name as mentioned above will have no effect on the output.
If used as a scalar with::

np.float(123)

changing it can subtly change the result. In this case, the Python version
``float(123)`` or ``int(12.)`` is normally preferable, although the NumPy
version may be useful for consistency with NumPy arrays (for example,
NumPy behaves differently for things like division by zero).

(`gh-14882 <https://github.com/numpy/numpy/pull/14882>`__)

Expand Down Expand Up @@ -972,5 +1000,3 @@ The former result can still be obtained with::

(`gh-16841 <https://github.com/numpy/numpy/pull/16841>`__)



2 changes: 2 additions & 0 deletions doc/source/user/basics.types.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _basics.types:

**********
Data types
**********
Expand Down
74 changes: 49 additions & 25 deletions numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,34 +161,58 @@

# Deprecations introduced in NumPy 1.20.0, 2020-06-06
import builtins as _builtins

_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.")

_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"))]

__deprecated_attrs__.update({
n: (
getattr(_builtins, n),
"`np.{n}` is a deprecated alias for the builtin `{n}`. "
"Use `{n}` by itself, which is identical in behavior, to silence "
"this warning. "
"If you specifically wanted the numpy scalar type, use `np.{n}_` "
"here."
.format(n=n)
)
for n in ["bool", "int", "float", "complex", "object", "str"]
})
__deprecated_attrs__.update({
n: (
getattr(compat, n),
"`np.{n}` is a deprecated alias for `np.compat.{n}`. "
"Use `np.compat.{n}` by itself, which is identical in behavior, "
"to silence this warning. "
"In the likely event your code does not need to work on Python 2 "
"you can use the builtin ``{n2}`` for which ``np.compat.{n}`` is "
"itself an alias. "
"If you specifically wanted the numpy scalar type, use `np.{n2}_` "
"here."
.format(n=n, n2=n2)
)
for n, n2 in [("long", "int"), ("unicode", "str")]
n: (getattr(_builtins, n), _msg.format(n=n, extended_msg=extended_msg))
for n, extended_msg in _type_info
})

_msg = (
"`np.{n}` is a deprecated alias for `np.compat.{n}`. "
"To silence this warning, use `np.compat.{n}` by itself. "
"In the likely event your code does not need to work on Python 2 "
"you can use 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, "long"),
_msg.format(n="unciode", n2="str",
extended_msg=_specific_msg.format("str_")))

del _msg, _specific_msg, _int_extended_msg, _type_info, _builtins

from .core import round, abs, max, min
# now that numpy modules are imported, can initialize limits
core.getlimits._register_known_types()
Expand Down

0 comments on commit da175ab

Please sign in to comment.