diff options
author | 2016-11-01 10:41:24 -0800 | |
---|---|---|
committer | 2016-11-01 11:55:11 -0700 | |
commit | 6bd7f4d7d7866db1d1406266f25d656bc5f54364 (patch) | |
tree | 6f5ad175f77d3067ce1fb547a17a4abf78f84794 /tensorflow/python/util/decorator_utils.py | |
parent | cee57eb0e21a66532a5322aafa372147f6f90feb (diff) |
Move deprecation module into core framework from contrib/framework
Change: 137850119
Diffstat (limited to 'tensorflow/python/util/decorator_utils.py')
-rw-r--r-- | tensorflow/python/util/decorator_utils.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/tensorflow/python/util/decorator_utils.py b/tensorflow/python/util/decorator_utils.py new file mode 100644 index 0000000000..155003498c --- /dev/null +++ b/tensorflow/python/util/decorator_utils.py @@ -0,0 +1,62 @@ +# Copyright 2016 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== + +"""Utility functions for writing decorators (which modify docstrings).""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + + +def get_qualified_name(function): + # Python 3 + if hasattr(function, '__qualname__'): + return function.__qualname__ + + # Python 2 + if hasattr(function, 'im_class'): + return function.im_class.__name__ + '.' + function.__name__ + return function.__name__ + + +def add_notice_to_docstring( + doc, instructions, no_doc_str, suffix_str, notice): + """Adds a deprecation notice to a docstring.""" + if not doc: + lines = [no_doc_str] + else: + lines = doc.splitlines() + lines[0] += ' ' + suffix_str + + notice = [''] + notice + [instructions] + + if len(lines) > 1: + # Make sure that we keep our distance from the main body + if lines[1].strip(): + notice.append('') + + lines[1:1] = notice + else: + lines += notice + + return '\n'.join(lines) + + +def validate_callable(func, decorator_name): + if not hasattr(func, '__call__'): + raise ValueError( + '%s is not a function. If this is a property, make sure' + ' @property appears before @%s in your source code:' + '\n\n@property\n@%s\ndef method(...)' % ( + func, decorator_name, decorator_name)) |