MCPcopy Index your code
hub / github.com/numpy/numpy / test_iter_broadcasting_errors

Function test_iter_broadcasting_errors

numpy/_core/tests/test_nditer.py:768–841  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

766 i = np.nditer([np.ones(2), None, None], itershape=(2,))
767
768def test_iter_broadcasting_errors():
769 # Check that errors are thrown for bad broadcasting shapes
770
771 # 1D with 1D
772 assert_raises(ValueError, nditer, [arange(2), arange(3)],
773 [], [['readonly']] * 2)
774 # 2D with 1D
775 assert_raises(ValueError, nditer,
776 [arange(6).reshape(2, 3), arange(2)],
777 [], [['readonly']] * 2)
778 # 2D with 2D
779 assert_raises(ValueError, nditer,
780 [arange(6).reshape(2, 3), arange(9).reshape(3, 3)],
781 [], [['readonly']] * 2)
782 assert_raises(ValueError, nditer,
783 [arange(6).reshape(2, 3), arange(4).reshape(2, 2)],
784 [], [['readonly']] * 2)
785 # 3D with 3D
786 assert_raises(ValueError, nditer,
787 [arange(36).reshape(3, 3, 4), arange(24).reshape(2, 3, 4)],
788 [], [['readonly']] * 2)
789 assert_raises(ValueError, nditer,
790 [arange(8).reshape(2, 4, 1), arange(24).reshape(2, 3, 4)],
791 [], [['readonly']] * 2)
792
793 # Verify that the error message mentions the right shapes
794 try:
795 nditer([arange(2).reshape(1, 2, 1),
796 arange(3).reshape(1, 3),
797 arange(6).reshape(2, 3)],
798 [],
799 [['readonly'], ['readonly'], ['writeonly', 'no_broadcast']])
800 raise AssertionError('Should have raised a broadcast error')
801 except ValueError as e:
802 msg = str(e)
803 # The message should contain the shape of the 3rd operand
804 assert_(msg.find('(2,3)') >= 0,
805 f'Message "{msg}" doesn\'t contain operand shape (2,3)')
806 # The message should contain the broadcast shape
807 assert_(msg.find('(1,2,3)') >= 0,
808 f'Message "{msg}" doesn\'t contain broadcast shape (1,2,3)')
809
810 try:
811 nditer([arange(6).reshape(2, 3), arange(2)],
812 [],
813 [['readonly'], ['readonly']],
814 op_axes=[[0, 1], [0, np.newaxis]],
815 itershape=(4, 3))
816 raise AssertionError('Should have raised a broadcast error')
817 except ValueError as e:
818 msg = str(e)
819 # The message should contain "shape->remappedshape" for each operand
820 assert_(msg.find('(2,3)->(2,3)') >= 0,
821 f'Message "{msg}" doesn\'t contain operand shape (2,3)->(2,3)')
822 assert_(msg.find('(2,)->(2,newaxis)') >= 0,
823 f'Message "{msg}" doesn\'t contain remapped operand shape'
824 '(2,)->(2,newaxis)')
825 # The message should contain the itershape parameter

Callers

nothing calls this directly

Calls 4

assert_raisesFunction · 0.90
assert_Function · 0.90
reshapeMethod · 0.80
findMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…