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: Change to inspect.signature for calltips #1382

Closed
wants to merge 10 commits into from
Closed

bpo-19903: IDLE: Change to inspect.signature for calltips #1382

wants to merge 10 commits into from

Conversation

louisom
Copy link
Contributor

@louisom louisom commented May 2, 2017

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

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.
@mention-bot
Copy link

@lulouie, 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.

@@ -136,6 +136,9 @@ def get_argspec(ob):
argspec = ""
try:
ob_call = ob.__call__
except AttributeError:
Copy link
Member

Choose a reason for hiding this comment

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

Could you left this change for separate issue?

if (isinstance(ob, (type, types.MethodType)) or
isinstance(ob_call, types.MethodType)):
argspec = _first_param.sub("", argspec)
if argspec.startswith("(self,"):
Copy link
Member

Choose a reason for hiding this comment

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

What if the first parameter have a different name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right. I'll still need to figure out how it does different from getfullargspec

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Find out that signature will deal correctly with MethodType, but will not handle __init__ correctly.
Fix the condition of trim first param.

@brettcannon brettcannon added the type-feature A feature request or enhancement label May 2, 2017
@terryjreedy terryjreedy self-assigned this May 2, 2017
@louisom
Copy link
Contributor Author

louisom commented May 3, 2017

@serhiy-storchaka @terryjreedy Also add the test for first param not self test.

c = C()
for meth, mtip in ((C.m1, '(*args)'), (c.m1, "(*args)"),
(C.m2, "(**kwds)"), (c.m2, "(**kwds)"),):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove invalid method signature, move to test_invalid_method_signature


mtip = "This function has an invalid method signature"
self.assertEqual(signature(C().m2), mtip)
self.assertEqual(signature(Test()), mtip)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test borrow from test_signature

@@ -156,17 +164,22 @@ def test_attribute_exception(self):
class NoCall:
def __getattr__(self, name):
raise BaseException
class Call(NoCall):
class CallA(NoCall):
def __call__(oui, a, b, c):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add corner case for first param not self

self.assertEqual(signature(meth), mtip)

def test_non_callables(self):
for obj in (0, 0.0, '0', b'0', [], {}):
self.assertEqual(signature(obj), '')


Copy link
Contributor Author

Choose a reason for hiding this comment

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

PEP8

@terryjreedy terryjreedy self-requested a review May 4, 2017 07:07
@@ -145,10 +148,16 @@ def get_argspec(ob):
else:
fob = ob
if isinstance(fob, (types.FunctionType, types.MethodType)):
Copy link
Member

Choose a reason for hiding this comment

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

Remove and dedent block.

argspec = inspect.formatargspec(*inspect.getfullargspec(fob))
try:
argspec = str(inspect.signature(fob))
except ValueError:
Copy link
Member

Choose a reason for hiding this comment

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

Must get and test error message.

try:
argspec = str(inspect.signature(fob))
except ValueError:
argspec = "This function has an invalid method signature"
Copy link
Member

Choose a reason for hiding this comment

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

Add _invalid_signature to global defs before function so can access for testing without repeating string.

argspec = inspect.formatargspec(*inspect.getfullargspec(fob))
try:
argspec = str(inspect.signature(fob))
except ValueError:
Copy link
Member

Choose a reason for hiding this comment

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

Condition return on error message

except ValueError:
argspec = "This function has an invalid method signature"
return argspec

if (isinstance(ob, (type, types.MethodType)) or
Copy link
Member

Choose a reason for hiding this comment

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

Add argspec != '' to later revised condition

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.

See msg292964 on issue for explanations and comment on what test addition is needed.

@@ -1,4 +1,4 @@
"""calltips.py - An IDLE Extension to Jog Your Memory
b"""calltips.py - An IDLE Extension to Jog Your Memory
Copy link
Member

Choose a reason for hiding this comment

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

??? Docstrings are not bytes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A typo..., I'm using trackpoint on laptop, sometime when scrolling down, it will unexpectedly
pressing "b"

@@ -139,21 +139,32 @@ def get_argspec(ob):
ob_call = ob.__call__
except BaseException:
return argspec
if isinstance(ob, type):
fob = ob.__init__
Copy link
Member

Choose a reason for hiding this comment

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

Why remove this? It worked before. Does signature() do this automatically? Such a change should be explained on the issue with examples, both for Python and C coded classed. And yes, I wish I had added comments here or in test file or, by reference, an issue, when I revised this and and added test file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As you said on msg, this will automatically done by signature.

if '/' in argspec:
"""Argument Clinic positional-only mark, ignore the result of signature
"""
argspec = ''
Copy link
Member

Choose a reason for hiding this comment

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

Why? '/' is used in legitimate signatures, and appear, I believe, in the docs.

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 use your method in msg293008

@louisom louisom changed the title bpo-19903: Change to inspect.signature for calltips bpo-19903: IDLE: Change to inspect.signature for calltips May 10, 2017
@terryjreedy
Copy link
Member

terryjreedy commented Jul 24, 2017

Replaced by #2822 with new account name. LEAVE THIS OPEN FOR NOW. Since it will not be merged, I am using it as one example of not being able to edit the code due to lack permission.

@terryjreedy
Copy link
Member

Edit by others being turned off seems to be resolved for now.

@terryjreedy terryjreedy closed this Aug 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants