def decorator(func):
    """Invoked by Python when `@contract` is used as a function decorator."""

    def check_preconditions_and_run_func(
        *args: Any, **kwargs: Dict[Text, Any]) -> Any:
      """Invoked by Python when `@contract` is used as a function decorator."""
      # If contracts are disable, just call the function normally without any
      # intervention.
      if _all_disabled:
        return func(*args, **kwargs)

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

      args_dict: Dict[Text, Any] = _get_func_args(func, args, kwargs)
      if RESULT in args_dict:
        raise ValueError(
            "Analyzing function `{}` at {}:{}: tf_contracts does not allow "
            "`{}` to be a parameter name.".format(pretty_func_name, caller_file,
                                                  caller_line, RESULT))

      # Generate any extra `Dynamic` conditions.
      expanded_conditions = _expand_dynamic_conditions(
          conditions=conditions, args_dict=args_dict, context=context)
      # IMPORTANT: Use only `expanded_conditions` hereafter. Unfortunately,
      # `conditions` apparently can't be `del`ed due to *args and/or capturing.

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

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

      precondition_ops = _check_preconditions(
          precondition_specs,
          args_dict=args_dict,
          named_dims=named_dims,
          context=context)
      if _add_function_variable_scopes:
        with tf.variable_scope(func.__name__):
          with tf.control_dependencies(precondition_ops):
            result = func(*args, **kwargs)
      else:
        with tf.control_dependencies(precondition_ops):
          result = func(*args, **kwargs)

      result = _check_postconditions(
          postcondition_specs,
          args_dict=args_dict,
          named_dims=named_dims,
          context=context,
          result=result)
      return result
    return check_preconditions_and_run_func
Invoked by Python when `@contract` is used as a function decorator.
language/canine/tensor_contracts.py