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

Method test_nested_arraylikes

numpy/core/tests/test_array_coercion.py:517–540  ·  view source on GitHub ↗
(self, arraylike)

Source from the content-addressed store, hash-verified

515
516 @pytest.mark.parametrize("arraylike", arraylikes())
517 def test_nested_arraylikes(self, arraylike):
518 # We try storing an array like into an array, but the array-like
519 # will have too many dimensions. This means the shape discovery
520 # decides that the array-like must be treated as an object (a special
521 # case of ragged discovery). The result will be an array with one
522 # dimension less than the maximum dimensions, and the array being
523 # assigned to it (which does work for object or if `float(arraylike)`
524 # works).
525 initial = arraylike(np.ones((1, 1)))
526
527 nested = initial
528 for i in range(np.MAXDIMS - 1):
529 nested = [nested]
530
531 with pytest.raises(ValueError, match=".*would exceed the maximum"):
532 # It will refuse to assign the array into
533 np.array(nested, dtype="float64")
534
535 # If this is object, we end up assigning a (1, 1) array into (1,)
536 # (due to running out of dimensions), this is currently supported but
537 # a special case which is not ideal.
538 arr = np.array(nested, dtype=object)
539 assert arr.shape == (1,) * np.MAXDIMS
540 assert arr.item() == np.array(initial).item()
541
542 @pytest.mark.parametrize("arraylike", arraylikes())
543 def test_uneven_depth_ragged(self, arraylike):

Callers

nothing calls this directly

Calls 1

itemMethod · 0.80

Tested by

no test coverage detected