[issue17159] Remove explicit type check from inspect.Signature.from_function()

Stefan Behnel report at bugs.python.org
Sat Feb 1 17:20:48 CET 2014


Stefan Behnel added the comment:

Hmm, I now notice that I was mistaken about this working:

'''
import inspect

def test_isfunction():
    """
    >>> test_isfunction()
    True
    """
    return inspect.isfunction(test_isfunction)
'''

It only worked in Cython's test suite because its test runner monkey patches "inspect.isfunction", and I had completely forgotten about it. Sorry for the confusion.

The thing is that Cython's function type isn't really a Python function (obviously), it inherits from PyCFunction, so it should return True for isbuiltin(). A problem on our side prevented that. If I fix it up, then the newly added duck-typing code actually ends up not being used, because signature() tests for isbuiltin() first and runs into Signature.from_builtin(), which is the Argument Clinic code path that expects a textual signature representation. Cython functions don't have that, because they are compatible with Python functions.

This situation could be helped in inspect.signature() by reversing the test order, i.e. by changing this code

    if _signature_is_builtin(obj):
        return Signature.from_builtin(obj)

    if isfunction(obj) or _signature_is_functionlike(obj):
        # If it's a pure Python function, or an object that is duck type
        # of a Python function (Cython functions, for instance), then:
        return Signature.from_function(obj)

into this:

    if isfunction(obj) or _signature_is_functionlike(obj):
        # If it's a pure Python function, or an object that is duck type
        # of a Python function (Cython functions, for instance), then:
        return Signature.from_function(obj)

    if _signature_is_builtin(obj):
        return Signature.from_builtin(obj)

Would this be ok?

I would also argue that the implementation of _signature_is_builtin() is, well, not ideal, because what it should test for according to the comment at the top of the function is the existance of "__text_signature__". Instead, it does several type tests, one of which goes wrong in this case.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17159>
_______________________________________


More information about the Python-bugs-list mailing list