classification
Title: inspect.signature removes initial starred method params (bug)
Type: behavior Stage: needs patch
Components: Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: yselivanov Nosy List: larry, louielu, ncoghlan, python-dev, terry.reedy, yselivanov
Priority: normal Keywords: patch

Created on 2014-01-27 02:16 by terry.reedy, last changed 2017-05-02 02:22 by terry.reedy. This issue is now closed.

Files
File name Uploaded Description Edit
signature_method_first_arg_01.patch yselivanov, 2014-01-28 00:03 review
Messages (12)
msg209378 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-01-27 02:16
>>> import inspect
>>> class C:
	def meth(*args): pass
	
>>> str(inspect.signature(C.meth))
'(*args)'
>>> c=C()
>>> str(s=inspect.signature(c.meth))
'()'

*args should have been left in even for the bound method, as idlelib calltips do.
Since '*' is not a word character,
idlelib.Calltips._first_param = re.compile('(?<=\()\w*\,?\s*')
does not see '*args' as a substring to be deleted with
    argspec = _first_param.sub("", argspec)

The same comment applies to
    def meth2(**kwds): pass
even though calling the bound method would fail.
msg209379 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-27 02:28
Yury, can you look in to this?

I have been modifying inspect.signature, but (almost) exclusively in from_builtin.  If this is my fault I'll be happy to fix it.
msg209386 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-01-27 03:47
There does not appear to be a test for this. If so, this case may not have ever worked right. Here is the only failure at the moment (which should also be fixed).

F:\Python\dev>4\py34\pcbuild\python_d.exe -m test test_inspect
[1/1] test_inspect
test test_inspect failed -- Traceback (most recent call last):
  File "F:\Python\dev\4\py34\lib\test\test_inspect.py", line 1623, in test_signature_on_builtins
    test_callable(type)
  File "F:\Python\dev\4\py34\lib\test\test_inspect.py", line 1604, in test_callable
    signature = inspect.signature(o)
  File "F:\Python\dev\4\py34\lib\inspect.py", line 1555, in signature
    raise ValueError('callable {!r} is not supported by signature'.format(obj))
ValueError: callable <class 'type'> is not supported by signature
msg209387 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-27 03:49
I just tried it in 3.3 (3.3.1rc1) and it had the same problem.
msg209393 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-01-27 04:37
Yes, that's s bug. I'll take a look tomorrow.
msg209400 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-01-27 05:35
Sorry, I should have said that I tried repository versions of both 3.3 and 3.4.
msg209488 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-01-28 00:03
Patch is attached, please review.
Should we commit this to 3.3 too?
msg209566 - (view) Author: Roundup Robot (python-dev) Date: 2014-01-28 17:26
New changeset 3544827d42e6 by Yury Selivanov in branch 'default':
inspect.signature: Handle bound methods with '(*args)' signature correctly #20401
http://hg.python.org/cpython/rev/3544827d42e6
msg209567 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-01-28 17:27
Committed. Thank you, Terry.
msg292637 - (view) Author: Louie Lu (louielu) * Date: 2017-04-30 17:11
Is there any reason that the second case Terry provide still will failed with ValueError?

class C:
    def meth2(**kwds): pass


ip.signature(C().meth2)

Traceback (most recent call last):
  File "/home/linux/Python/cpython/Lib/idlelib/idle_test/test_calltips.py", line 139, in test_starred_parameter
    self.assertEqual(signature(meth), mtip)
  File "/home/linux/Python/cpython/Lib/idlelib/calltips.py", line 149, in get_argspec
    argspec = str(inspect.signature(fob))
  File "/home/linux/Python/cpython/Lib/inspect.py", line 3028, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
  File "/home/linux/Python/cpython/Lib/inspect.py", line 2778, in from_callable
    follow_wrapper_chains=follow_wrapped)
  File "/home/linux/Python/cpython/Lib/inspect.py", line 2195, in _signature_from_callable
    return _signature_bound_method(sig)
  File "/home/linux/Python/cpython/Lib/inspect.py", line 1785, in _signature_bound_method
    raise ValueError('invalid method signature')
ValueError: invalid method signature
msg292701 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-05-01 21:36
> Is there any reason that the second case Terry provide still will failed with ValueError?

> class C:
>    def meth2(**kwds): pass


> ip.signature(C().meth2)

Yes, the reason is that `C().meth2` is an invalid method that can't be called. Try calling `C().meth2()` and you will get a TypeError.

There is no bug here.
msg292711 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-05-02 02:22
OK, we will change this example in test_calltips once calltips uses .signature (#19903).
History
Date User Action Args
2017-05-02 02:22:59terry.reedysetmessages: + msg292711
2017-05-01 21:36:19yselivanovsetmessages: + msg292701
2017-04-30 17:11:33louielusetnosy: + louielu
messages: + msg292637
2014-01-28 17:27:33yselivanovsetstatus: open -> closed
resolution: fixed
messages: + msg209567
2014-01-28 17:26:41python-devsetnosy: + python-dev
messages: + msg209566
2014-01-28 00:03:49yselivanovsetfiles: + signature_method_first_arg_01.patch

nosy: + ncoghlan
messages: + msg209488

assignee: yselivanov
keywords: + patch
2014-01-27 05:35:14terry.reedysetmessages: + msg209400
2014-01-27 04:37:54yselivanovsetmessages: + msg209393
2014-01-27 03:49:41larrysetmessages: + msg209387
2014-01-27 03:47:41terry.reedysetmessages: + msg209386
2014-01-27 02:40:14terry.reedylinkissue19903 dependencies
2014-01-27 02:28:30larrysetnosy: + larry
messages: + msg209379
2014-01-27 02:16:33terry.reedycreate