Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't tab complete object's attribute when called via container class's __getitem__ method #13647

Open
eseiver opened this issue Apr 21, 2022 · 2 comments

Comments

@eseiver
Copy link

eseiver commented Apr 21, 2022

I have a list of instances of one class (Cat) stored in a container class (Cats) as an attribute. I created a __getitem__ method in the container class that retrieves items from the list by index. When I use that method to get an item in the list, the attribute autocomplete options do not appear for that Cat object. The attributes do appear in tab-complete when I call the list by index directly.

In [1]: class Cats:
   ...:     def __init__(self, cat_list):
   ...:         self.cat_list = cat_list
   ...:
   ...:     def __getitem__(self, key):
   ...:         return self.cat_list[key]
   ...:
   ...: class Cat:
   ...:     def __init__(self, name, color):
   ...:         self.name = name
   ...:         self.color = color

In [2]: cat1 = Cat('Muffy', 'brown')
   ...: cat2 = Cat('Fluffy', 'white')
   ...: cat3 = Cat('Tuffy', 'tuxedo')
   ...: all_cats = [cat1, cat2, cat3]
   ...: cats = Cats(cat_list=all_cats)

In [3]: cats[0] == cats.cat_list[0]
Out[3]: True

In [4]: cats[0].name
Out[4]: 'Muffy'

In [5]: cats.cat_list[0].<tab>  # name, color at top of tab-complete list

In [6]: cats[0].<tab>  # name and color do not appear

@Carreau
Copy link
Member

Carreau commented Apr 21, 2022

Thanks for the report,

I'll look into it, but that might be something we do on purpose for __getitem__ as it may have side effects.

A long time ago we had a bug report from people using __getitem__ to access an electronic device values. The problem with electronic devices being that most of the time when you read the value they get erased to leave place for the next one. That lead to the problematic.

>>> device['x_voltage'].<tab>

Trying to read the content of x_voltage and thus erasing the value.

You can try to turn on "greedy" completion.

--Completer.greedy=<Bool>
    Activate greedy completion
            PENDING DEPRECATION. this is now mostly taken care of with Jedi.
            This will enable completion on elements of lists, results of function calls, etc.,
            but can be unsafe because the code is actually evaluated on TAB.
    Default: False

Strange thing is that even with greedy completion it does not show (which I would expect), so there might be something else happening.

@krassowski
Copy link
Member

Strange thing is that even with greedy completion it does not show (which I would expect), so there might be something else happening.

This is because one also needs to switch use_jedi=False as otherwise python_matches are not used. More detailed analysis in #13836.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants