-
-
Notifications
You must be signed in to change notification settings - Fork 511
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
GTK+ 3 (GObject introspection) #531
Comments
Do you know fakegir ? |
No, but that looks very good and is code that even I can understand. Docstrings? @davidhalter should persistent pre-caching (which could be generated from a separately spawned process and kept in sync with timestamps) be used for more modules? Would solve many of the problems with builtins and also issues with being able to manage memory. Although maybe that's what you are already doing? |
@spillz I'm not sure what you are talking about. There's some caching for Python modules. But none for builtins (However, they are usually really fast). |
Maybe i am using wrong terminology. By builtin I mean c libs and other
|
No they are not cached, because importing them is very fast. Caching them would probably be slower. |
Probably not much slower, but more important is the possibility of
|
Possible. But not a high priority for me. Such a solution would be nice, but I just know that it's a lot of work and the current solution works very well. |
Hi! I'm working on a patch for gnome-builder (the new and awesome GNOME IDE) to provide auto-completion support for GObject Introspection libraries. For Python code, Builder already uses Jedi and it works great. Since it's a GNOME IDE, we really want it to be able to be able to have auto-completion for GObject Introspection. I can read and parse the GIR files (they are basically XML) or the typelib files (unsure if I can do this with python, it's a binary mmap-able database that has the same content of the XML files) of GObject Introspecton, but I'm unsure how to integrate that with Jedi... Can you give me some pointers? Would you accept a patch that will give Jedi the ability to parse these files? Alternatively, if that's out of the scope of Jedi itself, can I somehow monkey-patch this from within Builder when I call Jedi? I need Jedi in there (instead of just writing a whole new thing) because it already knows about types within the python code and works very well. Another option to consider is maybe (that's a big maybe, I'm not sure how simple it is to do) we can change pygobject to somehow be more Jedi friendly, so no change to Jedi itself will be needed? |
What does the The problem seems to be that in |
The Dynamic Importer (implementation: https://git.gnome.org/browse/pygobject/tree/gi/importer.py#n126 ) loads GObject-Intropsection enabled libraries. It implements the PEP302 Importer Protocol. It uses the IntrospectionModule class from https://git.gnome.org/browse/pygobject/tree/gi/module.py#n111 which is a wrapper around the introspection typelib. The actual wrapping is done by The actual loading is done in C, by this file https://git.gnome.org/browse/pygobject/tree/gi/pygi-repository.c (methods accessible by Python are defined in line 304). It's done by using the GObject Introspection library to load info about the libraries from the .typelib files which describe all the interfaces, properties, functions and such that the libraries provide. Here's some example code to get a list of properties of a GObject-Introspection library (module): from gi import Repository
r = Repository.get_default()
r.require('Gtk') # See note about versions
print(r.get_infos('Gtk')) For each object in this result set you can use Note about versions: The first parameter is the "namespace" - the name of the library/module you want to import, and the second parameter is the version. Is this clear enough? I could probably do more research and dive further into specifics. Regarding the problem you mentioned, I can't think of a way to implement PyGObject without using the meta_path feature - that's how If you feel special-casing PyGObject is out of scope for Jedi, can you suggest a way to monkey-patch Jedi when using it in Builder to add this kind of support? |
So why are you not just importing |
Because not every script / app that uses GObject has to use Gtk, and because that would only fix the case for one GObject-Intropsection library - Gtk. There are many more of these and we want to support them all for completion, and it doesn't make sense for pygobject to import all libraries just so completion would work. On my Fedora install I have 192 different library that can be used with GObject-Introspection. Gtk is just one of them. |
Ok. That's what I wanted to know. If you just want to monkey-patch Jedi, I guess it's best to replace the |
I've written a monkey patch to make jedi support GObject Introspection modules. You can re-use this code in Jedi if you want. from gi.importer import DynamicImporter
from gi.module import IntrospectionModule
import jedi
from jedi.evaluate.compiled import CompiledObject
from jedi.evaluate.imports import Importer
gi_importer = DynamicImporter('gi.repository')
class PatchedJediCompiledObject(CompiledObject):
"A modified version of Jedi CompiledObject to work with GObject Introspection modules"
def _cls(self):
if self.obj.__class__ == IntrospectionModule:
return self
else:
return super()._cls()
class PatchedJediImporter(Importer):
"A modified version of Jedi Importer to work with GObject Introspection modules"
def follow(self):
module_list = super().follow()
if module_list == []:
import_path = '.'.join([str(i) for i in self.import_path])
if import_path.startswith('gi.repository'):
try:
module = gi_importer.load_module(import_path)
module_list = [PatchedJediCompiledObject(module)]
except ImportError:
pass
return module_list
jedi.evaluate.imports.Importer = PatchedJediImporter Basically, in I patched I can re-format this as a patch for Jedi (and wrap it all in conditions, of course, so Jedi won't require pygojbect) if you want, but I am quite sure that my naive way of implementing this is not good enough to be upstreamed :) Another issue with this monkey patch is that iterating over (for example) GLib to get the completions will result in a lot of |
Well, it's actually a pretty good solution. Though, I don't know, why you need the However, I just remembered that we have
|
I need Overrides in GObject Introspection are basically python files that wrap around the module to make some APIs nicer, they are not used in many libraries because usually the automatic bindings just do the right thing. Regarding GObject Introspection is not just for Gtk, it's a generic system that allows you to get automatic language bindings for C libraries that use GObject, so the platform libraries are written in C and then via GObject Introspection you can use them from Python, JavaScript, Vala, and so on, without the developer having to generate bindings for each language manually. |
I know, but is it working with just |
Doesn't seem to be working:
|
Hmm ok. I (or you) should look into that sometime in the future. It's something that I would expect to work. Once it does we can add this star logic (which needs to be implemented first):
|
I'm not sure it's a good solution because loading all the GObject Introspection libraries available on the user's machine will waste a lot of memory - I'm pretty sure it loads the .so file after it parses the typelib. But it'd still be interesting to see why it didn't work. |
The star import wouldn't imply loading every single module. It would imply that Jedi would load each and every module requested from |
I'll debug why the auto import didn't work later this week. Meanwhile, I'm trying to make jedi understand pygobject style docstrings so it will know the return types of functions, to make completion work in more cases. These annotations either look like this: To test my implementation I'm using this line of code: If I try to run this without any modifications (apart from my patch to make jedi know about pygobject), it just outputs a very long traceback that ends in:
To work around that now I added a simple condition to change After working around this, I get no completions, so I tried adding a simple regex to catch the return type annotation in I kept looking and found Enabling jedi debugging makes it seems it goes too deep in the tree and finds documentation of a more basic type for some reason? I'm not sure what's going on here:
Clearly the relevant information would be in Can you give me any pointers as to how to debug this issue so I can get jedi to understand these annotations? Thanks. |
Hmm. To me it looks more like a bug. It actually reminds me a lot of a recent issue. Pretty sure this one would fix it: #615 (see last comment). |
It took me a while to find the time for this, but, I looked at the Running with debug enabled, I can see this:
It looks for the file, and since it doesn't find it, it raises an exception and The ImportError is added in the |
Hmm ok. We might actually need to modify the architecture then. Thanks for the research. |
What is the status of this issue ? |
Obivously no one has worked on it. |
@elad661 can you add a wip branch on your fork for this? |
Someone points out fakegir (but the link is broken)
Seems to workaround introspection autocompletion |
@albfan As far as I understand, and correct me if I'm wrong, @davidhalter will not accept GObject-Introspection-specific workarounds into Jedi. That's why we've been monkey-patching jedi in gnome-builder to add this functionality. |
Sure. is a good decision. Just adding some related info for people looking into this issue (because I see a broken link). |
After reread all this: This would be the way (accepted by jedi maintainer) to recognize GObject introspection
but it is not working #531 (comment) Meanwhile it is monkey-patched on Builder https://gitlab.gnome.org/GNOME/gnome-builder/blob/master/src/plugins/jedi/jedi_plugin.py#L100 |
I tried initial example on this issue using I get autocompletions like
but no for things like
As
So I think the example just miss some configuration: what is missing from That could clarify why autoimport_modules could work to offer autocompletions Here is the complete screencast: |
|
@albfan and all the others that wanted this. IMO the current jedi master branch is working well with the issues you've had. Sorry it took so long, but I think I never really figured out what the optimal solution was. IMO the current solution is not perfect, but it should be good enough for |
Thanks @davidhalter!! |
Work with upstream directly feels great! |
Can anyone plz help me with fixing this issue ?! |
@ShevoPramide this is fixed, open another issue if you have any problem |
github.com/strycore/fakegir is a logical workaround for GObject dynamic import. It works. 'gi', # This third-party repository (GTK stuff) doesn't really work with jedi Fakegir-like solution should be a standard for native modules, like a minimal documentation. Everything else in comments is hard/impossible to follow. |
I think once stubs exist for |
@davidhalter Probably a new open issue should be created, after further investigation.
But there exists /usr/lib/python3.8/site-packages/gi path with __init__.py in it. jedi.settings.auto_import_modules should be a separate python file that resides in a user's home directory. |
I really don't understand what you want :) |
Why?
Jedi can find it as /usr/lib/python3.8/site-packages/gi/__init__.py But I still want from Jedi a sys_path file: /usr/lib/python3.8/site-packages/gi/__init__.py that Jedi could find before polluting sys_path with a stub files path. That file in stubs can be empty, or not complete.
Without stubs Jedi can find I as a user don't want that file from stubs path if Jedi can find it in sys_path. Again, what if that file in stubs is empty, incomplete, or even missing? But Jedi can find it in sys_path without stubs. Currently by putting
Wrong assumption in the comment? |
Do stubs exist for Also about the sys path configuration, this is not really an issue and with the projects branch, one can configure quite a lot about sys paths. |
@davidhalter https://github.com/strycore/fakegir generates stubs for all found Gobject libs automatically and puts them into ~/.cache/fakegir And you are right, sys_path is easy, it's better to patch stub generating code if something is missing. fakegir is just in one python file. |
I'm not removing it for now. This is not a stub repository and as long as I don't see that I'm not removing it. (except if most other people using gi also prefer that fakegir). |
Doesn't matter after all.(low priority) After looking at how pyi(PEP 484) is used, it seems to me that Gobject Introspection was ahead of its time in 2008. |
Can somebody tell me how to remove |
Can't get completion from objects created by classes under Gtk: In code = '''
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
btn = Gtk.Button(label='Button')
btn.'''
jedi.Script(code).complete() Out [] |
It appears the jedi can't handle the GObject introspection module (using jedi 0.8.0-final0 on Ubuntu 14.04):
s2 completions show as expected, s3 is an empty list. Note that if I manually import Gtk3 and type dir(Gtk) I get a complete list of module symbols.
Are there any workarounds for this? (Don't get me started on what a disaster GI is for long suffering GTK+ app devs.)
The text was updated successfully, but these errors were encountered: