8000 MAINT: Change the type of error raised in set_printoptions (gh-13899) · numpy/numpy@feee90d · GitHub
[go: up one dir, main page]

Skip to content

Commit feee90d

Browse files
kianasunseberg
authored andcommitted
MAINT: Change the type of error raised in set_printoptions (gh-13899)
Previously an incorrect ``threshold`` raised ``ValueError``; it now raises ``TypeError`` for non-numeric types and ``ValueError`` for ``nan`` values.
1 parent 9259a7f commit feee90d

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

changelog/13899.changes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Incorrect ``threshold`` in ``np.set_printoptions`` raises ``TypeError`` or ``ValueError``
2+
-----------------------------------------------------------------------------------------
3+
Previously an incorrect ``threshold`` raised ``ValueError``; it now raises ``TypeError``
4+
for non-numeric types and ``ValueError`` for ``nan`` values.

numpy/core/arrayprint.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None,
8989
"`False`", stacklevel=3)
9090
if threshold is not None:
9191
# forbid the bad threshold arg suggested by stack overflow, gh-12351
92-
if not isinstance(threshold, numbers.Number) or np.isnan(threshold):
93-
raise ValueError("threshold must be numeric and non-NAN, try "
92+
if not isinstance(threshold, numbers.Number):
93+
raise TypeError("threshold must be numeric")
94+
if np.isnan(threshold):
95+
raise ValueError("threshold must be non-NAN, try "
9496
"sys.maxsize for untruncated representation")
9597
return options
9698

numpy/core/tests/test_arrayprint.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -847,9 +847,9 @@ def test_edgeitems(self):
847847
)
848848

849849
def test_bad_args(self):
850-
assert_raises(ValueError, np.set_printoptions, threshold='nan')
851-
assert_raises(ValueError, np.set_printoptions, threshold=u'1')
852-
assert_raises(ValueError, np.set_printoptions, threshold=b'1')
850+
assert_raises(ValueError, np.set_printoptions, threshold=float('nan'))
851+
assert_raises(TypeError, np.set_printoptions, threshold='1')
852+
assert_raises(TypeError, np.set_printoptions, threshold=b'1')
853853

854854
def test_unicode_object_array():
855855
import sys

0 commit comments

Comments
 (0)
0