Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Do not add extra brackets for autocallable functions.

Closes gh-1117.
  • Loading branch information...
commit 8303bd116657dd12fa06133dfd2d3f4672e74fc2 1 parent 21c436b
@bfroehle authored
Showing with 21 additions and 17 deletions.
  1. +20 −17 IPython/core/prefilter.py
  2. +1 −0  IPython/core/tests/test_handlers.py
View
37 IPython/core/prefilter.py
@@ -827,27 +827,30 @@ def handle(self, line_info):
newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
else:
# Auto-paren.
- # We only apply it to argument-less calls if the autocall
- # parameter is set to 2. We only need to check that autocall is <
- # 2, since this function isn't called unless it's at least 1.
- if not the_rest and (self.shell.autocall < 2) and not force_auto:
- newcmd = '%s %s' % (ifun,the_rest)
- auto_rewrite = False
+
+ if force_auto:
+ # Don't rewrite if it is already a call.
+ do_rewrite = not the_rest.startswith('(')
else:
- if not force_auto and the_rest.startswith('['):
- if hasattr(obj,'__getitem__'):
- # Don't autocall in this case: item access for an object
- # which is BOTH callable and implements __getitem__.
- newcmd = '%s %s' % (ifun,the_rest)
- auto_rewrite = False
- else:
- # if the object doesn't support [] access, go ahead and
- # autocall
- newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
- elif the_rest.endswith(';'):
+ if not the_rest:
+ # We only apply it to argument-less calls if the autocall
+ # parameter is set to 2.
+ do_rewrite = self.shell.autocall == 2
+ elif the_rest.startswith('[') and hasattr(obj, '__getitem__'):
+ # Don't autocall in this case: item access for an object
+ # which is BOTH callable and implements __getitem__.
+ do_rewrite = False
+ else:
+ do_rewrite = True
+
+ # Figure out the rewritten command
+ if do_rewrite:
+ if the_rest.endswith(';'):
newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
else:
newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
+ else:
+ newcmd = '%s %s' % (ifun,the_rest)
if auto_rewrite:
self.shell.auto_rewrite_input(newcmd)
View
1  IPython/core/tests/test_handlers.py
@@ -132,6 +132,7 @@ def test_handlers():
run([
('len "abc"', 'len "abc"'),
('autocallable', 'autocallable()'),
+ ('autocallable()', 'autocallable ()'),
(",list 1 2 3", 'list("1", "2", "3")'),
(";list 1 2 3", 'list("1 2 3")'),
("/len range(1,4)", 'len(range(1,4))'),
Please sign in to comment.
Something went wrong with that request. Please try again.