8000 API: Reintroduce numpy.bool · numpy/numpy@f546273 · GitHub
[go: up one dir, main page]

Skip to content

Commit f546273

Browse files
committed
API: Reintroduce numpy.bool
1 parent 29cbb1f commit f546273

File tree

6 files changed

+33
-17
lines changed

6 files changed

+33
-17
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Redefine `numpy.bool` as an alias for `numpy.bool_` for Array API compatibility.

doc/source/reference/arrays.scalars.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ Other types
265265
:members: __init__
266266
:exclude-members: __init__
267267

268+
.. autoclass:: numpy.bool
269+
:members: __init__
270+
:exclude-members: __init__
271+
268272
.. autoclass:: numpy.datetime64
269273
:members: __init__
270274
:exclude-members: __init__

doc/source/user/basics.types.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ confusion with builtin python type names, such as `numpy.bool_`.
3030
- Actual C type
3131
- Description
3232

33-
* - `numpy.bool_`
33+
* - `numpy.bool_` or `numpy.bool`
3434
- N/A
3535
- ``bool`` (defined in ``stdbool.h``)
3636
- Boolean (True or False) stored as a byte.

numpy/__init__.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,17 @@
128128
array_str, asanyarray, asarray, ascontiguousarray, asfortranarray,
129129
atleast_1d, atleast_2d, atleast_3d, base_repr, binary_repr,
130130
bitwise_and, bitwise_count, bitwise_not, bitwise_or, bitwise_xor,
131-
block, bool_, broadcast, busday_count, busday_offset, busdaycalendar,
132-
byte, bytes_, can_cast, cbrt, cdouble, ceil, character, choose, clip,
133-
clongdouble, complex128, complex64, complexfloating, compress,
134-
concatenate, conj, conjugate, convolve, copysign, copyto, correlate,
135-
cos, cosh, count_nonzero, cross, csingle, cumprod, cumproduct, cumsum,
136-
datetime64, datetime_as_string, datetime_data, deg2rad, degrees,
137-
diagonal, divide, divmod, dot, double, dtype, e, einsum, einsum_path,
138-
empty, empty_like, equal, errstate, euler_gamma, exp, exp2, expm1,
139-
fabs, finfo, flatiter, flatnonzero, flexible, float16, float32,
140-
float64, float_power, floating, floor, floor_divide, fmax, fmin, fmod,
131+
block, bool_, broadcast, busday_count, busday_offset,
132+
busdaycalendar, byte, bytes_, can_cast, cbrt, cdouble, ceil,
133+
character, choose, clip, clongdouble, complex128, complex64,
134+
complexfloating, compress, concatenate, conj, conjugate, convolve,
135+
copysign, copyto, correlate, cos, cosh, count_nonzero, cross, csingle,
136+
cumprod, cumproduct, cumsum, datetime64, datetime_as_string,
137+
datetime_data, deg2rad, degrees, diagonal, divide, divmod, dot,
138+
double, dtype, e, einsum, einsum_path, empty, empty_like, equal,
139+
errstate, euler_gamma, exp, exp2, expm1, fabs, finfo, flatiter,
140+
flatnonzero, flexible, float16, float32, float64, float_power,
141+
floating, floor, floor_divide, fmax, fmin, fmod,
141142
format_float_positional, format_float_scientific, frexp, from_dlpack,
142143
frombuffer, fromfile, fromfunction, fromiter, frompyfunc, fromstring,
143144
full, full_like, gcd, generic, geomspace, get_printoptions,
@@ -266,7 +267,6 @@
266267

267268
_type_info = [
268269
("object", ""), # The NumPy scalar only exists by name.
269-
("bool", _specific_msg.format("bool_")),
270270
("float", _specific_msg.format("float64")),
271271
("complex", _specific_msg.format("complex128")),
272272
("str", _specific_msg.format("str_")),
@@ -283,7 +283,10 @@
283283
# probably wait for NumPy 1.26 or 2.0.
284284
# When defined, these should possibly not be added to `__all__` to avoid
285285
# import with `from numpy import *`.
286-
__future_scalars__ = {"bool", "str", "bytes", "object"}
286+
__future_scalars__ = {"str", "bytes", "object"}
287+
288+
# Redefined in NumPy 2.0.
289+
globals()["bool"] = bool_
287290

288291
# now that numpy core module is imported, can initialize limits
289292
_core.getlimits._register_known_types()

numpy/_core/tests/test_deprecations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ def create(value, dtype):
689689
pass # OverflowErrors always happened also before and are OK.
690690

691691

692-
@pytest.mark.parametrize("name", ["bool", "str", "bytes", "object"])
692+
@pytest.mark.parametrize("name", ["str", "bytes", "object"])
693693
def test_future_scalar_attributes(name):
694694
# FutureWarning added 2022-11-17, NumPy 1.24,
695695
assert name not in dir(np) # we may want to not add them
@@ -710,7 +710,7 @@ class TestRemovedGlobals:
710710
# the previous deprecation, and should be removed similarly to one
711711
# (or faster).
712712
@pytest.mark.parametrize("name",
713-
["object", "bool", "float", "complex", "str", "int"])
713+
["object", "float", "complex", "str", "int"])
714714
def test_attributeerror_includes_info(self, name):
715715
msg = f".*\n`np.{name}` was a deprecated alias for the builtin"
716716
with pytest.raises(AttributeError, match=msg):

numpy/tests/test_public_api.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,22 @@ def test_array_api_entry_point():
492492

493493
def test_main_namespace_all_dir_coherence():
494494
"""
495-
Checks if `dir(np)` and `np.__all__` are consistent
496-
and return same content, excluding private members.
495+
Checks if `dir(np)` and `np.__all__` are consistent and return
496+
the same content, excluding exceptions and private members.
497497
"""
498498
def _remove_private_members(member_set):
499499
return {m for m in member_set if not m.startswith('_')}
500500

501+
def _remove_exceptions(member_set):
502+
return member_set.difference({
503+
"bool" # included only in __dir__
504+
})
505+
501506
all_members = _remove_private_members(np.__all__)
507+
all_members = _remove_exceptions(all_members)
508+
502509
dir_members = _remove_private_members(np.__dir__())
510+
dir_members = _remove_exceptions(dir_members)
503511

504512
assert all_members == dir_members, (
505513
"Members that break symmetry: "

0 commit comments

Comments
 (0)
0