##// END OF EJS Templates
Don't introspect __call__ for simple callables...
Thomas Kluyver -
Show More
@@ -48,6 +48,9 b' _object_init_docstring = object.__init__.__doc__'
48 _builtin_type_docstrings = {
48 _builtin_type_docstrings = {
49 t.__doc__ for t in (types.ModuleType, types.MethodType, types.FunctionType)
49 t.__doc__ for t in (types.ModuleType, types.MethodType, types.FunctionType)
50 }
50 }
51
52 _builtin_func_type = type(all)
53 _builtin_meth_type = type(str.upper) # Bound methods have the same type as builtin functions
51 #****************************************************************************
54 #****************************************************************************
52 # Builtin color schemes
55 # Builtin color schemes
53
56
@@ -180,6 +183,13 b' def getsource(obj,is_binary=False):'
180 encoding = get_encoding(obj)
183 encoding = get_encoding(obj)
181 return cast_unicode(src, encoding=encoding)
184 return cast_unicode(src, encoding=encoding)
182
185
186
187 def is_simple_callable(obj):
188 """True if obj is a function ()"""
189 return (inspect.isfunction(obj) or inspect.ismethod(obj) or \
190 isinstance(obj, _builtin_func_type) or isinstance(obj, _builtin_meth_type))
191
192
183 def getargspec(obj):
193 def getargspec(obj):
184 """Wrapper around :func:`inspect.getfullargspec` on Python 3, and
194 """Wrapper around :func:`inspect.getfullargspec` on Python 3, and
185 :func:inspect.getargspec` on Python 2.
195 :func:inspect.getargspec` on Python 2.
@@ -187,8 +197,7 b' def getargspec(obj):'
187 In addition to functions and methods, this can also handle objects with a
197 In addition to functions and methods, this can also handle objects with a
188 ``__call__`` attribute.
198 ``__call__`` attribute.
189 """
199 """
190 if not (inspect.isfunction(obj) or inspect.ismethod(obj)) \
200 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
191 and safe_hasattr(obj, '__call__'):
192 obj = obj.__call__
201 obj = obj.__call__
193
202
194 return inspect.getfullargspec(obj) if PY3 else inspect.getargspec(obj)
203 return inspect.getfullargspec(obj) if PY3 else inspect.getargspec(obj)
@@ -767,7 +776,7 b' class Inspector:'
767 out['init_docstring'] = init_ds
776 out['init_docstring'] = init_ds
768
777
769 # Call form docstring for callable instances
778 # Call form docstring for callable instances
770 if safe_hasattr(obj, '__call__'):
779 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
771 call_def = self._getdef(obj.__call__, oname)
780 call_def = self._getdef(obj.__call__, oname)
772 if call_def is not None:
781 if call_def is not None:
773 out['call_def'] = self.format(call_def)
782 out['call_def'] = self.format(call_def)
@@ -115,6 +115,10 b' class Call(object):'
115 def method(self, x, z=2):
115 def method(self, x, z=2):
116 """Some method's docstring"""
116 """Some method's docstring"""
117
117
118 class SimpleClass(object):
119 def method(self, x, z=2):
120 """Some method's docstring"""
121
118
122
119 class OldStyle:
123 class OldStyle:
120 """An old-style class for testing."""
124 """An old-style class for testing."""
@@ -271,6 +275,13 b' def test_info_awkward():'
271 # Just test that this doesn't throw an error.
275 # Just test that this doesn't throw an error.
272 i = inspector.info(Awkward())
276 i = inspector.info(Awkward())
273
277
278 def test_calldef_none():
279 # We should ignore __call__ for all of these.
280 for obj in [f, SimpleClass().method, any, str.upper]:
281 print(obj)
282 i = inspector.info(obj)
283 nt.assert_is(i['call_def'], None)
284
274 if py3compat.PY3:
285 if py3compat.PY3:
275 exec("def f_kwarg(pos, *, kwonly): pass")
286 exec("def f_kwarg(pos, *, kwonly): pass")
276
287
General Comments 0
You need to be logged in to leave comments. Login now