Skip to content

Commit bd4a3f2

Browse files
authored
bpo-39314: Closes parenthesis when autocompleting for functions that take no arguments (GH-20562)
1 parent 0c4f0f3 commit bd4a3f2

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

Lib/rlcompleter.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import atexit
3333
import builtins
34+
import inspect
3435
import __main__
3536

3637
__all__ = ["Completer"]
@@ -96,7 +97,13 @@ def complete(self, text, state):
9697

9798
def _callable_postfix(self, val, word):
9899
if callable(val):
99-
word = word + "("
100+
word += "("
101+
try:
102+
if not inspect.signature(val).parameters:
103+
word += ")"
104+
except ValueError:
105+
pass
106+
100107
return word
101108

102109
def global_matches(self, text):

Lib/test/test_rlcompleter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ def test_global_matches(self):
4040

4141
# test with a customized namespace
4242
self.assertEqual(self.completer.global_matches('CompleteM'),
43-
['CompleteMe('])
43+
['CompleteMe()'])
4444
self.assertEqual(self.completer.global_matches('eg'),
4545
['egg('])
4646
# XXX: see issue5256
4747
self.assertEqual(self.completer.global_matches('CompleteM'),
48-
['CompleteMe('])
48+
['CompleteMe()'])
4949

5050
def test_attr_matches(self):
5151
# test with builtins namespace
@@ -64,7 +64,7 @@ def test_attr_matches(self):
6464
['CompleteMe.spam'])
6565
self.assertEqual(self.completer.attr_matches('Completeme.egg'), [])
6666
self.assertEqual(self.completer.attr_matches('CompleteMe.'),
67-
['CompleteMe.mro(', 'CompleteMe.spam'])
67+
['CompleteMe.mro()', 'CompleteMe.spam'])
6868
self.assertEqual(self.completer.attr_matches('CompleteMe._'),
6969
['CompleteMe._ham'])
7070
matches = self.completer.attr_matches('CompleteMe.__')
@@ -134,7 +134,7 @@ def test_duplicate_globals(self):
134134
# No opening bracket "(" because we overrode the built-in class
135135
self.assertEqual(completer.complete('memoryview', 0), 'memoryview')
136136
self.assertIsNone(completer.complete('memoryview', 1))
137-
self.assertEqual(completer.complete('Ellipsis', 0), 'Ellipsis(')
137+
self.assertEqual(completer.complete('Ellipsis', 0), 'Ellipsis()')
138138
self.assertIsNone(completer.complete('Ellipsis', 1))
139139

140140
if __name__ == '__main__':
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`rlcompleter.Completer` and the standard Python shell now close the
2+
parenthesis for functions that take no arguments. Patch contributed by Rémi
3+
Lapeyre.

0 commit comments

Comments
 (0)