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

bpo-19903: IDLE: Calltips changed to use inspect.signature #2822

Merged
merged 10 commits into from
Aug 10, 2017

Conversation

mlouielu
Copy link
Contributor

@mlouielu mlouielu commented Jul 23, 2017

This commit change the get_argspec from using inspect.getfullargspec
to inspect.signature. It will improve the tip message for use.

Also, if object is not callable, now will return this message for
user, not blank tips. If the methods has an invalid method signature,
it will also return message to user.

Re-open from #1382

https://bugs.python.org/issue19903

@mention-bot
Copy link

@mlouielu, thanks for your PR! By analyzing the history of the files in this pull request, we identified @terryjreedy, @kbkaiser and @serhiy-storchaka to be potential reviewers.

@terryjreedy
Copy link
Member

Louie, I cannot add commits to this issue, and a few others of yours and others. Please follow the instructions at https://help.github.com/articles/allowing-changes-to-a-pull-request-branch-created-from-a-fork/#enabling-repository-maintainer-permissions-on-existing-pull-requests to make sure "Allow edits from maintainers." is checked for this and other IDLE issues. Please let me know what you found. If that is already checked, then I have some other problem with github.

@mlouielu
Copy link
Contributor Author

@terryjreedy The check is on now. Hmmm, I thought that is default to check-up? FWIR this has been discussed on python-committers before?

@terryjreedy
Copy link
Member

There seems to have been a problem with a robot unchecking the edit switch under some circumstances. Since patched.

@terryjreedy terryjreedy self-requested a review August 2, 2017 21:46
@terryjreedy terryjreedy self-assigned this Aug 2, 2017
@terryjreedy
Copy link
Member

terryjreedy commented Aug 2, 2017

I want to make a few minor changes, do some live tests, and merge. But when I tried to make a local PR, git trapped my clone in a loop. See core-mentorship. Fixed

Copy link
Member

@terryjreedy terryjreedy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote minor changes I planned to write, to make code slightly clearer. The should not affect tests.

PR also needs a blurb added. The title line might be enough. In any case, web edit works well for blurbs.

@@ -139,17 +141,31 @@ def get_argspec(ob):
ob_call = ob.__call__
except BaseException:
return argspec
Copy link
Member

@terryjreedy terryjreedy Aug 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/argspec = ""/argspec = default = ""/ above
/argspec/default/ here
-- done

if isinstance(ob, type):
fob = ob.__init__
elif isinstance(ob_call, types.MethodType):
if isinstance(ob_call, types.MethodType):
fob = ob_call
else:
fob = ob
Copy link
Member

@terryjreedy terryjreedy Aug 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with conditional expression.
-- done

pass
else:
"""Callable is not supported by signature"""
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace body with conditional expression:
return _invalid_method if str(err).startswith(_invalid_method) else default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do this, it will break the unittest, for e.g. range will not able to return argspec.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, in these cases, we still look for docstring.

This commit change the get_argspec from using inspect.getfullargspec
to inspect.signature. It will improve the tip message for use.

Also, if object is not callable, now will return this message for
user, not blank tips. If the methods has an invalid method signature,
it will also return message to user.
Copy link
Member

@terryjreedy terryjreedy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am doing some manual checking while waiting for CI, but expect to merge.

@mlouielu
Copy link
Contributor Author

mlouielu commented Aug 4, 2017

@terryjreedy Can this be backported to 3.5 and 3.6?

@terryjreedy
Copy link
Member

Funny you should ask. When 3.6.0b1 came out, I stopped backporting to anything before 3.6: too much work and too much danger of regressions. In testing this, I noticed that ')' no longer closes calltips -- a bad regression since 3.6.2. Since repository 3.6.2+ has the same problem, it is definitely not this patch. I suspect bpo-30723. Of course, there is no test of such behaviors.

That aside, this patch would have less effect in 3.5 (pre-Arg Clinic, I believe), and the
last 3.5 maintenance release candidate is already out, so it is effectively in security-fix only mode.

