[Python-Dev] Why does Signature.from_function() have to check the type of its argument?

Nick Coghlan ncoghlan at gmail.com
Fri Feb 8 16:20:41 CET 2013
On Sat, Feb 9, 2013 at 1:06 AM, Benjamin Peterson <benjamin at python.org> wrote:
> 2013/2/8 Stefan Behnel <stefan_ml at behnel.de>:
>> Hi,
>>
>> I'm wondering about the purpose of this code in
>> inspect.Signature.from_function():
>>
>> """
>>     if not isinstance(func, types.FunctionType):
>>         raise TypeError('{!r} is not a Python function'.format(func))
>> """
>>
>> Is there any reason why this method would have to explicitly check the type
>> of its argument? Why can't it just accept any object that quacks like a
>> function?
>
> The signature() function checks for types.FunctionType in order to
> call Signature.from_function(). How would you reimplement that?

I assumed Stefan was wanting to use Signature.from_function() to set
__signature__ (as soon as you do that, inspect.signature will do the
right thing).

At the moment, you would have to build the signature by hand (which
was the original intent of the design), but we simply didn't think
about projects like Cython that might be providing the full
types.FunctionType API without being ordinary function instances.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list