Properly handle callable objects.
authorA. Unique TensorFlower <gardener@tensorflow.org>
Fri, 6 Apr 2018 18:17:41 +0000 (11:17 -0700)
committerTensorFlower Gardener <gardener@tensorflow.org>
Fri, 6 Apr 2018 18:21:38 +0000 (11:21 -0700)
PiperOrigin-RevId: 191913834

tensorflow/contrib/autograph/pyct/inspect_utils.py
tensorflow/contrib/autograph/pyct/inspect_utils_test.py

index d19c6ed..30a5961 100644 (file)
@@ -74,6 +74,12 @@ def getmethodclass(m):
     ValueError: if the class could not be resolved for any unexpected reason.
   """
 
+  # Callable objects: return their own class.
+  if (not hasattr(m, '__name__') and hasattr(m, '__class__') and
+      hasattr(m, '__call__')):
+    if isinstance(m.__class__, six.class_types):
+      return m.__class__
+
   # Instance method and class methods: should be bound to a non-null "self".
   # If self is a class, then it's a class method.
   if hasattr(m, '__self__'):
index ddca6f9..eda3fc1 100644 (file)
@@ -225,6 +225,15 @@ class InspectUtilsTest(test.TestCase):
         inspect_utils.getmethodclass(test_obj.wrap_decorated_member),
         LocalClass)
 
+  def test_getmethodclass_callables(self):
+    class TestCallable(object):
+
+      def __call__(self):
+        pass
+
+    c = TestCallable()
+    self.assertEqual(inspect_utils.getmethodclass(c), TestCallable)
+
 
 if __name__ == '__main__':
   test.main()