Home | History | Annotate | Download | only in util
      1 # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 # ==============================================================================
     15 
     16 """Keyword args functions."""
     17 
     18 from __future__ import absolute_import
     19 from __future__ import division
     20 from __future__ import print_function
     21 
     22 import functools
     23 
     24 from tensorflow.python.util import decorator_utils
     25 
     26 
     27 def keyword_args_only(func):
     28   """Decorator for marking specific function accepting keyword args only.
     29 
     30   This decorator raises a `ValueError` if the input `func` is called with any
     31   non-keyword args. This prevents the caller from providing the arguments in
     32   wrong order.
     33 
     34   Args:
     35     func: The function or method needed to be decorated.
     36 
     37   Returns:
     38     Decorated function or method.
     39 
     40   Raises:
     41     ValueError: If `func` is not callable.
     42   """
     43 
     44   decorator_utils.validate_callable(func, "keyword_args_only")
     45   @functools.wraps(func)
     46   def new_func(*args, **kwargs):
     47     """Keyword args only wrapper."""
     48     if args:
     49       raise ValueError(
     50           "Must use keyword args to call {}.".format(func.__name__))
     51     return func(**kwargs)
     52   return new_func
     53