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

Nick Coghlan ncoghlan at gmail.com
Fri Feb 8 16:08:16 CET 2013


On Sat, Feb 9, 2013 at 12:09 AM, Stefan Behnel <stefan_ml at behnel.de> wrote:

Hi,

I'm wondering about the purpose of this code in inspect.Signature.fromfunction(): """ 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).

Cheers, Nick.

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



More information about the Python-Dev mailing list