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

[Question] Getting class of object on which completion was requested #1895

Open
nishikantparmariam opened this issue Nov 21, 2022 · 3 comments

Comments

@nishikantparmariam
Copy link

nishikantparmariam commented Nov 21, 2022

Hi, I am developing tab completion feature for a Python library and have found jedi to be quite useful. My use case is to detect the class name of the object on which completion was requested. Is there a clean way to achieve this? I could find below workaround, here the goal is to find that x is of type Z.

from jedi import Interpreter

class Z:
    def foo(self): pass

code = """
x = Z()
x.
"""

interpreter = Interpreter(code, [locals()])
completions = interpreter.complete(line=3, column=2)
foo = completions[0].full_name  # completion for function foo, full name is '__main__.Z.foo'
class_name = '.'.join(foo.split('.')[:-1])
class_name

Thanks!

@davidhalter
Copy link
Owner

Does completions[0].parent().full_name work for you?

@nishikantparmariam
Copy link
Author

Does completions[0].parent().full_name work for you?

Thanks for the reply!! Yes, this works.

But, actually I was looking for a way to this without calling interpreter.complete. I would like to explain my use-case a bit more -

I want to show custom completions only on objects which are of a certain class (say Z in this case), I want to determine statically if x is of type Z - the workaround I am using is putting a method foo on Z and checking if foo comes when I complete x.. This doesn't scale well though. Happy to know if there some solutions around this in jedi.

@davidhalter
Copy link
Owner

I don't understand, sorry.

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

2 participants