[issue20828] inspect.getargspec() returns wrong answer with datetime.today.__call__()

mike bayer report at bugs.python.org
Sun Mar 2 19:07:40 CET 2014


mike bayer added the comment:

we basically need to be able to get the argument signature for anything that passes callable(); function, method, class, object with __call__.  So this is the logic we use:

import inspect

def get_callable_argspec(fn):
    if inspect.isfunction(fn) or inspect.ismethod(fn):
        inspectable = fn
    elif inspect.isclass(fn):
        inspectable = fn.__init__
    elif hasattr(fn, '__call__'):
        inspectable = fn.__call__
    else:
        inspectable = fn

    try:
        return inspect.getargspec(inspectable)
    except TypeError:
        raise

def callable1(self, x, y):
    pass

class SomeClass(object):
    def __init__(self, x, y):
        pass

    def callable2(self, x, y):
        pass

    def __call__(self, x, y):
        pass

callable3 = SomeClass
callable4 = SomeClass(2, 3)

for callable_ in (callable1, SomeClass(1, 2).callable2, callable3, callable4):
    assert callable(callable_)  # the interpreter can tell me this

    # how can it reliably tell me this? 
    assert get_callable_argspec(callable_) == (["self", "x", "y"], None, None, None)


If you pass a builtin like datetime.datetime.today to it, isfunction()/ismethod()/isclass() return false, but it does have a __call__().

I'm working around this now by just refusing to act on anything that is types.BuiltinMethodType or types.BuiltinFunctionType.   

Any guidance on what the proper way is to get the argument signature for any object that returns True for callable() would be very helpful (py2.6-3.x compatible).  I'm not sure if there's a stdlib call for this.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20828>
_______________________________________


More information about the Python-bugs-list mailing list