too dangerousCurrently, everything is backported to 3.6, and only 3.6. The last 3.5 maintenance release candidate was issued a week ago, so it is effectively in security-only mode.

@@ -123,6 +123,8 @@ def get_entity(expression):
_INDENT = ' '*4 # for wrapped signatures
_first_param = re.compile(r'(?<=\()\w*\,?\s*')
_default_callable_argspec = "See source or doc"
_invalid_method = "invalid method signature"
_argument_positional = "('/' marks preceding arguments as positional-only)"
Copy link
Member

@terryjreedy terryjreedy Aug 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon seeing a real example, I don't like the clash between () as call operator and comment marker in tips such as

(object, /)
('/' marks preceding arguments as positional-only)
Append object to the end of the list.

I prefer either [] or # as the comment marker. What think you?

(object, /)
['/' marks preceding arguments as positional-only]
Append object to the end of the list.

(object, /)
# '/' marks preceding arguments as positional-only
Append object to the end of the list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll prefer ['/' marks preceding arguments as positional-only] style, it has more conspicuous when the text was end at the box margin.

Also, I think the word is OK, using google to search will got this stackoverflow post at the top, it should not be a problem for newbie.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By 'word', I presume you meant 'positional-only'. Nice check. We'll go with your preference.


if '/' in argspec:
"""Using AC's positional argument should add the explain"""
argspec = '\n'.join([argspec, _argument_positional])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argspec += '\n' + _argument_positional

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix _argument_positional to "\n['/' marks preceding arguments as positional-only]\n"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better.

non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a string, backslash escapes in it are processed. If it is
a callable, it's passed the match object and must return''')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer that this be truncated also. We allowed up to 5 docstring lines for builtins to get the signature (bytes needs 5), but if we already have signature, only 1 should be allowed, as below. However, after checking the code and the docstring, I decided that the source of the problem is that the re.sub docstring is defective in not having an overly long summary sentence and then not having a blank line after the first '.', as it should. We could check for '.', but not worth the effort now. Instead I would like to add a comment so it is clear that this case was thought about. This is a good additional test case.

# Comment runs on because of long first sentence and missing blank thereafter. Display as above.

@mlouielu
Copy link
Contributor Author

mlouielu commented Aug 8, 2017

This PR should be block until the regression of not closing tipbox been fixed.

I don't think the problem came from #2306, I test it on mac first git pr 2822, then git revert fae2c3538e, the problem still exists.

@mlouielu
Copy link
Contributor Author

mlouielu commented Aug 8, 2017

Block fixed by 8922587

Copy link
Member

@terryjreedy terryjreedy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes made, some with improvements.

@terryjreedy terryjreedy merged commit 3b0f620 into python:master Aug 10, 2017
@bedevere-bot
Copy link

GH-3053 is a backport of this pull request to the 3.6 branch.

terryjreedy pushed a commit to terryjreedy/cpython that referenced this pull request Aug 10, 2017
terryjreedy added a commit to terryjreedy/cpython that referenced this pull request Aug 10, 2017
…honGH-2822)

Idlelib.calltips.get_argspec now uses inspect.signature instead of inspect.getfullargspec, like help() does.  This improves the signature in the call tip in a few different cases, including builtins converted to provide a signature.  A message is added if the object is not callable, has an invalid signature, or if it has positional-only parameters.
Patch by Louie Lu..
(cherry picked from commit 3b0f620)
terryjreedy added a commit that referenced this pull request Aug 10, 2017
…2822) (#3053)

Idlelib.calltips.get_argspec now uses inspect.signature instead of inspect.getfullargspec, like help() does.  This improves the signature in the call tip in a few different cases, including builtins converted to provide a signature.  A message is added if the object is not callable, has an invalid signature, or if it has positional-only parameters.
Patch by Louie Lu..
(cherry picked from commit 3b0f620)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants