Skip to content

Commit

Permalink
Merge pull request #8885 from guilhermeleobas/guilhermeloebas/fix-np-…
Browse files Browse the repository at this point in the history
…allclose

Fix np.allclose not handling default args
  • Loading branch information
sklam committed May 9, 2023
2 parents 08a514b + 202d12c commit 7cd3e30
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
18 changes: 9 additions & 9 deletions numba/np/arraymath.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,22 +840,22 @@ def _allclose_scalars(a_v, b_v, rtol=1e-05, atol=1e-08, equal_nan=False):
def np_allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):

if not type_can_asarray(a):
raise TypeError('The first argument "a" must be array-like')
raise TypingError('The first argument "a" must be array-like')

if not type_can_asarray(b):
raise TypeError('The second argument "b" must be array-like')
raise TypingError('The second argument "b" must be array-like')

if not isinstance(rtol, types.Float):
raise TypeError('The third argument "rtol" must be a '
'floating point')
if not isinstance(rtol, (float, types.Float)):
raise TypingError('The third argument "rtol" must be a '
'floating point')

if not isinstance(atol, types.Float):
if not isinstance(atol, (float, types.Float)):
raise TypingError('The fourth argument "atol" must be a '
'floating point')

if not isinstance(equal_nan, types.Boolean):
raise TypeError('The fifth argument "equal_nan" must be a '
'boolean')
if not isinstance(equal_nan, (bool, types.Boolean)):
raise TypingError('The fifth argument "equal_nan" must be a '
'boolean')

is_a_scalar = isinstance(a, types.Number)
is_b_scalar = isinstance(b, types.Number)
Expand Down
32 changes: 25 additions & 7 deletions numba/tests/test_np_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3760,14 +3760,32 @@ def test_allclose_exception(self):
pyfunc = np_allclose
cfunc = jit(nopython=True)(pyfunc)

a = np.asarray([1e10, 1e-9, np.nan])
b = np.asarray([1.0001e10, 1e-9])

with self.assertRaises(ValueError) as e:
cfunc(a, b)
inps = [
(np.asarray([1e10, 1e-9, np.nan]),
np.asarray([1.0001e10, 1e-9]),
1e-05, 1e-08, False,
"shape mismatch: objects cannot be broadcast to a single shape",
ValueError),
('hello', 3, False, 1e-08, False,
'The first argument "a" must be array-like',
TypingError),
(3, 'hello', False, 1e-08, False,
'The second argument "b" must be array-like',
TypingError),
(2, 3, False, 1e-08, False,
'The third argument "rtol" must be a floating point',
TypingError),
(2, 3, 1e-05, False, False,
'The fourth argument "atol" must be a floating point',
TypingError),
(2, 3, 1e-05, 1e-08, 1,
'The fifth argument "equal_nan" must be a boolean',
TypingError),
]

self.assertIn(("shape mismatch: objects cannot be broadcast to "
"a single shape"), str(e.exception))
for a, b, rtol, atol, equal_nan, exc_msg, exc in inps:
with self.assertRaisesRegex(exc, exc_msg):
cfunc(a, b, rtol, atol, equal_nan)

def test_interp_basic(self):
pyfunc = interp
Expand Down

0 comments on commit 7cd3e30

Please sign in to comment.