ipython: Tab complete of args list runs unexpected property code

Ipython executes parts of my code when using the autocomplete functionality.

System info:

>> python -c "import IPython; print(IPython.sys_info())"
{'commit_hash': u'8b97549',
 'commit_source': 'repository',
 'default_encoding': 'UTF-8',
 'ipython_path': '/home/ipython/IPython',
 'ipython_version': '4.1.0-dev',
 'os_name': 'posix',
 'platform': 'Linux-3.16.0-53-generic-x86_64-with-Ubuntu-14.04-trusty',
 'sys_executable': '/home/ipython/virtual_environment/bin/python',
 'sys_platform': 'linux2',
 'sys_version': '2.7.6 (default, Jun 22 2015, 17:58:13) \n[GCC 4.8.2]'}

Example:

In [1]: class Hello(object):                                                                                                                                            
    @property                                                                   
    def hello(self):                                                                
        print "It's me" 

In [2]: h=Hello()

Trying to autocomplete h.b, h.hello or h.b( works as I would expect. However h.h( or h.hello( results in

In [3]: h.h(It's me
Display all ... possibilities? (y or n)
....
In [3]: h.h(

which resulted in an unexpected printout.

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 2
  • Comments: 16 (3 by maintainers)

Most upvoted comments

Curiously, this also happens with plain Python. I just reproduced this in a fresh minimal conda environment with only Python 3.9, and see that it is in fact executing on autocompletion of h.<TAB>, but there is no IPython nor Jedi around:

(ipython-9094) bilderbuchi@osprey:~$ python
Python 3.9.1 (default, Dec 11 2020, 14:32:07) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class Hello:
...     @property
...     def hello(self):
...         print('In an executing property')
... 
>>> h = Hello()
>>> h.In an executing property
hello
In an executing property
>>> 
[4]+  Stopped                 python
(ipython-9094) bilderbuchi@osprey:~$ conda list
# packages in environment at /home/bilderbuchi/anaconda3/envs/ipython-9094:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main  
ca-certificates           2020.12.8            h06a4308_0  
certifi                   2020.12.5        py39h06a4308_0  
ld_impl_linux-64          2.33.1               h53a641e_7  
libedit                   3.1.20191231         h14c3975_1  
libffi                    3.3                  he6710b0_2  
libgcc-ng                 9.1.0                hdf63c60_0  
libstdcxx-ng              9.1.0                hdf63c60_0  
ncurses                   6.2                  he6710b0_1  
openssl                   1.1.1i               h27cfd23_0  
pip                       20.3.3           py39h06a4308_0  
python                    3.9.1                hdb3f193_2  
readline                  8.0                  h7b6447c_0  
setuptools                51.0.0           py39h06a4308_2  
sqlite                    3.33.0               h62c20be_0  
tk                        8.6.10               hbc83047_0  
tzdata                    2020d                h14c3975_0  
wheel                     0.36.2             pyhd3eb1b0_0  
xz                        5.2.5                h7b6447c_0  
zlib                      1.2.11               h7b6447c_3  

I searched for an issue in the Python bug tracker, but could not find anything.

Also, please allow me one remark on this:

a property should never be something that takes a lot of time to execute, otherwise it should probably be part of a function. It’s essentially a sign of bad code.

At least for our part (and probably also @natezb), this is not primarily about speed, but undesirable communication/code execution (in this case communication with hardware over the wire).

Properties are used to implement a simple (for the user) instrument interaction API, e.g. both setting and getting e.g. an instrument.voltage, while hiding away all the communication, value validation, and other details.

I sometimes use a sourcemeter instrument (think of it as a complicated power supply plus multimeter) with ~35 properties defined. Depending on the instrument state/configuration, not all are appropriate to run/query. I’ll have to check some time what exactly happens if I try to autocomplete on that one (see another demonstration in another package here). Yes, that could well become a speed issue on a serial connection, but I am more concerned about accidental side effects of code that was never intentionally run by the user, when all they wanted was to know what is available.

I am still seeing this issue with Jedi 0.15.1