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

Method test_squeeze_axis_handling

numpy/core/tests/test_regression.py:1725–1784  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1723 assert_(b.flags.f_contiguous)
1724
1725 def test_squeeze_axis_handling(self):
1726 # Issue #10779
1727 # Ensure proper handling of objects
1728 # that don't support axis specification
1729 # when squeezing
1730
1731 class OldSqueeze(np.ndarray):
1732
1733 def __new__(cls,
1734 input_array):
1735 obj = np.asarray(input_array).view(cls)
1736 return obj
1737
1738 # it is perfectly reasonable that prior
1739 # to numpy version 1.7.0 a subclass of ndarray
1740 # might have been created that did not expect
1741 # squeeze to have an axis argument
1742 # NOTE: this example is somewhat artificial;
1743 # it is designed to simulate an old API
1744 # expectation to guard against regression
1745 def squeeze(self):
1746 return super().squeeze()
1747
1748 oldsqueeze = OldSqueeze(np.array([[1],[2],[3]]))
1749
1750 # if no axis argument is specified the old API
1751 # expectation should give the correct result
1752 assert_equal(np.squeeze(oldsqueeze),
1753 np.array([1,2,3]))
1754
1755 # likewise, axis=None should work perfectly well
1756 # with the old API expectation
1757 assert_equal(np.squeeze(oldsqueeze, axis=None),
1758 np.array([1,2,3]))
1759
1760 # however, specification of any particular axis
1761 # should raise a TypeError in the context of the
1762 # old API specification, even when using a valid
1763 # axis specification like 1 for this array
1764 with assert_raises(TypeError):
1765 # this would silently succeed for array
1766 # subclasses / objects that did not support
1767 # squeeze axis argument handling before fixing
1768 # Issue #10779
1769 np.squeeze(oldsqueeze, axis=1)
1770
1771 # check for the same behavior when using an invalid
1772 # axis specification -- in this case axis=0 does not
1773 # have size 1, but the priority should be to raise
1774 # a TypeError for the axis argument and NOT a
1775 # ValueError for squeezing a non-empty dimension
1776 with assert_raises(TypeError):
1777 np.squeeze(oldsqueeze, axis=0)
1778
1779 # the new API knows how to handle the axis
1780 # argument and will return a ValueError if
1781 # attempting to squeeze an axis that is not
1782 # of length 1

Callers

nothing calls this directly

Calls 4

assert_equalFunction · 0.90
assert_raisesFunction · 0.90
OldSqueezeClass · 0.85
squeezeMethod · 0.45

Tested by

no test coverage detected