This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author zzzeek
Recipients larry, ncoghlan, yselivanov, zzzeek
Date 2014-03-02.18:07:39
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1393783660.25.0.499364821276.issue20828@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2014-03-02 18:07:40zzzeeksetrecipients: + zzzeek, ncoghlan, larry, yselivanov
2014-03-02 18:07:40zzzeeksetmessageid: <1393783660.25.0.499364821276.issue20828@psf.upfronthosting.co.za>
2014-03-02 18:07:40zzzeeklinkissue20828 messages
2014-03-02 18:07:39zzzeekcreate