8000 Merge pull request #23912 from seberg/nep50-finalize · numpy/numpy@5abbb3a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5abbb3a

Browse files
authored
Merge pull request #23912 from seberg/nep50-finalize
API: Switch to NEP 50 behavior by default
2 parents 057ac96 + b5c8398 commit 5abbb3a

23 files changed

+360
-297
lines changed

benchmarks/benchmarks/bench_function_base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import numpy as np
44

5+
try:
6+
# SkipNotImplemented is available since 6.0
7+
from asv_runner.benchmarks.mark import SkipNotImplemented
8+
except ImportError:
9+
SkipNotImplemented = NotImplementedError
10+
11+
512
class Linspace(Benchmark):
613
def setup(self):
714
self.d = np.array([1, 2, 3])
@@ -169,6 +176,13 @@ def reversed(size, dtype):
169176
"""
170177
Returns an array that's in descending order.
171178
"""
179+
dtype = np.dtype(dtype)
180+
try:
181+
with np.errstate(over="raise"):
182+
res = dtype.type(size-1)
183+
except (OverflowError, FloatingPointError):
184+
raise SkipNotImplemented("Cannot construct arange for this size.")
185+
172186
return np.arange(size-1, -1, -1, dtype=dtype)
173187

174188
@staticmethod

doc/source/user/basics.creation.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ When you use :func:`numpy.array` to define a new array, you should
4343
consider the :doc:`dtype <basics.types>` of the elements in the array,
4444
which can be specified explicitly. This feature gives you
4545
more control over the underlying data structures and how the elements
46-
are handled in C/C++ functions. If you are not careful with ``dtype``
47-
assignments, you can get unwanted overflow, as such
48-
49-
::
50-
51-
>>> a = np.array([127, 128, 129], dtype=np.int8)
52-
>>> a
53-
array([ 127, -128, -127], dtype=int8)
46+
are handled in C/C++ functions.
47+
When values do not fit and you are using a ``dtype``, NumPy may raise an
48+
error::
49+
50+
>>> np.array([127, 128, 129], dtype=np.int8)
51+
Traceback (most recent call last):
52+
...
53+
OverflowError: Python integer 128 out of bounds for int8
5454

5555
An 8-bit signed integer represents integers from -128 to 127.
5656
Assigning the ``int8`` array to integers outside of this range results

numpy/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,9 @@ def hugepage_setup():
510510
# it is tidier organized.
511511
_core.multiarray._multiarray_umath._reload_guard()
512512

513-
# TODO: Switch to defaulting to "weak".
513+
# TODO: Remove the environment variable entirely now that it is "weak"
514514
_core._set_promotion_state(
515-
os.environ.get("NPY_PROMOTION_STATE", "legacy"))
515+
os.environ.get("NPY_PROMOTION_STATE", "weak"))
516516

517517
# Tell PyInstaller where to find hook-numpy.py
518518
def _pyinstaller_hooks_dir():

numpy/_core/code_generators/ufunc_docstrings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ def add_newdoc(place, name, doc):
18931893
result and can lead to unexpected results in some cases (see
18941894
:ref:`Casting Rules <ufuncs.casting>`):
18951895
1896-
>>> a = np.left_shift(np.uint8(255), 1) # Expect 254
1896+
>>> a = np.left_shift(np.uint8(255), np.int64(1)) # Expect 254
18971897
>>> print(a, type(a)) # Unexpected result due to upcasting
18981898
510 <class 'numpy.int64'>
18991899
>>> b = np.left_shift(np.uint8(255), np.uint8(1))

numpy/_core/multiarray.py

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,12 @@ def can_cast(from_, to, casting=None):
511511
can_cast(from_, to, casting='safe')
512512
513513
Returns True if cast between data types can occur according to the
514-
casting rule. If from is a scalar or array scalar, also returns
515-
True if the scalar value can be cast without overflow or truncation
516-
to an integer.
514+
casting rule.
517515
518516
Parameters
519517
----------
520-
from_ : dtype, dtype specifier, scalar, or array
521-
Data type, scalar, or array to cast from.
518+
from_ : dtype, dtype specifier, NumPy scalar, or array
519+
Data type, NumPy scalar, or array to cast from.
522520
to : dtype or dtype specifier
523521
Data type to cast to.
524522
casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
@@ -548,6 +546,10 @@ def can_cast(from_, to, casting=None):
548546
that the string dtype length is long enough to store the maximum
549547
integer/float value converted.
550548
549+
.. versionchanged:: 2.0
550+
This function does not support Python scalars anymore and does not
551+
apply any value-based logic for 0-D arrays and NumPy scalars.
552+
551553
See also
552554
--------
553555
dtype, result_type
@@ -570,52 +572,6 @@ def can_cast(from_, to, casting=None):
570572
>>> np.can_cast('i4', 'S4')
571573
False
572574
573-
Casting scalars
574-
575-
>>> np.can_cast(100, 'i1')
576-
True
577-
>>> np.can_cast(150, 'i1')
578-
False
579-
>>> np.can_cast(150, 'u1')
580-
True
581-
582-
>>> np.can_cast(3.5e100, np.float32)
583-
False
584-
>>> np.can_cast(1000.0, np.float32)
585-
True
586-
587-
Array scalar checks the value, array does not
588-
589-
>>> np.can_cast(np.array(1000.0), np.float32)
590-
True
591-
>>> np.can_cast(np.array([1000.0]), np.float32)
592-
False
593-
594-
Using the casting rules
595-
596-
>>> np.can_cast('i8', 'i8', 'no')
597-
True
598-
>>> np.can_cast('<i8', '>i8', 'no')
599-
False
600-
601-
>>> np.can_cast('<i8', '>i8', 'equiv')
602-
True
603-
>>> np.can_cast('<i4', '>i8', 'equiv')
604-
False
605-
606-
>>> np.can_cast('<i4', '>i8', 'safe')
607-
True
608-
>>> np.can_cast('<i8', '>i4', 'safe')
609-
False
610-
611-
>>> np.can_cast('<i8', '>i4', 'same_kind')
612-
True
613-
>>> np.can_cast('<i8', '>u4', 'same_kind')
614-
False
615-
616-
>>> np.can_cast('<i8', '>u4', 'unsafe')
617-
True
618-
619575
"""
620576
return (from_,)
621577

0 commit comments

Comments
 (0)
0