def local_invariant(*conditions: Condition) -> ContextManager[None]:
  """A variant of a contract that can be specified inline via a `with` block."""
  if _all_disabled:
    return _noop_context_manager()

  caller_name, caller_file, caller_line = _get_function_and_line(
      inspect.stack())
  pretty_func_name = "{}.local_invariant.{}".format(caller_name, caller_line)
  context = _Context(
      func_name=pretty_func_name,
      caller_name=caller_name,
      caller_file=caller_file,
      caller_line=caller_line)

  # We'll treat the local variables defined within the calling context
  # as the 'args' here.
  args_dict: Dict[Text, Any] = _get_caller_locals(inspect.stack())
  if RESULT in args_dict:
    raise ValueError(
        "Analyzing function `{}` at {}:{}: tf_contracts does not allow "
        "`{}` to be a local variable.".format(pretty_func_name, caller_file,
                                              caller_line, RESULT))

  named_dims = _get_named_dims(
      conditions=conditions, args_dict=args_dict, context=context)

  precondition_specs: List[Union[Require, RequireTrue]] = list()
  for condition in conditions:
    if isinstance(condition, NamedDim):
      pass
    # `Ensure` / `EnsureTrue` must be handled first since they're subclasses
    # of `Require` / `RequireTrue`.
    elif isinstance(condition, (Ensure, EnsureTrue)):
      raise ValueError(
          "Postconditions not allowed in `local_invariant`: {}".format(
              type(condition)))
    elif isinstance(condition, (Require, RequireTrue)):
      precondition_specs.append(condition)
    else:
      raise ValueError(
          "`Condition` subclass not supported in `local_invariant`: {}".format(
              type(condition)))

  precondition_ops = _check_preconditions(
      precondition_specs,
      args_dict=args_dict,
      named_dims=named_dims,
      context=context)

  # This will be used in the resulting `with` block.
  return tf.control_dependencies(precondition_ops)
A variant of a contract that can be specified inline via a `with` block.
language/canine/tensor_contracts.py