[Python-Dev] Why does Signature.from_function() have to check the type of its argument? (original) (raw)
Stefan Behnel stefan_ml at behnel.de
Fri Feb 8 23:44:11 CET 2013
- Previous message: [Python-Dev] Why does Signature.from_function() have to check the type of its argument?
- Next message: [Python-Dev] Why does Signature.from_function() have to check the type of its argument?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Stefan Behnel, 08.02.2013 22:14:
PJ Eby, 08.02.2013 19:46:
On Fri, Feb 8, 2013 at 10:54 AM, Stefan Behnel wrote:
Nick Coghlan, 08.02.2013 16:20:
On Sat, Feb 9, 2013 at 1:06 AM, Benjamin Peterson wrote:
2013/2/8 Stefan Behnel:
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? The signature() function checks for types.FunctionType in order to call Signature.fromfunction(). How would you reimplement that? It should call isfunction() instead of running an explicit type check. Isn't it possible now for an object to implement instancecheck and claim to be an instance of FunctionType, anyway? (For that matter, shouldn't there be some ABCs for this?) Wow, good call. Providing an instancecheck() method that simply says yes when it's asked for PyFunctionType really works.
Argh - sorry, got it all wrong. "instancecheck()" works exactly the other way round. In the type check above, it's the FunctionType type that gets asked for an instance check, which doesn't help at all in this case because it simply doesn't know about the desired subtype relation. It would work if type(func).instancecheck() was used, but that doesn't happen.
So, no help from that side, sadly.
Stefan
- Previous message: [Python-Dev] Why does Signature.from_function() have to check the type of its argument?
- Next message: [Python-Dev] Why does Signature.from_function() have to check the type of its argument?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]