aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/util
diff options
context:
space:
mode:
authorGravatar IMBurbank <bassmanburbank@gmail.com>2018-09-27 22:21:47 -0600
committerGravatar IMBurbank <bassmanburbank@gmail.com>2018-09-27 22:21:47 -0600
commitefe17306442aa91192df953ae537d3f9b824dae6 (patch)
treecd36fdea64c4927bb9e868abdda2f0e08d21e630 /tensorflow/python/util
parent5bbcdb8a58efd97b0f73927218d5896da67f5203 (diff)
Updated python3 tf_inspect.getargspec calls to use getfullargspec and repackage the return values into the getargspec struct.
Diffstat (limited to 'tensorflow/python/util')
-rw-r--r--tensorflow/python/util/tf_contextlib_test.py2
-rw-r--r--tensorflow/python/util/tf_inspect.py89
2 files changed, 56 insertions, 35 deletions
diff --git a/tensorflow/python/util/tf_contextlib_test.py b/tensorflow/python/util/tf_contextlib_test.py
index 1e921b5ea3..4a5bf388a6 100644
--- a/tensorflow/python/util/tf_contextlib_test.py
+++ b/tensorflow/python/util/tf_contextlib_test.py
@@ -83,7 +83,7 @@ class TfContextlibTest(test.TestCase):
self.assertFalse(isinstance(target, tf_decorator.TFDecorator))
def testGetArgSpecReturnsWrappedArgSpec(self):
- argspec = tf_inspect.getfullargspec(test_params_and_defaults)
+ argspec = tf_inspect.getargspec(test_params_and_defaults)
self.assertEqual(['a', 'b', 'c', 'd'], argspec.args)
self.assertEqual((2, True, 'hello'), argspec.defaults)
diff --git a/tensorflow/python/util/tf_inspect.py b/tensorflow/python/util/tf_inspect.py
index 234850ac3f..3cd6c515b9 100644
--- a/tensorflow/python/util/tf_inspect.py
+++ b/tensorflow/python/util/tf_inspect.py
@@ -36,6 +36,53 @@ else:
'annotations'
])
+if hasattr(_inspect, 'getfullargspec'):
+ _getfullargspec = _inspect.getfullargspec # pylint: disable=invalid-name
+
+ def _getargspec(target):
+ """A python3 version of getargspec.
+
+ Calls `getfullargspec` and assigns args, varargs,
+ varkw, and defaults to a python 2/3 compatible `ArgSpec`.
+
+ The parameter name 'varkw' is changed to 'keywords' to fit the
+ `ArgSpec` struct.
+
+ Args:
+ target: the target object to inspect.
+ Returns:
+ An ArgSpec with args, varargs, keywords, and defaults parameters
+ from FullArgSpec.
+ """
+ fullargspecs = getfullargspec(target)
+ argspecs = ArgSpec(
+ args=fullargspecs.args,
+ varargs=fullargspecs.varargs,
+ keywords=fullargspecs.varkw,
+ defaults=fullargspecs.defaults)
+ return argspecs
+else:
+ _getargspec = _inspect.getargspec
+
+ def _getfullargspec(target):
+ """A python2 version of getfullargspec.
+
+ Args:
+ target: the target object to inspect.
+ Returns:
+ A FullArgSpec with empty kwonlyargs, kwonlydefaults and annotations.
+ """
+ argspecs = getargspec(target)
+ fullargspecs = FullArgSpec(
+ args=argspecs.args,
+ varargs=argspecs.varargs,
+ varkw=argspecs.keywords,
+ defaults=argspecs.defaults,
+ kwonlyargs=[],
+ kwonlydefaults=None,
+ annotations={})
+ return fullargspecs
+
def currentframe():
"""TFDecorator-aware replacement for inspect.currentframe."""
@@ -45,10 +92,8 @@ def currentframe():
def getargspec(obj):
"""TFDecorator-aware replacement for `inspect.getargspec`.
- This should not be called from other modules. It is deprecated in python3.
-
- Use `getfullargspec`. It is a TFDecorator-aware replacement for
- `inspect.getfullargspec` compatible with both python2 and python3.
+ Note: `getfullargspec` is recommended as the python 2/3 compatible
+ replacement for this function.
Args:
obj: A function, partial function, or callable object, possibly
@@ -56,8 +101,8 @@ def getargspec(obj):
Returns:
The `ArgSpec` that describes the signature of the outermost decorator that
- changes the callable's signature. If the callable is not decorated,
- `inspect.getargspec()` will be called directly on the object.
+ changes the callable's signature, or the `ArgSpec` that describes
+ the object if not decorated.
Raises:
ValueError: When callable's signature can not be expressed with
@@ -77,24 +122,24 @@ def getargspec(obj):
try:
# Python3 will handle most callables here (not partial).
- return _inspect.getargspec(target)
+ return _getargspec(target)
except TypeError:
pass
if isinstance(target, type):
try:
- return _inspect.getargspec(target.__init__)
+ return _getargspec(target.__init__)
except TypeError:
pass
try:
- return _inspect.getargspec(target.__new__)
+ return _getargspec(target.__new__)
except TypeError:
pass
# The `type(target)` ensures that if a class is received we don't return
# the signature of it's __call__ method.
- return _inspect.getargspec(type(target).__call__)
+ return _getargspec(type(target).__call__)
def _get_argspec_for_partial(obj):
@@ -177,30 +222,6 @@ def _get_argspec_for_partial(obj):
return ArgSpec(args, varargs, keywords, tuple(all_defaults[first_default:]))
-if hasattr(_inspect, 'getfullargspec'):
- _getfullargspec = _inspect.getfullargspec
-else:
-
- def _getfullargspec(target):
- """A python2 version of getfullargspec.
-
- Args:
- target: the target object to inspect.
- Returns:
- A FullArgSpec with empty kwonlyargs, kwonlydefaults and annotations.
- """
- argspecs = getargspec(target)
- fullargspecs = FullArgSpec(
- args=argspecs.args,
- varargs=argspecs.varargs,
- varkw=argspecs.keywords,
- defaults=argspecs.defaults,
- kwonlyargs=[],
- kwonlydefaults=None,
- annotations={})
- return fullargspecs
-
-
def getfullargspec(obj):
"""TFDecorator-aware replacement for `inspect.getfullargspec`.