Skip to content

inspect.signature removes initial starred method params (bug) #64600

Closed

Description

@terryjreedy
BPO 20401
Nosy @terryjreedy, @ncoghlan, @larryhastings, @1st1, @mlouielu
Files
  • signature_method_first_arg_01.patch
  • 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 = 'https://github.com/1st1'
    closed_at = <Date 2014-01-28.17:27:33.930>
    created_at = <Date 2014-01-27.02:16:33.088>
    labels = ['type-bug']
    title = 'inspect.signature removes initial starred method params (bug)'
    updated_at = <Date 2017-05-02.02:22:59.304>
    user = 'https://github.com/terryjreedy'

    bugs.python.org fields:

    activity = <Date 2017-05-02.02:22:59.304>
    actor = 'terry.reedy'
    assignee = 'yselivanov'
    closed = True
    closed_date = <Date 2014-01-28.17:27:33.930>
    closer = 'yselivanov'
    components = []
    creation = <Date 2014-01-27.02:16:33.088>
    creator = 'terry.reedy'
    dependencies = []
    files = ['33758']
    hgrepos = []
    issue_num = 20401
    keywords = ['patch']
    message_count = 12.0
    messages = ['209378', '209379', '209386', '209387', '209393', '209400', '209488', '209566', '209567', '292637', '292701', '292711']
    nosy_count = 6.0
    nosy_names = ['terry.reedy', 'ncoghlan', 'larry', 'python-dev', 'yselivanov', 'louielu']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'needs patch'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue20401'
    versions = ['Python 3.3', 'Python 3.4']

    Activity

    1. terryjreedy commented on Jan 27, 2014

      @terryjreedy
      MemberAuthor
      >>> import inspect
      >>> class C:
      	def meth(*args): pass
      	
      >>> str(inspect.signature(C.meth))
      '(*args)'
      >>> c=C()
      >>> str(s=inspect.signature(c.meth))
      '()'

      *args should have been left in even for the bound method, as idlelib calltips do.
      Since '*' is not a word character,
      idlelib.Calltips._first_param = re.compile('(?<=\()\w*\,?\s*')
      does not see '*args' as a substring to be deleted with
      argspec = _first_param.sub("", argspec)

      The same comment applies to
      def meth2(**kwds): pass
      even though calling the bound method would fail.

    2. larryhastings commented on Jan 27, 2014

      @larryhastings
      Contributor

      Yury, can you look in to this?

      I have been modifying inspect.signature, but (almost) exclusively in from_builtin. If this is my fault I'll be happy to fix it.

    3. terryjreedy commented on Jan 27, 2014

      @terryjreedy
      MemberAuthor

      There does not appear to be a test for this. If so, this case may not have ever worked right. Here is the only failure at the moment (which should also be fixed).

      F:\Python\dev>4\py34\pcbuild\python_d.exe -m test test_inspect
      [1/1] test_inspect
      test test_inspect failed -- Traceback (most recent call last):
        File "F:\Python\dev\4\py34\lib\test\test_inspect.py", line 1623, in test_signature_on_builtins
          test_callable(type)
        File "F:\Python\dev\4\py34\lib\test\test_inspect.py", line 1604, in test_callable
          signature = inspect.signature(o)
        File "F:\Python\dev\4\py34\lib\inspect.py", line 1555, in signature
          raise ValueError('callable {!r} is not supported by signature'.format(obj))
      ValueError: callable <class 'type'> is not supported by signature
    4. larryhastings commented on Jan 27, 2014

      @larryhastings
      Contributor

      I just tried it in 3.3 (3.3.1rc1) and it had the same problem.

    5. 1st1 commented on Jan 27, 2014

      @1st1
      Member

      Yes, that's s bug. I'll take a look tomorrow.

    6. terryjreedy commented on Jan 27, 2014

      @terryjreedy
      MemberAuthor

      Sorry, I should have said that I tried repository versions of both 3.3 and 3.4.

    7. 1st1 commented on Jan 28, 2014

      @1st1
      Member

      Patch is attached, please review.
      Should we commit this to 3.3 too?

    8. self-assigned this
      on Jan 28, 2014
    9. python-dev commented on Jan 28, 2014

      python-devmannequin
      Mannequin

      New changeset 3544827d42e6 by Yury Selivanov in branch 'default':
      inspect.signature: Handle bound methods with '(*args)' signature correctly bpo-20401
      http://hg.python.org/cpython/rev/3544827d42e6

    10. 1st1 commented on Jan 28, 2014

      @1st1
      Member

      Committed. Thank you, Terry.

    11. mlouielu commented on Apr 30, 2017

      mlouielumannequin
      Mannequin

      Is there any reason that the second case Terry provide still will failed with ValueError?

      class C:
          def meth2(**kwds): pass

      ip.signature(C().meth2)

      Traceback (most recent call last):
        File "/home/linux/Python/cpython/Lib/idlelib/idle_test/test_calltips.py", line 139, in test_starred_parameter
          self.assertEqual(signature(meth), mtip)
        File "/home/linux/Python/cpython/Lib/idlelib/calltips.py", line 149, in get_argspec
          argspec = str(inspect.signature(fob))
        File "/home/linux/Python/cpython/Lib/inspect.py", line 3028, in signature
          return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
        File "/home/linux/Python/cpython/Lib/inspect.py", line 2778, in from_callable
          follow_wrapper_chains=follow_wrapped)
        File "/home/linux/Python/cpython/Lib/inspect.py", line 2195, in _signature_from_callable
          return _signature_bound_method(sig)
        File "/home/linux/Python/cpython/Lib/inspect.py", line 1785, in _signature_bound_method
          raise ValueError('invalid method signature')
      ValueError: invalid method signature
    12. 1st1 commented on May 1, 2017

      @1st1
      Member

      Is there any reason that the second case Terry provide still will failed with ValueError?

      class C:
      def meth2(**kwds): pass

      ip.signature(C().meth2)

      Yes, the reason is that C().meth2 is an invalid method that can't be called. Try calling C().meth2() and you will get a TypeError.

      There is no bug here.

    13. terryjreedy commented on May 2, 2017

      @terryjreedy
      MemberAuthor

      OK, we will change this example in test_calltips once calltips uses .signature (bpo-19903).

    14. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions