Commit 949fe976 authored by Terry Jan Reedy's avatar Terry Jan Reedy Committed by GitHub

bpo-35763: Make IDLE calltip note about '/' less obtrusive (GH-13791)

Add it to the end of the first line if there is room.  Tests were reworked.
parent 59e7bbca
...@@ -118,7 +118,7 @@ _INDENT = ' '*4 # for wrapped signatures ...@@ -118,7 +118,7 @@ _INDENT = ' '*4 # for wrapped signatures
_first_param = re.compile(r'(?<=\()\w*\,?\s*') _first_param = re.compile(r'(?<=\()\w*\,?\s*')
_default_callable_argspec = "See source or doc" _default_callable_argspec = "See source or doc"
_invalid_method = "invalid method signature" _invalid_method = "invalid method signature"
_argument_positional = "\n['/' marks preceding arguments as positional-only]\n" _argument_positional = " # '/' marks preceding args as positional-only."
def get_argspec(ob): def get_argspec(ob):
'''Return a string describing the signature of a callable object, or ''. '''Return a string describing the signature of a callable object, or ''.
...@@ -144,11 +144,11 @@ def get_argspec(ob): ...@@ -144,11 +144,11 @@ def get_argspec(ob):
if msg.startswith(_invalid_method): if msg.startswith(_invalid_method):
return _invalid_method return _invalid_method
if '/' in argspec: if '/' in argspec and len(argspec) < _MAX_COLS - len(_argument_positional):
"""Using AC's positional argument should add the explain""" # Add explanation TODO remove after 3.7, before 3.9.
argspec += _argument_positional argspec += _argument_positional
if isinstance(fob, type) and argspec == '()': if isinstance(fob, type) and argspec == '()':
"""fob with no argument, use default callable argspec""" # If fob has no argument, use default callable argspec.
argspec = _default_callable_argspec argspec = _default_callable_argspec
lines = (textwrap.wrap(argspec, _MAX_COLS, subsequent_indent=_INDENT) lines = (textwrap.wrap(argspec, _MAX_COLS, subsequent_indent=_INDENT)
......
...@@ -4,8 +4,7 @@ from idlelib import calltip ...@@ -4,8 +4,7 @@ from idlelib import calltip
import unittest import unittest
import textwrap import textwrap
import types import types
import re
default_tip = calltip._default_callable_argspec
# Test Class TC is used in multiple get_argspec test methods # Test Class TC is used in multiple get_argspec test methods
...@@ -28,6 +27,7 @@ class TC(): ...@@ -28,6 +27,7 @@ class TC():
t6.tip = "(no, self)" t6.tip = "(no, self)"
def __call__(self, ci): 'doc' def __call__(self, ci): 'doc'
__call__.tip = "(self, ci)" __call__.tip = "(self, ci)"
def nd(self): pass # No doc.
# attaching .tip to wrapped methods does not work # attaching .tip to wrapped methods does not work
@classmethod @classmethod
def cm(cls, a): 'doc' def cm(cls, a): 'doc'
...@@ -36,11 +36,12 @@ class TC(): ...@@ -36,11 +36,12 @@ class TC():
tc = TC() tc = TC()
signature = calltip.get_argspec # 2.7 and 3.x use different functions default_tip = calltip._default_callable_argspec
get_spec = calltip.get_argspec
class Get_signatureTest(unittest.TestCase): class Get_argspecTest(unittest.TestCase):
# The signature function must return a string, even if blank. # The get_spec function must return a string, even if blank.
# Test a variety of objects to be sure that none cause it to raise # Test a variety of objects to be sure that none cause it to raise
# (quite aside from getting as correct an answer as possible). # (quite aside from getting as correct an answer as possible).
# The tests of builtins may break if inspect or the docstrings change, # The tests of builtins may break if inspect or the docstrings change,
...@@ -49,57 +50,59 @@ class Get_signatureTest(unittest.TestCase): ...@@ -49,57 +50,59 @@ class Get_signatureTest(unittest.TestCase):
def test_builtins(self): def test_builtins(self):
def tiptest(obj, out):
self.assertEqual(get_spec(obj), out)
# Python class that inherits builtin methods # Python class that inherits builtin methods
class List(list): "List() doc" class List(list): "List() doc"
# Simulate builtin with no docstring for default tip test # Simulate builtin with no docstring for default tip test
class SB: __call__ = None class SB: __call__ = None
def gtest(obj, out):
self.assertEqual(signature(obj), out)
if List.__doc__ is not None: if List.__doc__ is not None:
gtest(List, '(iterable=(), /)' + calltip._argument_positional tiptest(List,
+ '\n' + List.__doc__) f'(iterable=(), /){calltip._argument_positional}'
gtest(list.__new__, f'\n{List.__doc__}')
tiptest(list.__new__,
'(*args, **kwargs)\n' '(*args, **kwargs)\n'
'Create and return a new object. ' 'Create and return a new object. '
'See help(type) for accurate signature.') 'See help(type) for accurate signature.')
gtest(list.__init__, tiptest(list.__init__,
'(self, /, *args, **kwargs)' '(self, /, *args, **kwargs)'
+ calltip._argument_positional + '\n' + + calltip._argument_positional + '\n' +
'Initialize self. See help(type(self)) for accurate signature.') 'Initialize self. See help(type(self)) for accurate signature.')
append_doc = (calltip._argument_positional append_doc = (calltip._argument_positional
+ "\nAppend object to the end of the list.") + "\nAppend object to the end of the list.")
gtest(list.append, '(self, object, /)' + append_doc) tiptest(list.append, '(self, object, /)' + append_doc)
gtest(List.append, '(self, object, /)' + append_doc) tiptest(List.append, '(self, object, /)' + append_doc)
gtest([].append, '(object, /)' + append_doc) tiptest([].append, '(object, /)' + append_doc)
tiptest(types.MethodType, "method(function, instance)")
tiptest(SB(), default_tip)
gtest(types.MethodType, "method(function, instance)")
gtest(SB(), default_tip)
import re
p = re.compile('') p = re.compile('')
gtest(re.sub, '''\ tiptest(re.sub, '''\
(pattern, repl, string, count=0, flags=0) (pattern, repl, string, count=0, flags=0)
Return the string obtained by replacing the leftmost Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable; replacement repl. repl can be either a string or a callable;
if a string, backslash escapes in it are processed. If it is if a string, backslash escapes in it are processed. If it is
a callable, it's passed the Match object and must return''') a callable, it's passed the Match object and must return''')
gtest(p.sub, '''\ tiptest(p.sub, '''\
(repl, string, count=0) (repl, string, count=0)
Return the string obtained by replacing the leftmost \ Return the string obtained by replacing the leftmost \
non-overlapping occurrences o...''') non-overlapping occurrences o...''')
def test_signature_wrap(self): def test_signature_wrap(self):
if textwrap.TextWrapper.__doc__ is not None: if textwrap.TextWrapper.__doc__ is not None:
self.assertEqual(signature(textwrap.TextWrapper), '''\ self.assertEqual(get_spec(textwrap.TextWrapper), '''\
(width=70, initial_indent='', subsequent_indent='', expand_tabs=True, (width=70, initial_indent='', subsequent_indent='', expand_tabs=True,
replace_whitespace=True, fix_sentence_endings=False, break_long_words=True, replace_whitespace=True, fix_sentence_endings=False, break_long_words=True,
drop_whitespace=True, break_on_hyphens=True, tabsize=8, *, max_lines=None, drop_whitespace=True, break_on_hyphens=True, tabsize=8, *, max_lines=None,
placeholder=' [...]')''') placeholder=' [...]')''')
def test_properly_formated(self): def test_properly_formated(self):
def foo(s='a'*100): def foo(s='a'*100):
pass pass
...@@ -112,35 +115,35 @@ non-overlapping occurrences o...''') ...@@ -112,35 +115,35 @@ non-overlapping occurrences o...''')
indent = calltip._INDENT indent = calltip._INDENT
str_foo = "(s='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"\ sfoo = "(s='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"\
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + indent + "aaaaaaaaa"\ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + indent + "aaaaaaaaa"\
"aaaaaaaaaa')" "aaaaaaaaaa')"
str_bar = "(s='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"\ sbar = "(s='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"\
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + indent + "aaaaaaaaa"\ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + indent + "aaaaaaaaa"\
"aaaaaaaaaa')\nHello Guido" "aaaaaaaaaa')\nHello Guido"
str_baz = "(s='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"\ sbaz = "(s='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"\
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + indent + "aaaaaaaaa"\ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + indent + "aaaaaaaaa"\
"aaaaaaaaaa', z='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"\ "aaaaaaaaaa', z='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"\
"bbbbbbbbbbbbbbbbb\n" + indent + "bbbbbbbbbbbbbbbbbbbbbb"\ "bbbbbbbbbbbbbbbbb\n" + indent + "bbbbbbbbbbbbbbbbbbbbbb"\
"bbbbbbbbbbbbbbbbbbbbbb')" "bbbbbbbbbbbbbbbbbbbbbb')"
self.assertEqual(calltip.get_argspec(foo), str_foo) for func,doc in [(foo, sfoo), (bar, sbar), (baz, sbaz)]:
self.assertEqual(calltip.get_argspec(bar), str_bar) with self.subTest(func=func, doc=doc):
self.assertEqual(calltip.get_argspec(baz), str_baz) self.assertEqual(get_spec(func), doc)
def test_docline_truncation(self): def test_docline_truncation(self):
def f(): pass def f(): pass
f.__doc__ = 'a'*300 f.__doc__ = 'a'*300
self.assertEqual(signature(f), '()\n' + 'a' * (calltip._MAX_COLS-3) + '...') self.assertEqual(get_spec(f), f"()\n{'a'*(calltip._MAX_COLS-3) + '...'}")
def test_multiline_docstring(self): def test_multiline_docstring(self):
# Test fewer lines than max. # Test fewer lines than max.
self.assertEqual(signature(range), self.assertEqual(get_spec(range),
"range(stop) -> range object\n" "range(stop) -> range object\n"
"range(start, stop[, step]) -> range object") "range(start, stop[, step]) -> range object")
# Test max lines # Test max lines
self.assertEqual(signature(bytes), '''\ self.assertEqual(get_spec(bytes), '''\
bytes(iterable_of_ints) -> bytes bytes(iterable_of_ints) -> bytes
bytes(string, encoding[, errors]) -> bytes bytes(string, encoding[, errors]) -> bytes
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
...@@ -150,7 +153,7 @@ bytes() -> empty bytes object''') ...@@ -150,7 +153,7 @@ bytes() -> empty bytes object''')
# Test more than max lines # Test more than max lines
def f(): pass def f(): pass
f.__doc__ = 'a\n' * 15 f.__doc__ = 'a\n' * 15
self.assertEqual(signature(f), '()' + '\na' * calltip._MAX_LINES) self.assertEqual(get_spec(f), '()' + '\na' * calltip._MAX_LINES)
def test_functions(self): def test_functions(self):
def t1(): 'doc' def t1(): 'doc'
...@@ -166,14 +169,16 @@ bytes() -> empty bytes object''') ...@@ -166,14 +169,16 @@ bytes() -> empty bytes object''')
doc = '\ndoc' if t1.__doc__ is not None else '' doc = '\ndoc' if t1.__doc__ is not None else ''
for func in (t1, t2, t3, t4, t5, TC): for func in (t1, t2, t3, t4, t5, TC):
self.assertEqual(signature(func), func.tip + doc) with self.subTest(func=func):
self.assertEqual(get_spec(func), func.tip + doc)
def test_methods(self): def test_methods(self):
doc = '\ndoc' if TC.__doc__ is not None else '' doc = '\ndoc' if TC.__doc__ is not None else ''
for meth in (TC.t1, TC.t2, TC.t3, TC.t4, TC.t5, TC.t6, TC.__call__): for meth in (TC.t1, TC.t2, TC.t3, TC.t4, TC.t5, TC.t6, TC.__call__):
self.assertEqual(signature(meth), meth.tip + doc) with self.subTest(meth=meth):
self.assertEqual(signature(TC.cm), "(a)" + doc) self.assertEqual(get_spec(meth), meth.tip + doc)
self.assertEqual(signature(TC.sm), "(b)" + doc) self.assertEqual(get_spec(TC.cm), "(a)" + doc)
self.assertEqual(get_spec(TC.sm), "(b)" + doc)
def test_bound_methods(self): def test_bound_methods(self):
# test that first parameter is correctly removed from argspec # test that first parameter is correctly removed from argspec
...@@ -181,7 +186,8 @@ bytes() -> empty bytes object''') ...@@ -181,7 +186,8 @@ bytes() -> empty bytes object''')
for meth, mtip in ((tc.t1, "()"), (tc.t4, "(*args)"), for meth, mtip in ((tc.t1, "()"), (tc.t4, "(*args)"),
(tc.t6, "(self)"), (tc.__call__, '(ci)'), (tc.t6, "(self)"), (tc.__call__, '(ci)'),
(tc, '(ci)'), (TC.cm, "(a)"),): (tc, '(ci)'), (TC.cm, "(a)"),):
self.assertEqual(signature(meth), mtip + doc) with self.subTest(meth=meth, mtip=mtip):
self.assertEqual(get_spec(meth), mtip + doc)
def test_starred_parameter(self): def test_starred_parameter(self):
# test that starred first parameter is *not* removed from argspec # test that starred first parameter is *not* removed from argspec
...@@ -189,17 +195,18 @@ bytes() -> empty bytes object''') ...@@ -189,17 +195,18 @@ bytes() -> empty bytes object''')
def m1(*args): pass def m1(*args): pass
c = C() c = C()
for meth, mtip in ((C.m1, '(*args)'), (c.m1, "(*args)"),): for meth, mtip in ((C.m1, '(*args)'), (c.m1, "(*args)"),):
self.assertEqual(signature(meth), mtip) with self.subTest(meth=meth, mtip=mtip):
self.assertEqual(get_spec(meth), mtip)
def test_invalid_method_signature(self): def test_invalid_method_get_spec(self):
class C: class C:
def m2(**kwargs): pass def m2(**kwargs): pass
class Test: class Test:
def __call__(*, a): pass def __call__(*, a): pass
mtip = calltip._invalid_method mtip = calltip._invalid_method
self.assertEqual(signature(C().m2), mtip) self.assertEqual(get_spec(C().m2), mtip)
self.assertEqual(signature(Test()), mtip) self.assertEqual(get_spec(Test()), mtip)
def test_non_ascii_name(self): def test_non_ascii_name(self):
# test that re works to delete a first parameter name that # test that re works to delete a first parameter name that
...@@ -208,12 +215,9 @@ bytes() -> empty bytes object''') ...@@ -208,12 +215,9 @@ bytes() -> empty bytes object''')
assert calltip._first_param.sub('', uni) == '(a)' assert calltip._first_param.sub('', uni) == '(a)'
def test_no_docstring(self): def test_no_docstring(self):
def nd(s): for meth, mtip in ((TC.nd, "(self)"), (tc.nd, "()")):
pass with self.subTest(meth=meth, mtip=mtip):
TC.nd = nd self.assertEqual(get_spec(meth), mtip)
self.assertEqual(signature(nd), "(s)")
self.assertEqual(signature(TC.nd), "(s)")
self.assertEqual(signature(tc.nd), "()")
def test_attribute_exception(self): def test_attribute_exception(self):
class NoCall: class NoCall:
...@@ -229,11 +233,13 @@ bytes() -> empty bytes object''') ...@@ -229,11 +233,13 @@ bytes() -> empty bytes object''')
for meth, mtip in ((NoCall, default_tip), (CallA, default_tip), for meth, mtip in ((NoCall, default_tip), (CallA, default_tip),
(NoCall(), ''), (CallA(), '(a, b, c)'), (NoCall(), ''), (CallA(), '(a, b, c)'),
(CallB(), '(ci)')): (CallB(), '(ci)')):
self.assertEqual(signature(meth), mtip) with self.subTest(meth=meth, mtip=mtip):
self.assertEqual(get_spec(meth), mtip)
def test_non_callables(self): def test_non_callables(self):
for obj in (0, 0.0, '0', b'0', [], {}): for obj in (0, 0.0, '0', b'0', [], {}):
self.assertEqual(signature(obj), '') with self.subTest(obj=obj):
self.assertEqual(get_spec(obj), '')
class Get_entityTest(unittest.TestCase): class Get_entityTest(unittest.TestCase):
......
Make calltip reminder about '/' meaning positional-only less obtrusive by
only adding it when there is room on the first line.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment