classification
Title: "inspect.getargspec()" and "inspect.getcallargs()" don't work for builtins
Type: enhancement Stage:
Components: Versions: Python 3.4
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: larry Nosy List: asvetlov, berker.peksag, brett.cannon, david.villa, jcea, larry, yselivanov
Priority: normal Keywords:

Created on 2012-11-17 03:36 by jcea, last changed 2014-01-27 20:34 by yselivanov. This issue is now closed.

Messages (11)
msg175725 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2012-11-17 03:36
Yesterday I was attending a conference about a MOCK like library and the speaker told us about some "inspect" functionalities not working correctly with builtins. For instance:

"""
Python 3.3.0 (default, Oct  2 2012, 02:07:16) 
[GCC 4.4.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> def f(a=None) :
...   pass
... 
>>> inspect.getcallargs(f)
{'a': None}
>>> inspect.getargspec(f)
ArgSpec(args=['a'], varargs=None, keywords=None, defaults=(None,))
>>> inspect.getcallargs(list)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.3/inspect.py", line 993, in getcallargs
    spec = getfullargspec(func)
  File "/usr/local/lib/python3.3/inspect.py", line 850, in getfullargspec
    raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <class 'list'> is not a Python function
>>> inspect.getargspec(list)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.3/inspect.py", line 823, in getargspec
    getfullargspec(func)
  File "/usr/local/lib/python3.3/inspect.py", line 850, in getfullargspec
    raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <class 'list'> is not a Python function
>>> 
"""

Can we annotate builtins to support this?. What about types defined in CModules?
msg175726 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2012-11-17 03:51
I am fully aware that the interpreter doesn't know how a method/function written in C is going to parse the parameter list/keywords. And that even if we solve this (for instance, with annotations that create wrappers calling "PyArg_Parse()" automatically), every C extension out there would need to support it too.

Just brainstorming.
msg175745 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2012-11-17 14:46
So at this point you should use inspect.signature(), not getfullargspec(). 

With that you could do this if you either allowed setting the __signature__ attribute and then provided code that would set it, made __signature__ a property that returned the Signature object if desired on a built-in, or come up with some automated way to take the argument string from PyArg_Parse() as you suggested.
msg175752 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-11-17 15:08
I'm working on a solution for this--expect an announcement on c.l.p-d in, oh, a week.
msg176693 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2012-11-30 16:45
Larry, Ping... :-)
msg176867 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-12-03 22:49
http://mail.python.org/pipermail/python-dev/2012-December/122920.html
msg176878 - (view) Author: David Villa Alises (david.villa) Date: 2012-12-04 08:06
What about something like gobject.introspection?
https://live.gnome.org/GObjectIntrospection/
msg177022 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2012-12-06 01:25
David, please subscribe to Issue #16612.
msg177221 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2012-12-09 14:14
This looks like a duplicate of issue 1748064.
msg207922 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-01-11 23:21
This is related to issue #17481
msg209475 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-01-27 20:34
Closing this issue. See #17481 for details.
History
Date User Action Args
2014-01-27 20:34:56yselivanovsetstatus: open -> closed
resolution: duplicate
messages: + msg209475
2014-01-11 23:21:19yselivanovsetnosy: + yselivanov
messages: + msg207922
2012-12-11 11:11:46asvetlovlinkissue1748064 superseder
2012-12-09 14:14:25berker.peksagsetnosy: + berker.peksag
messages: + msg177221
2012-12-06 01:25:46jceasetmessages: + msg177022
2012-12-04 08:06:09david.villasetmessages: + msg176878
2012-12-03 22:49:39larrysetmessages: + msg176867
2012-11-30 16:45:08jceasetmessages: + msg176693
2012-11-22 12:09:16david.villasetnosy: + david.villa
2012-11-17 15:08:18larrysetassignee: larry
2012-11-17 15:08:11larrysetmessages: + msg175752
2012-11-17 14:46:45brett.cannonsetnosy: + brett.cannon, larry
messages: + msg175745
2012-11-17 14:18:01asvetlovsetnosy: + asvetlov
2012-11-17 03:51:52jceasetmessages: + msg175726
2012-11-17 03:37:29jceasettype: enhancement
2012-11-17 03:37:17jceasettitle: "inspect.getargspec()" and "inspect.getcallargs()" don't work with builtins -> "inspect.getargspec()" and "inspect.getcallargs()" don't work for builtins
2012-11-17 03:36:29jceacreate