pyright icon indicating copy to clipboard operation
pyright copied to clipboard

Unknown import symbol when importing a module and content from module

Open Izaird opened this issue 1 year ago • 5 comments

Describe the bug When importing symbols from gi.repository a unknown symbol error is raised.

Removing the module import makes the error go away

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
❯ pyright config/config.py --verbose
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
Assuming Python version 3.13.1.final.0
Execution environment: python
  Extra paths:
    (none)
  Python version: 3.13.1.final.0
  Python platform: Linux
  Search paths:
    /usr/lib/node_modules/pyright/dist/typeshed-fallback/stdlib
    /home/izaird/.config/Ax-Shell
    /home/izaird/.config/Ax-Shell/typings
    /usr/lib/node_modules/pyright/dist/typeshed-fallback/stubs/...
    /usr/lib/python3.13
    /usr/lib/python3.13/lib-dynload
    /usr/lib/python3.13/site-packages
Found 1 source file
Could not import 'IPython.lib.pretty' in file '/usr/lib/python3.13/site-packages/PIL/Image.py'
  Looking in stubPath 'file:///home/izaird/.config/Ax-Shell/typings'
  Attempting to resolve stub package using root path 'file:///home/izaird/.config/Ax-Shell/typings'
  Attempting to resolve using root path 'file:///home/izaird/.config/Ax-Shell/typings'
  Looking in root directory of execution environment 'file:///home/izaird/.config/Ax-Shell'
  Attempting to resolve stub package using root path 'file:///home/izaird/.config/Ax-Shell'
  Attempting to resolve using root path 'file:///home/izaird/.config/Ax-Shell'
  Looking for typeshed stdlib path
  Looking for typeshed stdlib path
  Attempting to resolve using root path 'file:///usr/lib/node_modules/pyright/dist/typeshed-fallback/stdlib'
  Typeshed path not found
  Finding python search paths
  Executing interpreter: 'python3'
  Skipping '/usr/lib/python313.zip' because it is not a valid directory
  Received 3 paths from interpreter
    file:///usr/lib/python3.13
    file:///usr/lib/python3.13/lib-dynload
    file:///usr/lib/python3.13/site-packages
  Looking in python search path 'file:///usr/lib/python3.13'
  Attempting to resolve stub package using root path 'file:///usr/lib/python3.13'
  Attempting to resolve using root path 'file:///usr/lib/python3.13'
  Looking in python search path 'file:///usr/lib/python3.13/lib-dynload'
  Attempting to resolve stub package using root path 'file:///usr/lib/python3.13/lib-dynload'
  Attempting to resolve using root path 'file:///usr/lib/python3.13/lib-dynload'
  Looking in python search path 'file:///usr/lib/python3.13/site-packages'
  Attempting to resolve stub package using root path 'file:///usr/lib/python3.13/site-packages'
  Attempting to resolve using root path 'file:///usr/lib/python3.13/site-packages'
  Looking for typeshed third-party path
  Looking for typeshed stubs path
  Typeshed path not found
pyright 1.1.393
/home/izaird/.config/Ax-Shell/config/config.py
  /home/izaird/.config/Ax-Shell/config/config.py:7:27 - error: "Gtk" is unknown import symbol (reportAttributeAccessIssue)
1 error, 0 warnings, 0 informations 
Completed in 0.41sec

VS Code extension or command-line ❯ pyright --version pyright 1.1.393

Izaird avatar Feb 06 '25 07:02 Izaird

This is unlikely to be a bug in pyright, but I'm happy to help you diagnose your issue. I'll need additional information from you, however. What library (package name and version) do I need to install to repro the issue? Also, please provide a code sample that fully repros the problem. In your code sample above, you're referencing the symbol gi in the first line without first importing it, so that sample is incomplete.

erictraut avatar Feb 06 '25 07:02 erictraut

thank you

heres a little bit more of the code its from this repo

the path to this file Ax-Shell/config/config.py

import os
import json
import shutil
import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from PIL import Image
import toml

# Constants
SOURCE_STRING = """
# Ax-Shell
source = ~/.config/Ax-Shell/config/hypr/ax-shell.conf
"""

CONFIG_DIR = os.path.expanduser("~/.config/Ax-Shell")
WALLPAPERS_DIR_DEFAULT = os.path.expanduser("~/.config/Ax-Shell/assets/wallpapers_example")

this is the packages that I'm using

❯ paru -Qi python-gobject
Name            : python-gobject
Version         : 3.50.0-2

I'm on arch linux and this is my kernel version

❯ uname -r 
6.12.12-1-lts

Izaird avatar Feb 06 '25 19:02 Izaird

There is no package named python-gobject published in the pypi registry. There is a gobject package, but the version you've listed above doesn't match. I don't see a requirements.txt or pyproject.toml in your repo, so that doesn't give me any clues. I'll need more information to repro.

erictraut avatar Feb 06 '25 19:02 erictraut

There is no package named python-gobject published in the pypi registry.

Oh sorry that's the way I installed that package for python here is that should be on the pypi registry

❯ pip list | rg PyGObject                       
PyGObject                 3.50.0

I don't see a requirements.txt or pyproject.toml in your repo

The closest thing I found was this on the readme

Python dependencies:
pillow
toml
setproctitle

and on the install.sh these are packages being installed

    python-fabric-git
    python-pillow
    python-setproctitle
    python-toml
    python-watchdog

Also I don't know if this helps but it's working with python-lsp-server

Izaird avatar Feb 06 '25 20:02 Izaird

I'm not able to install PyGObject successfully on my machine. It appears that it is a native library that requires a compiler and build environment to install. I don't have the time to diagnose the installation failures further.

You'll need to diagnose this yourself, but I can give you some tips.

As I mentioned above, this is very unlikely to be a bug in pyright. It's most likely to be caused by a missing stub file or dynamic Python code that will not work with a static analyzer. If it's a compiled library, you will need stub files that describe the type information for each of the modules in the package (or at least the modules that you use in your code). For more details about stub files, refer to this documentation.

I can't offer much more advice without the ability to repro the problem locally and without any further details such as error messages, error logs, etc.

If you have any other questions about import resolution behaviors, feel free to start a thread in the discussion section.

erictraut avatar Feb 06 '25 20:02 erictraut