def __init__(self,
               dim_name: Text,
               tensor: Optional[Text] = None,
               dim: Optional[int] = None,
               tuple_index: Optional[int] = None,
               value_of: Optional[Text] = None,
               var: Optional[tf.Tensor] = None,
               var_name: Optional[Text] = None):
    """Creates a `NamedDim`.

    Caller must specify either (`tensor` and `dim`) OR `value_of`.

    Args:
      dim_name: The name of the dimension, which will be usable elsewhere.
      tensor: The name of the function argument (a tensor) that defines the
        dimension.
      dim: The index of the desired tensor dimension.
      tuple_index: Index of the desired tensor inside `tensor` iff `tensor` is
        actually a tuple return type.
      value_of: The name of a function argument whose type is an `int` or
        scalar `tf.Tensor`; this value will define this dimension.
      var: For use in `tc.Dynamic` conditions: The actual variable instance to
        use as a `tensor`. (In normal conditions, `tensor` is a string that
        names the function argument to use).
      var_name: For use when `var` is specified: The name of the variable being
        passed to `var`, which will be used to provide meaningful error
        messages.
    """
    self.dim_name = dim_name
    self.tensor = tensor
    self.dim = dim
    self.tuple_index = tuple_index
    self.var = var
    self.var_name = var_name
    self.value_of = value_of

    # PyType doesn't always catch this, which can lead to very confusing errors
    # given that this is the first argument.
    if not isinstance(dim_name, str):
      raise ValueError("Expected `dim_name` to be a string but got: {}".format(
          type(dim_name)))

    if var is not None:
      if not var_name:
        raise ValueError(
            "Expected `var_name` to be specified when `var` is specified.")
      if tensor is not None:
        raise ValueError(
            "Expected `tensor` to be `None` because `var` is specified.")
      if dim is None:
        raise ValueError(
            "Expected `dim` to be specified because `var` is specified.")
      return

    valid_tensor_and_dim = (tensor and dim is not None)
    valid_value_of = value_of is not None
    specified_both = (tensor or dim is not None) and value_of is not None
    if (not valid_tensor_and_dim and not valid_value_of) or specified_both:
      raise ValueError(
          "Either (`tensor` and `dim`) or (`value_of`) must be "
          "specified when defining `dim_name`='{}' in `NamedDim`.".format(
              dim_name))
Creates a `NamedDim`.

    Caller must specify either (`tensor` and `dim`) OR `value_of`.

    Args:
      dim_name: The name of the dimension, which will be usable elsewhere.
      tensor: The name of the function argument (a tensor) that defines the
        dimension.
      dim: The index of the desired tensor dimension.
      tuple_index: Index of the desired tensor inside `tensor` iff `tensor` is
        actually a tuple return type.
      value_of: The name of a function argument whose type is an `int` or
        scalar `tf.Tensor`; this value will define this dimension.
      var: For use in `tc.Dynamic` conditions: The actual variable instance to
        use as a `tensor`. (In normal conditions, `tensor` is a string that
        names the function argument to use).
      var_name: For use when `var` is specified: The name of the variable being
        passed to `var`, which will be used to provide meaningful error
        messages.
    
language/canine/tensor_contracts.py