microsoft / pylance-release

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

Pylance fails to recognice pkgutil.extend_path on src folders on "python.analysis.extraPaths" #5990

Closed jdrese closed 3 months ago

jdrese commented 3 months ago

Environment data

Code Snippet

"python.analysis.extraPaths": [
     "..path/to/a/src/with/pkgutil.extend_path",
     "..path/to/another/src/extending_namespace" 
]

Repro Steps

  1. Create 2 different folder structures that will have a src folder and a python package name that is the same
  2. On the first python package init file add __path__ = pkgutil.extend_path(__path__, __name__)
  3. Add both python src folder to a workspace "python.analysis.extraPaths" to have auto complete and code intelissence
  4. Try importing python code from both of the folders. One of them will fail depending on the order of paths in "python.analysis.extraPaths"

Expected behavior

Pylance should be able to pick up that the python packages are extending the namespace

Actual behavior

Pylance fails to read content of one of the packages.

Extra note

The python.analisys.autoSearchPaths does work correctly and if you are withing a same workspace that has all the src folders within the space of the same root folder. Things work. But if another workspace is trying to "reference" code from another package it does not.

debonte commented 3 months ago

Pyright (the type checker upon which Pylance is built), does not support dynamic modification of __path__. Here are several old Pyright issues discussing this that have some more detail:

If I misunderstood what you were asking for here, feel free to provide more detail and reopen the issue. In that case, a repo or zip file with a folder structure that reproduces the issue would be helpful.