Skip to content

Commit cb91aa7

Browse files
committed
TST: Cover some more internal array-function code paths
1 parent c3b96e4 commit cb91aa7

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

numpy/core/tests/test_overrides.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,56 @@ def func():
394394
except TypeError as exc:
395395
assert exc is error # unmodified exception
396396

397+
def test_properties(self):
398+
# Check that str and repr are sensible
399+
func = dispatched_two_arg
400+
assert str(func) == str(func._implementation)
401+
repr_no_id = repr(func).split("at ")[0]
402+
repr_no_id_impl = repr(func._implementation).split("at ")[0]
403+
assert repr_no_id == repr_no_id_impl
404+
405+
@pytest.mark.parametrize("func", [
406+
lambda x, y: 0, # no like argument
407+
lambda like=None: 0, # not keyword only
408+
lambda *, like=None, a=3: 0, # not last (not that it matters)
409+
])
410+
def test_bad_like_sig(self, func):
411+
# We sanity check the signature, and these should fail.
412+
with pytest.raises(RuntimeError):
413+
array_function_dispatch()(func)
414+
415+
def test_bad_like_passing(self):
416+
# Cover internal sanity check for passing like as first positional arg
417+
def func(*, like=None):
418+
pass
419+
420+
func_with_like = array_function_dispatch()(func)
421+
with pytest.raises(TypeError):
422+
func_with_like()
423+
with pytest.raises(TypeError):
424+
func_with_like(like=234)
425+
426+
def test_too_many_args(self):
427+
# Mainly a unit-test to increase coverage
428+
objs = []
429+
for i in range(40):
430+
class MyArr:
431+
def __array_function__(self, *args, **kwargs):
432+
return NotImplemented
433+
434+
objs.append(MyArr())
435+
436+
def _dispatch(*args):
437+
return args
438+
439+
@array_function_dispatch(_dispatch)
440+
def func(*args):
441+
pass
442+
443+
with pytest.raises(TypeError, match="maximum number"):
444+
func(*objs)
445+
446+
397447

398448
class TestNDArrayMethods:
399449

0 commit comments

Comments
 (0)