pygobject / pygobject-stubs

PEP 561 Typing Stubs for PyGObject
GNU Lesser General Public License v2.1
78 stars 27 forks source link

Import "gi.repository.GLib" could not be resolved from source #189

Open laoshaw opened 3 months ago

laoshaw commented 3 months ago

After I did: pip install pygobject-stubs --no-cache-dir --config-settings=config=Gtk4,Gdk4,Soup3 (for gtk4), or just the default install(pip install pygobject-stubs pygobject), my vim still reports above warning, it runs, but the warning stays no matter what.

theCapypara commented 3 months ago

Is your vim using a LSP or something that is actually aware of typestubs? If it's analyzing "real" code only it won't find it.

laoshaw commented 3 months ago

yes I'm using coc-nvim that has coc-pyright installed, it worked fine with other python packages, it can also autocomplete GLib and Gtk methods etc, but the warning (see the screenshot) always stays no matter what I try, not a showstopper though.

Screenshot from 2024-08-04 16-36-03

Vazgen005 commented 2 months ago

I'm having the same problem. I'm using the Pylance extension from VsCode, which uses pyright under the hood. image

theCapypara commented 2 months ago

The typestubs are normal PEP 561 typestubs.

If your editor or type checker does not find them then they are most likely not installed or something is not configured correctly on your end. Did you make sure to read the README and specifiy the config-settings to tell what stubs to generate?

You can try uninstalling the typestubs and reinstalling them, with the config-settings. Make sure to pass --no-cache-dir like explained in the README. But if you do not specify config-settings, there are still default versions used. So it should still work. Still, confirm that the typestubs are actually installed.

After this, check if the typestubs are recognized by pyright itself (without VS Code).

Vazgen005 commented 2 months ago

As @lashawn said, the stubs work fine. The only problem is the warning. That's all. Perhaps the problem is on the pyright side.

theCapypara commented 2 months ago

But if the imports are not resolved then how can the stubs work? Are you sure the warning isn't coming from VSCode itself?

lovetox commented 2 months ago

are you maybe using virtual environments? or conda environments? if yes you need to install the stubs in these environments.

if your stubs work otherwise, maybe open a issue on the pylance or pyright tracker and ask what the problem could be.

other things to check that come to mind

Side Note: gi.require_version() must come before the import, as it makes sure that version 4.0 is imported. Afterwards it does nothing.

Vazgen005 commented 2 months ago

Yes, i use venv. Here's my setup: image I use rye for project management.

Vazgen005 commented 1 month ago

I solved this issue by adding following rule to pyproject.toml, corresponding to pyright documentation.

[tool.pyright]
reportMissingModuleSource = false