Title: Allow callable objects in inspect.getfullargspec
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.3
Status: closed Resolution: fixed
Dependencies: 17481 Superseder:
Assigned To: Nosy List: BreamoreBoy, benjamin.peterson, daniel.urban, eric.araujo, eric.snow, gsakkis, marco.mariani, maxbublis, michael.foord, ncoghlan, yselivanov
Priority: normal Keywords: patch

Created on 2010-05-06 21:58 by gsakkis, last changed 2014-01-29 20:55 by yselivanov. This issue is now closed.

File name Uploaded Description Edit
inspect.patch maxbublis, 2011-08-02 00:30 Adds support for callable objects in inspect.getfullargspec review
Messages (12)
msg105166 - (view) Author: George Sakkis (gsakkis) Date: 2010-05-06 21:58
Not sure if this has been brought before but how about extending  getargspec to work with callable instances, i.e. make it equivalent to getargspec(obj.__call__) ?
msg116518 - (view) Author: Marco Mariani (marco.mariani) Date: 2010-09-16 09:46
I second this, I depend on this monkeypatch for my turbogears projects, where I use callable objects as error handlers:

    def getargspec(func):
        if getattr(func, '__call__') and not isfunction(func) and not ismethod(func):
            func = func.__call__
        if ismethod(func):
            func = func.im_func
        if not isfunction(func):
            raise TypeError('arg is not a Python function')
        args, varargs, varkw = getargs(func.func_code)
        return args, varargs, varkw, func.func_defaults

but I suppose 2.7 is locked to this change so I propose it for 3.x
msg140069 - (view) Author: √Čric Araujo (eric.araujo) * (Python committer) Date: 2011-07-09 14:27
Adding to nosy the developers who last touched inspect.
msg140106 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2011-07-11 11:22
Doesn't seem like an unreasonable request. Nick / Benjamin, what do you think?
msg140111 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-07-11 12:00
This API has changed around a bit in 3.x, so it is actually inspect.getfullargspec that needs to change (getargspec will inherit the new behaviour though, since it uses getfullargspec internally)

With appropriate docs and tests updates, I don't see a problem with adding the feature, though. Docs should note and tests should ensure that this only goes one level deep - if __call__ isn't a real function either, inspect shouldn't try to follow the descriptor chain down the rabbit hole. Anything else runs the risk of infinite recursion in the face of things like "inspect.getargspec(list)".
msg140112 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2011-07-11 12:01
I can produce a patch w/ tests and documentation for you to review Nick.
msg141536 - (view) Author: Maxim Bublis (maxbublis) Date: 2011-08-01 21:44
I've ran into the same problem with getfullargspec not supporting callables, so I've written patch with docs and tests, that adds support for any Python callable. As a result of getfullargspec's implementation change, getargspec function also supports callables.
msg141539 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-08-01 22:36
I'm -0.5. I think the current patch makes too many assumptions for the caller. For example, someone calling a class may really desire __new__'s signature, not that of __init__. Moreover, conceptually, getargspec() returns the argspec of a directly callable *function* or *method*.
msg141540 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2011-08-01 22:44
Right. For a callable object (instance with __call__ method), it's unambiguous which signature you want. For a class it's ambiguous.
msg141541 - (view) Author: Maxim Bublis (maxbublis) Date: 2011-08-02 00:30
Agree, support for __new__ or __init__ methods would add some ambiquity, so i've decided to drop __init__ support from patch. Patch has been reuploaded.
msg185929 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2013-04-03 15:05
Would someone please review the patch file as it's out of my league.
msg209681 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-01-29 20:55
This is now fixed in #17481.
Date User Action Args
2014-01-29 20:55:18yselivanovsetstatus: open -> closed

nosy: + yselivanov
messages: + msg209681

dependencies: + inspect.getfullargspec should use __signature__
resolution: fixed
2013-04-03 15:05:27BreamoreBoysetnosy: + BreamoreBoy
messages: + msg185929
2011-08-02 13:13:32maxbublissetfiles: - inspect.patch
2011-08-02 00:30:25maxbublissetfiles: + inspect.patch

messages: + msg141541
2011-08-01 22:44:16michael.foordsetmessages: + msg141540
2011-08-01 22:36:14benjamin.petersonsetmessages: + msg141539
2011-08-01 21:44:36maxbublissetfiles: + inspect.patch

nosy: + maxbublis
messages: + msg141536

keywords: + patch
2011-07-11 12:01:26michael.foordsetmessages: + msg140112
2011-07-11 12:00:14ncoghlansetmessages: + msg140111
title: Allow callable objects in inspect.getargspec -> Allow callable objects in inspect.getfullargspec
2011-07-11 11:22:43michael.foordsetmessages: + msg140106
2011-07-09 20:50:44eric.snowsetnosy: + eric.snow
2011-07-09 20:14:10daniel.urbansetnosy: + daniel.urban
2011-07-09 14:27:54eric.araujosetversions: - Python 2.7, Python 3.2
2011-07-09 14:27:45eric.araujosetnosy: + ncoghlan, eric.araujo, benjamin.peterson, michael.foord
messages: + msg140069
2010-09-16 09:46:42marco.marianisetnosy: + marco.mariani

messages: + msg116518
versions: + Python 2.7, Python 3.3
2010-07-11 15:44:03BreamoreBoysetversions: - Python 2.7
2010-05-06 21:58:28gsakkiscreate