IPython - Enhanced Interactive Python

Autocall fails if first function argument begins with "-" or "+"

Reported by Dirk Muders on 2009-01-10
2
Affects Status Importance Assigned to Milestone
IPython
Confirmed
Undecided
Fernando Perez

Bug Description

Auto parentheses fail if the first argument of a function begins with "-" or "+". Thus these examples fail:

autocall 2
def f(a):
    print a

f -1

f +1

The reason is the re_exclude_auto in prefilter.py which reads

re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]'
                                                   r'|^is |^not |^in |^and |^or ')

in v0.9.1. It appears that this line had been changed at some point after v0.6.2 to also catch operators. However, "-" and "+" can be used with numbers as shown above. Changing the line to

re_exclude_auto = re.compile(r'^[,&^\|\*/]'
                                                   r'|^is |^not |^in |^and |^or ')

fixes the problem but I do not know if something else was intended by adding "\+-". If so, then this would need to be caught in a different way.

Fernando Perez (fdo.perez) wrote :

No, see this comment in the code:

# RegExp to exclude strings with this start from autocalling. In
# particular, all binary operators should be excluded, so that if foo is
# callable, foo OP bar doesn't become foo(OP bar), which is invalid.

Since any callable X could potentially also implement arithmetic, so that

X+1

is as valid as (and different from!):

X(1)

We can't autocall on *any* binary operator.

Changed in ipython:
status: New → Invalid
Dirk Muders (dmuders) wrote :

I understand that argument. In our application we have created a user CLI based on IPython, partially because of being able to omit the parentheses. This makes it easier for users who are unfamiliar with Python but who have used other interfaces that do not need the parentheses. The case with signs (of course mostly the "-" sign) as first characters often occurs in our interface, hence the default IPython behavior breaks the general behavior of our interface and is thus undesirable. We are working with a locally modified version of IPython, but it would be nice to have an "official" switch to choose between the two options.

Changed in ipython:
status: Invalid → Confirmed
assignee: nobody → Fernando Perez (fdo.perez)
Fernando Perez (fdo.perez) wrote :

Well, if you are embedding IPython yourselves, you can try doing something like (this is code for trunk, the imports will be slightly different for 0.10):

import re
from IPython.core import prefilter

prefilter.re_exclude_auto = re.compile('your regexp here')

# rest of your code...

Let me know if this works for you. If it does what you need OK, you have a workaround for now, and I'll then refactor the code to have an official API for this.

I've reopened the bug so it stays tracked.

Dirk Muders (dmuders) wrote :

We are using IPython directly and we are currently working with a version that we edited in prefilter.py. Next time we upgrade IPython we would have to re-introduce this modification. For the time being we can work this way.

OK. Since the upcoming 0.11 release is going to be somewhat of a
'tech preview' (many apis will change), when we put it out we're going
to ask for a lot of feedback from anyone making embedded uses of it.
At that point, we can look at a good api for this (and possibly other
filtering-related things).

Do ping us to remind us when the discussion on api requests comes up,
we want feedback from embedded users on all possible needs.

Cheers,

f

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers