MCPcopy Create free account
hub / github.com/numpy/numpy / test_to_int_scalar

Method test_to_int_scalar

numpy/core/tests/test_multiarray.py:8767–8802  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

8765 self_containing[0] = None # resolve circular reference
8766
8767 def test_to_int_scalar(self):
8768 # gh-9972 means that these aren't always the same
8769 int_funcs = (int, lambda x: x.__int__())
8770 for int_func in int_funcs:
8771 assert_equal(int_func(np.array(0)), 0)
8772 with assert_warns(DeprecationWarning):
8773 assert_equal(int_func(np.array([1])), 1)
8774 with assert_warns(DeprecationWarning):
8775 assert_equal(int_func(np.array([[42]])), 42)
8776 assert_raises(TypeError, int_func, np.array([1, 2]))
8777
8778 # gh-9972
8779 assert_equal(4, int_func(np.array('4')))
8780 assert_equal(5, int_func(np.bytes_(b'5')))
8781 assert_equal(6, int_func(np.str_('6')))
8782
8783 # The delegation of int() to __trunc__ was deprecated in
8784 # Python 3.11.
8785 if sys.version_info < (3, 11):
8786 class HasTrunc:
8787 def __trunc__(self):
8788 return 3
8789 assert_equal(3, int_func(np.array(HasTrunc())))
8790 with assert_warns(DeprecationWarning):
8791 assert_equal(3, int_func(np.array([HasTrunc()])))
8792 else:
8793 pass
8794
8795 class NotConvertible:
8796 def __int__(self):
8797 raise NotImplementedError
8798 assert_raises(NotImplementedError,
8799 int_func, np.array(NotConvertible()))
8800 with assert_warns(DeprecationWarning):
8801 assert_raises(NotImplementedError,
8802 int_func, np.array([NotConvertible()]))
8803
8804
8805class TestWhere:

Callers

nothing calls this directly

Calls 7

assert_equalFunction · 0.90
assert_warnsFunction · 0.90
assert_raisesFunction · 0.90
int_funcFunction · 0.85
HasTruncClass · 0.85
NotConvertibleClass · 0.85
__int__Method · 0.45

Tested by

no test coverage detected