classification
Title: inspect.signature doesn't always return a signature
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: yselivanov Nosy List: Claudiu.Popa, larry, ncoghlan, python-dev, yselivanov
Priority: normal Keywords: needs review, patch

Created on 2014-06-18 12:41 by Claudiu.Popa, last changed 2014-06-23 17:24 by yselivanov. This issue is now closed.

Files
File name Uploaded Description Edit
inspect_signature.patch Claudiu.Popa, 2014-06-18 12:41 review
issue21801.patch yselivanov, 2014-06-20 18:13 review
Messages (5)
msg220936 - (view) Author: Claudiu Popa (Claudiu.Popa) * Date: 2014-06-18 12:41
Hello. I noticed the following behaviour while working with xmlrpc proxy methods.

>>> import inspect, xmlrpc.client
>>> proxy = xmlrpc.client.ServerProxy('http://localhost:8000')
>>> proxy.mul
<xmlrpc.client._Method object at 0x03B0C890>
>>> inspect.signature(proxy.mul)
<xmlrpc.client._Method object at 0x03B0CC90>
>>>


Now, according to the documentation, inspect.signature should return a signature or fail, but in this case it returns the actual object, which is misleading at least. This happens because _Method implements a proxy __getattr__, any accessed attribute becoming again a _Method. At the same time, inspect.signature happily uses 

try:
   obj.__signature__
except AttributeError:
   ...

to obtain the signature, if present.

The attached patch checks if __signature__ is an actual Signature object. I searched, but couldn't find any, if __signature__ can be anything, so I hope that this approach works.
msg221103 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-06-20 18:13
This behaviour is indeed a bug. However, I think that the solution you propose is wrong.

If we ignore invalid contents of __signature__ we are masking a bug or incorrect behaviour. In this case, you should have checked the requested attribute name in '__getattr__', and return something other than _Method, if it is a '__signature__'.

Please find attached a patch, that checks if __signature__ is an instance of Signature class, and raises a TypeError if it isn't.
msg221114 - (view) Author: Claudiu Popa (Claudiu.Popa) * Date: 2014-06-20 19:16
Your patch looks good to me.
msg221368 - (view) Author: Roundup Robot (python-dev) Date: 2014-06-23 17:24
New changeset cc0f5d6ccb70 by Yury Selivanov in branch '3.4':
inspect: Validate that __signature__ is None or an instance of Signature.
http://hg.python.org/cpython/rev/cc0f5d6ccb70

New changeset fa5b985f0920 by Yury Selivanov in branch 'default':
inspect: Validate that __signature__ is None or an instance of Signature.
http://hg.python.org/cpython/rev/fa5b985f0920
msg221369 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-06-23 17:24
Fixed in 3.4 and 3.5.
Thanks for the bug report!
History
Date User Action Args
2014-06-23 17:24:36yselivanovsetmessages: + msg221369
2014-06-23 17:24:02python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg221368

resolution: fixed
stage: resolved
2014-06-20 20:06:06yselivanovsetkeywords: + needs review
assignee: yselivanov
versions: + Python 3.4
2014-06-20 19:16:39Claudiu.Popasetmessages: + msg221114
2014-06-20 18:13:56yselivanovsetfiles: + issue21801.patch
nosy: + ncoghlan, larry
messages: + msg221103

2014-06-18 12:41:45Claudiu.Popacreate