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

Unable to complete on an attribute defined via a property #1305

Closed
ngoldbaum opened this issue Apr 1, 2019 · 3 comments
Closed

Unable to complete on an attribute defined via a property #1305

ngoldbaum opened this issue Apr 1, 2019 · 3 comments

Comments

@ngoldbaum
Copy link

Following on from #1027, I don't seem to get "foo" or "bar" printed out:

class CompleteAttrs(object):
    def __getattr__(self, name):
        if name == 'foo':
            return 1
        if name == 'bar':
            return 2
        raise AttributeError(name)

    def __dir__(self):
        return ['foo', 'bar'] + object.__dir__(self)

class MyClass(object):
    _ca = None

    @property
    def ca(self):
        if self._ca:
            return self._ca
        self._ca = CompleteAttrs()
        return self._ca

from jedi.api import Interpreter

itp = Interpreter("foo.ca.", [{'foo': MyClass()}])
for c in itp.completions():
    print(c.name)

If I add property to ALLOWED_DESCRIPTOR_ACCESS in jedi/evaluate/compiled/access.py then I get the result I expect, however it seems this breaks test_property_error_oldstyle.

If this is intended behavior Is there a way I can keep my cached property attributes that are set up like this without breaking tab completion in IPython, or does jedi want me to define attributes at runtime in the __init__ methods of my objects?

@ngoldbaum
Copy link
Author

I guess this is a repeat of #1299

@davidhalter
Copy link
Owner

Thanks for checking the other tickets as well, that helps :) I guess it's kind of the same.

As I noted before, the problem with this is that Jedi wants to avoid code execution.

Would it help you if Jedi understood this properly:

    def ca(self) -> CompleteAttrs:

I know this is a bit cumbersome to write and think about, but it would at least help with autocompletion. Another pending thing would be a flag to enable code execution, but I feel like that's just a bad idea.

@ngoldbaum
Copy link
Author

The type hint solution would be fine, although that won't fix old code that's already in the wild.

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

No branches or pull requests

2 participants