microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.7k stars 769 forks source link

Pyright emits meaningless `reportMissingModuleSource` warnings for submodules of native extension #6269

Open apparebit opened 1 month ago

apparebit commented 1 month ago

The Bug

Pyright emits reportMissingModuleSource warnings for submodules implemented by the same native extension, even though:

Typing stubs, native library, and symbolic library links really should be enough to appease the type checker. If we really want two independent sources of truth for accepting native code, I suggest loading the native library and inspecting that. I must admit I'm not a fan of the symbolic links. I also don't know what to do about Windows. My understanding is that Windows finally does support symbolic links even for files (yay!), but that most Windows users leave that feature disabled because symbolic links might turn into a vector for exploits (oops!).

The Details

While prettypretty does build and run on Windows for CI, the above steps only work with (macOS + Homebrew) or (Linux + APT) for now.

The Gory Details

Verbose command line output Prettypretty uses `# pyright: ignore [reportMissingModuleSource]` comments to avoid useless warnings. I did enable one of them for this run. ``` (.venv) rgrimm@Precious prettypretty % npm run pyright -- --verbose --pythonpath ./.venv/bin/python > pyright > pyright --verbose --pythonpath ./.venv/bin/python Setting pythonPath for service "": "/Users/rgrimm/Work/prettypretty/.venv/bin/python" Loading pyproject.toml file at /Users/rgrimm/Work/prettypretty/pyproject.toml Auto-excluding **/node_modules Auto-excluding **/__pycache__ Auto-excluding **/.* Search paths for file:///Users/rgrimm/Work/prettypretty /Users/rgrimm/Work/prettypretty/node_modules/pyright/dist/typeshed-fallback/stdlib /Users/rgrimm/Work/prettypretty /Users/rgrimm/Work/prettypretty/src /Users/rgrimm/Work/prettypretty/typings /Users/rgrimm/Work/prettypretty/node_modules/pyright/dist/typeshed-fallback/stubs/... /Users/rgrimm/.pyenv/versions/3.12.2/lib/python3.12 /Users/rgrimm/.pyenv/versions/3.12.2/lib/python3.12/lib-dynload /Users/rgrimm/Work/prettypretty/.venv/lib/python3.12/site-packages /Users/rgrimm/Work/prettypretty Found 20 source files Could not resolve source for '.color.style' in file '/Users/rgrimm/Work/prettypretty/prettypretty/color_types.py' Attempting to resolve relative import Attempting to resolve using root path 'file:///Users/rgrimm/Work/prettypretty/prettypretty' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color/__init__.pyi' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color/style.pyi' Attempting to resolve using root path 'file:///Users/rgrimm/Work/prettypretty/prettypretty' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.abi3.so' Resolved with native lib '/Users/rgrimm/Work/prettypretty/prettypretty/color.abi3.so' Did not find file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.pyi' or 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.py' Could not resolve source for '.color.style' in file '/Users/rgrimm/Work/prettypretty/prettypretty/grid.py' Attempting to resolve relative import Attempting to resolve using root path 'file:///Users/rgrimm/Work/prettypretty/prettypretty' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color/__init__.pyi' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color/style.pyi' Attempting to resolve using root path 'file:///Users/rgrimm/Work/prettypretty/prettypretty' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.abi3.so' Resolved with native lib '/Users/rgrimm/Work/prettypretty/prettypretty/color.abi3.so' Did not find file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.pyi' or 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.py' Could not resolve source for '.color.trans' in file '/Users/rgrimm/Work/prettypretty/prettypretty/theme.py' Attempting to resolve relative import Attempting to resolve using root path 'file:///Users/rgrimm/Work/prettypretty/prettypretty' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color/__init__.pyi' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color/trans.pyi' Attempting to resolve using root path 'file:///Users/rgrimm/Work/prettypretty/prettypretty' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.abi3.so' Resolved with native lib '/Users/rgrimm/Work/prettypretty/prettypretty/color.abi3.so' Did not find file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.pyi' or 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.py' Could not resolve source for '.color.style' in file '/Users/rgrimm/Work/prettypretty/prettypretty/terminal.py' Attempting to resolve relative import Attempting to resolve using root path 'file:///Users/rgrimm/Work/prettypretty/prettypretty' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color/__init__.pyi' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color/style.pyi' Attempting to resolve using root path 'file:///Users/rgrimm/Work/prettypretty/prettypretty' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.abi3.so' Resolved with native lib '/Users/rgrimm/Work/prettypretty/prettypretty/color.abi3.so' Did not find file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.pyi' or 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.py' Heap stats: total_memory_size=131072MB, total_free_size=72256MB, total_heap_size=131MB, used_heap_size=106MB, cross_worker_used_heap_size=106MB, total_physical_size=131MB, total_available_size=4036MB, heap_size_limit=4144MB Could not resolve source for '.color.style' in file '/Users/rgrimm/Work/prettypretty/prettypretty/style_extras.py' Attempting to resolve relative import Attempting to resolve using root path 'file:///Users/rgrimm/Work/prettypretty/prettypretty' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color/__init__.pyi' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color/style.pyi' Attempting to resolve using root path 'file:///Users/rgrimm/Work/prettypretty/prettypretty' Resolved import with file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.abi3.so' Resolved with native lib '/Users/rgrimm/Work/prettypretty/prettypretty/color.abi3.so' Did not find file 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.pyi' or 'file:///Users/rgrimm/Work/prettypretty/prettypretty/color.py' pyright 1.1.375 /Users/rgrimm/Work/prettypretty/prettypretty/plot.py /Users/rgrimm/Work/prettypretty/prettypretty/plot.py:26:5 - warning: Import ".color.gamut" could not be resolved from source (reportMissingModuleSource) 0 errors, 1 warning, 0 informations Completed in 1.495sec ```
erictraut commented 1 month ago

The reportMissingModuleSource check arguably doesn't belong in a type checker. It was added by the pylance team for the benefit of their users. It makes more sense when pyright is being used to power a language server.

That said, I agree that this is a bogus error. The check for reportMissingModuleSource should be smart enough to ignore cases where the import statement is referencing a submodule of a native module.

@debonte, @rchiodo could someone from the pylance team please take ownership of this issue since you're the most familiar with the code that implements the reportMissingModuleSource check?

BTW, I was initially unable to repro this bug because I thought it was talking about code that imports the prettypretty library. The bug report is actually talking about type checking the prettypretty code itself. Here are the repro steps:

  1. Clone the prettypretty repo.
  2. Open the style_extras.py file and delete the # pyright: ignore [reportMissingModuleSource] comment.
apparebit commented 1 month ago

BTW, I was initially unable to repro this bug because I thought it was talking about code that imports the prettypretty library. The bug report is actually talking about type checking the prettypretty code itself.

Oops, good point. Thanks for the new and improved repro steps!