def _get_named_dims(
    conditions: Sequence[Condition], args_dict: Dict[Text, Any],
    context: _Context) -> Dict[Text, Union[int, tf.Tensor]]:
  """Resolves `NamedDim`s to actual dimension (static or dynamic)."""

  named_dims: Dict[Text, Union[int, tf.Tensor]] = dict()
  for condition in conditions:
    if not isinstance(condition, NamedDim):
      continue

    # TODO(jhclark): Check bounds for `dim` and give good error message.
    if condition.tensor == RESULT:
      raise ValueError(
          "Analyzing function `{}`: `NamedDim` does not allow `{}` "
          "to be a parameter name. Please acquire the dimension from an "
          "input instead.".format(context.func_name, RESULT))
    if condition.dim is not None:
      if condition.var is not None:
        var_name = condition.var_name
        tensor = condition.var  # Get `var` from `Dynamic` contract.
      else:
        var_name = condition.tensor
        tensor = _get_tensor_arg(
            args_dict,
            condition.tensor,
            tuple_index=condition.tuple_index,
            context=context)
      tensor = _to_tensor(tensor)
      if not _is_tensor(tensor):
        raise ValueError(
            "`{}`: Error while attempting to get `NamedDim` for `{}`: {}"
            "Not a tensor: ".format(context.func_name, var_name, type(tensor)))
      dim: Union[int, tf.Tensor] = _get_tensor_dim(
          tensor, dim=condition.dim, context=context)
    else:
      assert condition.value_of is not None
      dim = _get_arg(args_dict, tensor_name=condition.value_of, context=context)
      if not isinstance(dim, tf.Tensor) and not isinstance(
          dim, int) and not _is_scalar(dim):
        raise ValueError("Expected target of `NamedDim`'s `value_of`='{}'"
                         "to be a scalar, but got {}".format(
                             condition.value_of, dim.get_shape()))
    named_dims[condition.dim_name] = dim
  return named_dims
Resolves `NamedDim`s to actual dimension (static or dynamic).
language/canine/tensor_contracts.py