message: Short-circuit np.dim evaluation for list and tuple.
diff: @@ -155,8 +155,8 @@ def _constant_value(ragged_factory, inner_factory, pylist, dtype, ragged_rank,
  """
  if ragged_tensor.is_ragged(pylist):
  raise TypeError("pylist may not be a RaggedTensor or RaggedTensorValue.")
  -
  - if np.ndim(pylist) == 0:
  + # np.ndim builds an array, so we short-circuit lists and tuples.
  + if not isinstance(pylist, (list, tuple)) and np.ndim(pylist) == 0:
  # Scalar value
  if ragged_rank is not None and ragged_rank != 0:
  raise ValueError("Invalid pylist=%r: incompatible with ragged_rank=%d" %
  @@ -243,7 +243,9 @@ def _find_scalar_and_max_depth(pylist):
  Raises:
  ValueError: If pylist has inconsistent nesting depths for scalars.
  """
  - if np.ndim(pylist) != 0: # Check if pylist is not scalar
  + # Check if pylist is not scalar. np.ndim builds an array, so we
  + # short-circuit lists and tuples.
  + if isinstance(pylist, (list, tuple)) or np.ndim(pylist) != 0:
  scalar_depth = None
  max_depth = 1
  for child in pylist:
  @@ -262,7 +264,7 @@ def _default_inner_shape_for_pylist(pylist, ragged_rank):
  def get_inner_shape(item):
  """Returns the inner shape for a python list `item`."""
  - if np.ndim(item) == 0:
  + if not isinstance(item, (list, tuple)) and np.ndim(item) == 0:
  return ()
  elif item:
  return (len(item),) + get_inner_shape(item[0])
  @@ -270,7 +272,7 @@ def _default_inner_shape_for_pylist(pylist, ragged_rank):
  def check_inner_shape(item, shape):
  """Checks that `item` has a consistent shape matching `shape`."""
  - is_nested = np.ndim(item) != 0
  + is_nested = isinstance(item, (list, tuple)) or np.ndim(item) != 0
  if is_nested != bool(shape):
  raise ValueError("inner values have inconsistent shape")
  if is_nested:
  @@ -282,7 +284,8 @@ def _default_inner_shape_for_pylist(pylist, ragged_rank):
  # Collapse the ragged layers to get the list of inner values.
  flat_values = pylist
  for dim in range(ragged_rank):
  - if not all(np.ndim(v) != 0 for v in flat_values):
  + if not all(isinstance(v, (list, tuple)) or np.ndim(v) != 0
  + for v in flat_values):
  raise ValueError("pylist has scalar values depth %d, but ragged_rank=%d "
  "requires scalar value depth greater than %d" %
  (dim + 1, ragged_rank, ragged_rank))
  