Skip to content

API: Add and redefine numpy.bool [Array API] #25080

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

Merged
merged 3 commits into from
Nov 24, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/benchmarks/bench_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def time_divide_scalar2_inplace(self, dtype):

class CustomComparison(Benchmark):
params = (np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16,
np.uint32, np.uint64, np.float32, np.float64, np.bool_)
np.uint32, np.uint64, np.float32, np.float64, np.bool)
param_names = ['dtype']

def setup(self, dtype):
Expand Down
2 changes: 2 additions & 0 deletions doc/release/upcoming_changes/25080.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Redefine `numpy.bool` as an alias for `numpy.bool_` (as opposed to the `bool`
it was until NumPy 1.24) for Array API compatibility.
4 changes: 4 additions & 0 deletions doc/source/reference/arrays.scalars.rst
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ Other types
:members: __init__
:exclude-members: __init__

.. autoclass:: numpy.bool
:members: __init__
:exclude-members: __init__

.. autoclass:: numpy.datetime64
:members: __init__
:exclude-members: __init__
Expand Down
6 changes: 3 additions & 3 deletions doc/source/user/basics.types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ confusion with builtin python type names, such as `numpy.bool_`.
- Actual C type
- Description

* - `numpy.bool_`
* - `numpy.bool` or `numpy.bool_`
- N/A
- ``bool`` (defined in ``stdbool.h``)
- Boolean (True or False) stored as a byte.
Expand Down Expand Up @@ -141,7 +141,7 @@ aliases are provided (See :ref:`sized-aliases`).
NumPy numerical types are instances of ``dtype`` (data-type) objects, each
having unique characteristics. Once you have imported NumPy using
``>>> import numpy as np``
the dtypes are available as ``np.bool_``, ``np.float32``, etc.
the dtypes are available as ``np.bool``, ``np.float32``, etc.

Advanced types, not listed above, are explored in
section :ref:`structured_arrays`.
Expand Down Expand Up @@ -187,7 +187,7 @@ the type itself as a function. For example: ::
array([0, 1, 2], dtype=int8)

Note that, above, we use the *Python* float object as a dtype. NumPy knows
that ``int`` refers to ``np.int_``, ``bool`` means ``np.bool_``,
that ``int`` refers to ``np.int_``, ``bool`` means ``np.bool``,
that ``float`` is ``np.float64`` and ``complex`` is ``np.complex128``.
The other data-types do not have Python equivalents.

Expand Down
24 changes: 12 additions & 12 deletions numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,17 @@
array_str, asanyarray, asarray, ascontiguousarray, asfortranarray,
atleast_1d, atleast_2d, atleast_3d, base_repr, binary_repr,
bitwise_and, bitwise_count, bitwise_not, bitwise_or, bitwise_xor,
block, bool_, broadcast, busday_count, busday_offset, busdaycalendar,
byte, bytes_, can_cast, cbrt, cdouble, ceil, character, choose, clip,
clongdouble, complex128, complex64, complexfloating, compress,
concatenate, conj, conjugate, convolve, copysign, copyto, correlate,
cos, cosh, count_nonzero, cross, csingle, cumprod, cumproduct, cumsum,
datetime64, datetime_as_string, datetime_data, deg2rad, degrees,
diagonal, divide, divmod, dot, double, dtype, e, einsum, einsum_path,
empty, empty_like, equal, errstate, euler_gamma, exp, exp2, expm1,
fabs, finfo, flatiter, flatnonzero, flexible, float16, float32,
float64, float_power, floating, floor, floor_divide, fmax, fmin, fmod,
block, bool, bool_, broadcast, busday_count, busday_offset,
busdaycalendar, byte, bytes_, can_cast, cbrt, cdouble, ceil,
character, choose, clip, clongdouble, complex128, complex64,
complexfloating, compress, concatenate, conj, conjugate, convolve,
copysign, copyto, correlate, cos, cosh, count_nonzero, cross, csingle,
cumprod, cumproduct, cumsum, datetime64, datetime_as_string,
datetime_data, deg2rad, degrees, diagonal, divide, divmod, dot,
double, dtype, e, einsum, einsum_path, empty, empty_like, equal,
errstate, euler_gamma, exp, exp2, expm1, fabs, finfo, flatiter,
flatnonzero, flexible, float16, float32, float64, float_power,
floating, floor, floor_divide, fmax, fmin, fmod,
format_float_positional, format_float_scientific, frexp, from_dlpack,
frombuffer, fromfile, fromfunction, fromiter, frompyfunc, fromstring,
full, full_like, gcd, generic, geomspace, get_printoptions,
Expand Down Expand Up @@ -266,7 +267,6 @@

_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_")),
Expand All @@ -283,7 +283,7 @@
# 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", "str", "bytes", "object"}
__future_scalars__ = {"str", "bytes", "object"}

# now that numpy core module is imported, can initialize limits
_core.getlimits._register_known_types()
Expand Down
Loading