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

Stefan Behnel stefan_ml at behnel.de
Fri Feb 8 16:52:47 CET 2013

Nick Coghlan, 08.02.2013 16:08:
> On Sat, Feb 9, 2013 at 12:09 AM, Stefan Behnel wrote:
>> 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?
> Primarily because the expected approach for alternative objects that
> want to expose signature in 3.3 was for them to provide the
> __signature__ attribute directly, rather than providing the far
> messier grab bag of eclectic attributes exposed by native Python
> functions (there's also a tentative plan kicking around somewhere to
> have a string version for 3.4, so we can start doing something useful
> with functions implemented in C).
> I suspect the specific reason for the lack of ducktyping is merely to
> provide a nice error message when people call the class method rather
> than the module level function, so removing the check sounds
> reasonable to me.  The ultimate decision will be Georg's as RM,
> though. Probably the quickest way to get a decision from him would be
> a tracker issue set to release blocker, with a patch removing those
> two lines and a test confirming that non-functions with the right
> attributes are correctly accepted (e.g. by copying the attributes from
> a real function to a custom object and checking
> Signature.from_function gives the same answer).

Thanks for the feedback, patch is up.



More information about the Python-Dev mailing list