Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inspect.signature reporting "()" for all builtin & extension types #68122

Closed
ncoghlan opened this issue Apr 13, 2015 · 8 comments
Closed

inspect.signature reporting "()" for all builtin & extension types #68122

ncoghlan opened this issue Apr 13, 2015 · 8 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@ncoghlan
Copy link
Contributor

BPO 23934
Nosy @ncoghlan, @larryhastings, @1st1
Files
  • issue_23934.patch
  • issue_23934-test.patch: unit test fix for Lib/test/test_inspect.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2015-05-30.21:10:04.931>
    created_at = <Date 2015-04-13.16:38:13.421>
    labels = ['type-bug']
    title = 'inspect.signature reporting "()" for all builtin & extension types'
    updated_at = <Date 2015-05-30.21:10:04.930>
    user = 'https://github.com/ncoghlan'

    bugs.python.org fields:

    activity = <Date 2015-05-30.21:10:04.930>
    actor = 'yselivanov'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-05-30.21:10:04.931>
    closer = 'yselivanov'
    components = []
    creation = <Date 2015-04-13.16:38:13.421>
    creator = 'ncoghlan'
    dependencies = []
    files = ['38936', '38949']
    hgrepos = []
    issue_num = 23934
    keywords = ['patch']
    message_count = 8.0
    messages = ['240649', '240650', '240656', '240669', '240715', '240724', '240756', '244500']
    nosy_count = 5.0
    nosy_names = ['ncoghlan', 'larry', 'python-dev', 'yselivanov', 'james']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue23934'
    versions = ['Python 3.4', 'Python 3.5']

    @ncoghlan
    Copy link
    Contributor Author

    inspect.signature isn't currently handling builtin & extension types correctly - these show up as having neither __new__ *nor* __init__ as pure Python callables, so the inspect.signature logic falls through into a currently unhandled case.

    _testcapi isn't currently exporting an extension type as docstring introspection fodder, only callables, so test_inspect didn't pick up the problem. The problem can be seen with builtin types like str:

    >>> import inspect
    >>> inspect.signature(str)
    <inspect.Signature object at 0x7fb81d44e518>
    >>> print(inspect.signature(str))
    ()

    Expected behaviour would be to throw a ValueError as with builtin callables without a signature:

    >>> import _testcapi
    >>> import inspect
    >>> inspect.signature(_testcapi.docstring_no_signature)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/ncoghlan/devel/py3k/Lib/inspect.py", line 2830, in signature
        return Signature.from_callable(obj)
      File "/home/ncoghlan/devel/py3k/Lib/inspect.py", line 2586, in from_callable
        return _signature_from_callable(obj, sigcls=cls)
      File "/home/ncoghlan/devel/py3k/Lib/inspect.py", line 2064, in _signature_from_callable
        skip_bound_arg=skip_bound_arg)
      File "/home/ncoghlan/devel/py3k/Lib/inspect.py", line 1984, in _signature_from_builtin
        raise ValueError("no signature found for builtin {!r}".format(func))
    ValueError: no signature found for builtin <built-in function docstring_no_signature>

    @ncoghlan ncoghlan added the type-bug An unexpected behavior, bug, or error label Apr 13, 2015
    @larryhastings
    Copy link
    Contributor

    Would this qualify as an "easy" bug we could throw to a sprinter?

    @ncoghlan
    Copy link
    Contributor Author

    James Powell is currently looking into this at the PyCon sprints.

    The key problem appears to be the check that assumes "obj.__init__ is object.__init__" indicates that there's no user defined init or new method, when in fact a builtin or extension type that only overrides __new__ would also pass that check (since the earlier check for __new__ returns None if the method implementation isn't a pure Python callable)

    @james
    Copy link
    Mannequin

    james mannequin commented Apr 13, 2015

    Discussed with Nick Coghlan.

    See attached patch to return signature(object) only if both __new__ and __init__ are shared with object.

    Otherwise, raise TypeError indicating built-in types not supported.

    https://docs.python.org/3/library/inspect.html#inspect.signature
    "Raises ValueError if no signature can be provided, and TypeError if that type of object is not supported."

    @ncoghlan
    Copy link
    Contributor Author

    The main missing piece now is test fodder for this error. The simplest near-term option would be to use one of the actual builtins, such as 'str'.

    In Python 3.5+ we could borrow the matmulType that was added to _testcapi to test the "@" operator, future-proofing the test against eventual conversion of the builtin types to support Argument Clinic.

    @james
    Copy link
    Mannequin

    james mannequin commented Apr 13, 2015

    See attached patch for unittest.

    For 3.4, test that inspect.signature(str) raises TypeError.

    For >3.5, this can be improved to use _testcapi.matmulType

    @1st1
    Copy link
    Member

    1st1 commented Apr 13, 2015

    The patch looks good.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 30, 2015

    New changeset e59966bb6de5 by Yury Selivanov in branch '3.5':
    Issue bpo-23934: Fix inspect.signature to fail correctly for builtin types.
    https://hg.python.org/cpython/rev/e59966bb6de5

    New changeset 19e0ffdd1daa by Yury Selivanov in branch 'default':
    Issue bpo-23934: Fix inspect.signature to fail correctly for builtin types.
    https://hg.python.org/cpython/rev/19e0ffdd1daa

    @1st1 1st1 closed this as completed May 30, 2015
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants