microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.72k stars 766 forks source link

Auto import doesn't suggest re-exports in workspace files #5654

Closed nineteendo closed 7 months ago

nineteendo commented 8 months ago

Environment data

Code Snippet

# hello/__init__.py
__all__ = ["hello_world"]
from ._hello import hello_world as hello_world
# hello/_hello.py
def hello_world():
    print("Hello, world!")
# main.py
hello_world

Repro Steps

  1. Enable auto-import completions.
  2. Place cursor after hello_world
  3. Cmd+I

Expected behavior

First suggestion is hello_world from hello and can be imported with Tab.

Actual behavior

hello_world is not in the list:

Screenshot 2024-03-19 at 18 37 12

Workaround

This can be fixed with stubs though, but that's annoying if the source files are fully typed:

# hello/__init__.pyi
__all__ = ["hello_world"]
def hello_world():
Screenshot 2024-03-19 at 18 38 52

Logs

2024-03-19 18:41:16.526 [info] (Client) Pylance async client (2024.3.1) started with python extension (2024.2.1)
2024-03-19 18:41:17.771 [info] [Info  - 18:41:17] (8438) Pylance language server 2024.3.1 (pyright version 1.1.351, commit 87171fbf) starting
2024-03-19 18:41:17.772 [info] [Info  - 18:41:17] (8438) Server root directory: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist
2024-03-19 18:41:17.777 [info] [Info  - 18:41:17] (8438) Starting service instance "tmp"
2024-03-19 18:41:17.855 [info] (8438) No configuration file found.
2024-03-19 18:41:17.855 [info] (8438) No pyproject.toml file found.
2024-03-19 18:41:17.856 [info] [Info  - 18:41:17] (8438) Setting pythonPath for service "tmp": "/usr/local/bin/python3"
2024-03-19 18:41:17.860 [info] [Info  - 18:41:17] (8438) Setting environmentName for service "tmp": "3.11.7 (global)"
2024-03-19 18:41:17.913 [info] [Info  - 18:41:17] (8438) Assuming Python version 3.11
2024-03-19 18:41:17.914 [info] (8438) Assuming Python platform Darwin
2024-03-19 18:41:17.984 [info] [Info  - 18:41:17] (8438) Search paths for file:///Users/wannes/Desktop/tmp
2024-03-19 18:41:17.984 [info] [Info  - 18:41:17] (8438)   /Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib
2024-03-19 18:41:17.984 [info] [Info  - 18:41:17] (8438)   /Users/wannes/Desktop/tmp
2024-03-19 18:41:17.985 [info] [Info  - 18:41:17] (8438)   /Users/wannes/Desktop/tmp/typings
2024-03-19 18:41:17.985 [info] [Info  - 18:41:17] (8438)   /Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stubs/...
2024-03-19 18:41:17.985 [info] [Info  - 18:41:17] (8438)   /Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/bundled/stubs
2024-03-19 18:41:17.985 [info] [Info  - 18:41:17] (8438)   /usr/local/Cellar/python@3.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11
2024-03-19 18:41:17.985 [info] [Info  - 18:41:17] (8438)   /usr/local/Cellar/python@3.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload
2024-03-19 18:41:17.985 [info] [Info  - 18:41:17] (8438)   /Users/wannes/Library/Python/3.11/lib/python/site-packages
2024-03-19 18:41:17.985 [info] [Info  - 18:41:17] (8438)   /usr/local/lib/python3.11/site-packages
2024-03-19 18:41:17.985 [info] [Info  - 18:41:17] (8438)   /usr/local/opt/python-tk@3.11/libexec
2024-03-19 18:41:17.986 [info] [Info  - 18:41:17] (8438) Adding fs watcher for library directories:
 file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11
file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload
file:///Users/wannes/Library/Python/3.11/lib/python/site-packages
file:///usr/local/lib/python3.11/site-packages
file:///usr/local/opt/python-tk%403.11/libexec
2024-03-19 18:41:17.986 [info] [Info  - 18:41:17] (8438) Adding fs watcher for directories:
 file:///Users/wannes/Desktop/tmp
2024-03-19 18:41:17.986 [info] (8438) Searching for source files
2024-03-19 18:41:17.993 [info] [Info  - 18:41:17] (8438) Found 3 source files
2024-03-19 18:41:17.994 [info] (8438) pytest configurations: {"message":"request cancelled","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-03-19 18:41:17.994 [info] (8438) pytest configurations: {"message":"script","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-03-19 18:41:18.036 [info] (8438) [FG] parsing: file:///Users/wannes/Desktop/tmp/main.py (34ms)
2024-03-19 18:41:18.118 [info] (8438) [FG] parsing: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 3ms] (80ms)
2024-03-19 18:41:18.179 [info] (8438) [FG] binding: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/builtins.pyi (53ms)
2024-03-19 18:41:18.180 [info] (8438) [FG] binding: file:///Users/wannes/Desktop/tmp/main.py (0ms)
2024-03-19 18:41:18.349 [info] [Info  - 18:41:18] (8438) Background analysis(1) root directory: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist
2024-03-19 18:41:18.353 [info] [Info  - 18:41:18] (8438) Background analysis(1) started
2024-03-19 18:41:18.411 [info] (8438) pytest configurations: {"message":"request cancelled","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-03-19 18:41:18.478 [info] (8438) [BG(1)] analyzing: file:///Users/wannes/Desktop/tmp/main.py ...
2024-03-19 18:41:18.478 [info] (8438) [BG(1)]   parsing: file:///Users/wannes/Desktop/tmp/main.py (56ms)
2024-03-19 18:41:18.525 [info] [Info  - 18:41:18] (8438) Indexer background runner(2) root directory: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist (index)
2024-03-19 18:41:18.526 [info] [Info  - 18:41:18] (8438) Indexing(2) started
2024-03-19 18:41:18.538 [info] (8438) [BG(1)]   parsing: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 2ms] (59ms)
2024-03-19 18:41:18.566 [info] (8438) [BG(1)]   binding: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/builtins.pyi (28ms)
2024-03-19 18:41:18.567 [info] (8438) [BG(1)]   binding: file:///Users/wannes/Desktop/tmp/main.py (0ms)
2024-03-19 18:41:18.574 [info] (8438) [IDX(2)] scan packages file:///Users/wannes/Desktop/tmp ...
2024-03-19 18:41:18.576 [info] (8438) [IDX(2)]   read stdlib indices (42ms)
2024-03-19 18:41:18.604 [info] (8438) [BG(1)]   checking: file:///Users/wannes/Desktop/tmp/main.py ...
2024-03-19 18:41:18.605 [info] (8438) [BG(1)]     parsing: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/typing.pyi [fs read 0ms] (20ms)
2024-03-19 18:41:18.613 [info] (8438) [BG(1)]     binding: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/typing.pyi (9ms)
2024-03-19 18:41:18.633 [info] (8438) [BG(1)]     parsing: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/types.pyi [fs read 1ms] (8ms)
2024-03-19 18:41:18.638 [info] (8438) [BG(1)]     binding: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/types.pyi (4ms)
2024-03-19 18:41:18.655 [info] (8438) [BG(1)]     parsing: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 0ms] (7ms)
2024-03-19 18:41:18.656 [info] (8438) [BG(1)]     binding: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (2ms)
2024-03-19 18:41:18.665 [info] (8438) [BG(1)]     parsing: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/abc.pyi [fs read 0ms] (1ms)
2024-03-19 18:41:18.665 [info] (8438) [BG(1)]     binding: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/abc.pyi (0ms)
2024-03-19 18:41:18.668 [info] (8438) [BG(1)]   checking: file:///Users/wannes/Desktop/tmp/main.py (98ms)
2024-03-19 18:41:18.669 [info] (8438) [BG(1)] analyzing: file:///Users/wannes/Desktop/tmp/main.py (247ms)
2024-03-19 18:41:18.679 [info] (8438) [BG(1)] SemanticTokens range 0:0 - 1:0 at file:///Users/wannes/Desktop/tmp/main.py (2ms)
2024-03-19 18:41:18.680 [info] (8438) [BG(1)] SemanticTokens full at file:///Users/wannes/Desktop/tmp/main.py (0ms)
2024-03-19 18:41:18.684 [info] (8438) [BG(1)] indexing: file:///Users/wannes/Desktop/tmp/main.py [found 0] (1ms)
2024-03-19 18:41:18.685 [info] (8438) Indexing Done: file:///Users/wannes/Desktop/tmp/main.py
2024-03-19 18:41:18.686 [info] (8438) [BG(1)] indexing: file:///Users/wannes/Desktop/tmp/hello/__init__.py ...
2024-03-19 18:41:18.686 [info] (8438) [BG(1)]   parsing: file:///Users/wannes/Desktop/tmp/hello/__init__.py [fs read 0ms] (1ms)
2024-03-19 18:41:18.686 [info] (8438) [BG(1)]   binding: file:///Users/wannes/Desktop/tmp/hello/__init__.py (0ms)
2024-03-19 18:41:18.687 [info] (8438) [BG(1)] indexing: file:///Users/wannes/Desktop/tmp/hello/__init__.py [found 1] (2ms)
2024-03-19 18:41:18.687 [info] (8438) Indexing Done: file:///Users/wannes/Desktop/tmp/hello/__init__.py
2024-03-19 18:41:18.688 [info] (8438) [BG(1)] indexing: file:///Users/wannes/Desktop/tmp/hello/_hello.py ...
2024-03-19 18:41:18.688 [info] (8438) [BG(1)]   parsing: file:///Users/wannes/Desktop/tmp/hello/_hello.py [fs read 0ms] (0ms)
2024-03-19 18:41:18.688 [info] (8438) [BG(1)]   binding: file:///Users/wannes/Desktop/tmp/hello/_hello.py (0ms)
2024-03-19 18:41:18.688 [info] (8438) [BG(1)] indexing: file:///Users/wannes/Desktop/tmp/hello/_hello.py [found 1] (0ms)
2024-03-19 18:41:18.688 [info] (8438) Indexing Done: file:///Users/wannes/Desktop/tmp/hello/_hello.py
2024-03-19 18:41:18.690 [info] (8438) pytest configurations: {"message":"script","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-03-19 18:41:18.705 [info] [Info  - 18:41:18] (8438) SourceFile: Received fs event 'add' for path '/Users/wannes/Desktop/tmp/.mypy_cache/3.11/@plugins_snapshot.json'
2024-03-19 18:41:18.706 [info] [Info  - 18:41:18] (8438) SourceFile: Received fs event 'change' for path '/Users/wannes/Desktop/tmp/.mypy_cache/3.11/@plugins_snapshot.json.d81e740a04394df2'
2024-03-19 18:41:18.930 [info] (8438) [IDX(2)] scan packages file:///Users/wannes/Desktop/tmp (400ms)
2024-03-19 18:41:18.931 [info] [Info  - 18:41:18] (8438) scanned(2) 17 files over 1 exec env
2024-03-19 18:41:19.138 [info] (8438) [BG(1)] SemanticTokens delta previousResultId:1710870078672 at file:///Users/wannes/Desktop/tmp/main.py (0ms)
2024-03-19 18:41:19.518 [info] (8438) [IDX(2)] index packages file:///Users/wannes/Desktop/tmp ...
2024-03-19 18:41:19.518 [info] (8438) [IDX(2)]   index execution environment file:///Users/wannes/Desktop/tmp ...
2024-03-19 18:41:19.519 [info] (8438) [IDX(2)]     indexing: file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/__phello__/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.519 [info] (8438) [IDX(2)]     indexing: file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/concurrent/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.520 [info] (8438) [IDX(2)]     indexing: file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/distutils/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.520 [info] (8438) [IDX(2)]     indexing: file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/idlelib/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.520 [info] (8438) [IDX(2)]     indexing: file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib2to3/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.520 [info] (8438) [IDX(2)]     indexing: file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pydoc_data/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.521 [info] (8438) [IDX(2)]     indexing: file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/test/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.521 [info] (8438) [IDX(2)]     indexing: file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/turtledemo/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.521 [info] (8438) [IDX(2)]     indexing: file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.521 [info] (8438) [IDX(2)]     indexing: file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/wsgiref/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.522 [info] (8438) [IDX(2)]     indexing: file:///usr/local/Cellar/python%403.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/xmlrpc/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.522 [info] (8438) [IDX(2)]     indexing: file:///usr/local/lib/python3.11/site-packages/wheel/__init__.py [skipped: no '__all__' defined] (0ms)
2024-03-19 18:41:19.525 [info] (8438) [IDX(2)]   index execution environment file:///Users/wannes/Desktop/tmp [found 828 in 5 files] (11ms)
2024-03-19 18:41:19.525 [info] (8438) [IDX(2)] index packages file:///Users/wannes/Desktop/tmp [found 828 in 1 exec envs] (18ms)
2024-03-19 18:41:19.526 [info] [Info  - 18:41:19] (8438) indexed(2) 5 files over 1 exec env
2024-03-19 18:41:19.562 [info] [Info  - 18:41:19] (8438) Indexing finished(2).
2024-03-19 18:41:21.029 [info] (8438) [FG] parsing: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/typing.pyi [fs read 0ms] (14ms)
2024-03-19 18:41:21.040 [info] (8438) [FG] binding: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/typing.pyi (10ms)
2024-03-19 18:41:21.060 [info] (8438) [FG] parsing: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/types.pyi [fs read 1ms] (11ms)
2024-03-19 18:41:21.064 [info] (8438) [FG] binding: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/types.pyi (4ms)
2024-03-19 18:41:21.075 [info] (8438) [FG] parsing: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 0ms] (5ms)
2024-03-19 18:41:21.078 [info] (8438) [FG] binding: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (2ms)
2024-03-19 18:41:21.085 [info] (8438) [FG] parsing: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/abc.pyi [fs read 0ms] (1ms)
2024-03-19 18:41:21.086 [info] (8438) [FG] binding: file:///Users/wannes/.vscode/extensions/ms-python.vscode-pylance-2024.3.1/dist/typeshed-fallback/stdlib/abc.pyi (1ms)
2024-03-19 18:41:21.092 [info] (8438) IntelliCode model /Users/wannes/.vscode/extensions/visualstudioexptteam.vscodeintellicode-1.3.1/dist/bundledModels/python_intellisense-members-lstm-pylance
2024-03-19 18:41:21.694 [info] (8438) Loading ONNX runtime...
2024-03-19 18:41:21.695 [info] (8438) Loaded ONNX runtime. Creating IntelliCode session...
2024-03-19 18:41:21.815 [info] 2024-03-19 18:41:21.814731 [I:onnxruntime:, inference_session.cc:328 operator()] Flush-to-zero and denormal-as-zero are off

2024-03-19 18:41:21.816 [info] 2024-03-19 18:41:21.815720 [I:onnxruntime:, inference_session.cc:336 ConstructorCommon] Creating and using per session threadpools since use_per_session_threads_ is true
2024-03-19 18:41:21.815828 [I:onnxruntime:, inference_session.cc:354 ConstructorCommon] Dynamic block base set to 0

2024-03-19 18:41:21.860 [info] 2024-03-19 18:41:21.860427 [I:onnxruntime:, inference_session.cc:1400 Initialize] Initializing session.

2024-03-19 18:41:21.861 [info] 2024-03-19 18:41:21.860537 [I:onnxruntime:, inference_session.cc:1437 Initialize] Adding default CPU execution provider.

2024-03-19 18:41:21.899 [info] 2024-03-19 18:41:21.899240 [I:onnxruntime:, graph.cc:3556 CleanUnusedInitializersAndNodeArgs] Removing initializer 'pred_embedding_to_output_layer/einsum_1/mul/x:0'. It is no longer used by any node.

2024-03-19 18:41:21.908 [info] 2024-03-19 18:41:21.907588 [I:onnxruntime:, graph.cc:3619 CleanUnusedInitializersAndNodeArgs] Removing NodeArg 'rnn/transpose'. It is no longer used by any node.
2024-03-19 18:41:21.907763 [I:onnxruntime:, graph.cc:3619 CleanUnusedInitializersAndNodeArgs] Removing NodeArg 'Gather_out0'. It is no longer used by any node.
2024-03-19 18:41:21.907856 [I:onnxruntime:, graph.cc:3619 CleanUnusedInitializersAndNodeArgs] Removing NodeArg 'Transpose_out0'. It is no longer used by any node.

2024-03-19 18:41:21.941 [info] 2024-03-19 18:41:21.940898 [V:onnxruntime:, session_state.cc:1142 VerifyEachNodeIsAssignedToAnEp] Node placements
2024-03-19 18:41:21.941042 [V:onnxruntime:, session_state.cc:1145 VerifyEachNodeIsAssignedToAnEp]  All nodes placed on [CPUExecutionProvider]. Number of nodes: 56

2024-03-19 18:41:21.947 [info] 2024-03-19 18:41:21.947103 [V:onnxruntime:, session_state.cc:126 CreateGraphInfo] SaveMLValueNameIndexMapping
2024-03-19 18:41:21.947360 [V:onnxruntime:, session_state.cc:172 CreateGraphInfo] Done saving OrtValue mappings.

2024-03-19 18:41:21.948 [info] 2024-03-19 18:41:21.947696 [I:onnxruntime:, allocation_planner.cc:2401 CreateGraphPartitioner] Use DeviceBasedPartition as default

2024-03-19 18:41:21.951 [info] 2024-03-19 18:41:21.949989 [I:onnxruntime:, session_state_utils.cc:201 SaveInitializedTensors] Saving initialized tensors.

2024-03-19 18:41:21.974 [info] 2024-03-19 18:41:21.974203 [I:onnxruntime:, session_state_utils.cc:345 SaveInitializedTensors] Done saving initialized tensors

2024-03-19 18:41:21.978 [info] 2024-03-19 18:41:21.977862 [I:onnxruntime:, inference_session.cc:1774 Initialize] Session successfully initialized.

2024-03-19 18:41:21.979 [info] (8438) Created IntelliCode session.
2024-03-19 18:41:21.979 [info] (8438) Initialize deeplearning succeeded
StellaHuang95 commented 8 months ago

Thanks for your issue. If you look at the os.py module, the assignment of the path alias symbol is dynamic and platform dependent. In typeshed, which provides type hints for static type checking, path is defined as a module(path.pyi), while in actual library, path is treated as a symbol. This discrepancy and the dynamic behavior may lead static type checkers to struggle with associating the two so it drops that info somewhere.

erictraut commented 8 months ago

@nineteendo, any such change would need to be made to the typeshed stub file os/__init__.pyi here. Pylance / pyright consume these typeshed stubs, as do many other Python type checkers and static analysis tools.

erictraut commented 8 months ago

I'm saying that pyright (the type checker that underlies pylance) has no knowledge of path or path.os. Its behavior is dictated entirely by the type stubs that are provided by typeshed. If you want the behavior to change with regard to os.path, this would require a modification to the typeshed stubs. I don't think there's anything actionable for us to do in pyright or pylance here other than to pull an updated copy of the typeshed stubs if and when such a change is made. I typically pull a new version of the typeshed stubs every two weeks.

I don't know the reason why the typeshed maintainers chose to model os.path the way they did. You would need to explore that with them if you want to suggest a change.

nineteendo commented 8 months ago

@StellaHuang95, @erictraut, the issue is simply that no kind of re-export is ever included in the suggestions, only definitions. I've updated the overview to reflect this. Is this something manageable to fix, or do we need to rely on stubs for every re-export?

erictraut commented 8 months ago

As I said above, Pylance has no specific knowledge of this (or any other) module or its imports. The behavior is driven by the the type information in the type stubs. If you want the behavior to change, the typeshed stubs will need to be modified accordingly. You can file a bug report or a PR in the typeshed project if you'd like.

StellaHuang95 commented 8 months ago

Hope Eric answers your question about os.path. As for your other question about auto-import not suggesting re-exports, we will investigate to see what's going on. Seems like re-export should work in this case. If it's in the __all__, it should be an exported symbol.

nineteendo commented 8 months ago

@erictraut, I'm talking about imports in the stubs. Pylance seems to only look at the definitions and ignores the re-exports. The same happens with regular Python code.

I was able to get it working by explicitly defining all functions:

Screenshot 2024-03-19 at 20 52 39

It doesn't work with __all__ and import as:

Screenshot 2024-03-19 at 20 56 02
nineteendo commented 7 months ago

@erictraut, @StellaHuang95 are there any updates on this? It's been 2 weeks, do you have any idea what's going on? It's a bit strange, as the regular import completions work perfectly fine. And as the auto-import functionality isn't open source I can't look further into this.

erictraut commented 7 months ago

@nineteendo, you started this thread by reporting a problem with os.path. I've already provided an explanation of why you're seeing the auto-import behavior with os.path. That behavior is intended, and it won't change unless/until there is a corresponding change in the typeshed stubs. If that's the main issue you're reporting here, then we should close this issue because there's nothing actionable for pyright or pylance at this time. You can pursue a change in the typeshed stubs if you'd like, and pyright will pick up any resulting changes when I next refresh the bundled typeshed stubs.

If there's another issue that doesn't involve os.path, let us know.

nineteendo commented 7 months ago

I think you must have misunderstood me.

You started this thread by reporting a problem with os.path.

Yes, but this problem applies to ALL modules using re-exports in particular to my own code. (see the issue description for more details).

I've already provided an explanation of why you're seeing the auto-import behavior with os.path. That behavior is intended, and it won't change unless/until there is a corresponding change in the typeshed stubs.

The problem is that typeshed would be forced to replace all re-exports with explicit definitions if they want to support auto imports in vscode, leading to a lot of duplicate code.

You can pursue a change in the typeshed stubs if you'd like, and pyright will pick up any resulting changes when I next refresh the bundled typeshed stubs.

I will do that, but they'll probably argue that this is a problem for Pylance to fix.

erictraut commented 7 months ago

Yes, but this problem applies to ALL modules using re-exports in particular to my own code. (see the issue description for more details).

The os.path case is very distinct because of the way path is defined in the stubs. You can't generalize from that case.

It sounds like os.path is a red herring. Let's set it aside for now. If you're seeing an issue with other re-exported symbols, then we'll need a repro case. @StellaHuang95, have you been able to repro this issue for cases other than os.path?

StellaHuang95 commented 7 months ago

@erictraut, just tried with this simple example,

# hello/__init__.py
__all__ = ["hello_world", 'function1']
from ._hello import hello_world as hello_world

def function1():
    pass
# hello/_hello.py
def hello_world():
    print("Hello, world!")
# main.py
# type `function1` or `hello_world`

I was able to repro it. I am getting auto-import for function1 but not hello_world. Seems like re-export should work in this case since it's in the __all__ and supposed to be an exported symbol. I can take a look and see what's going wrong.

erictraut commented 7 months ago

@StellaHuang95, it looks like this is because of this code in autoImporter.ts. It looks like this code was introduced 3.5 years ago (on 10/7/2020) by Jake. The comment says "We don't include import alias in auto import for workspace files." I'm not sure why. Maybe it's to avoid duplicate symbols in the completion suggestions? In this case it's not a duplicate because the original symbol definition is not visible by virtue of being defined in a private submodule (i.e. one whose name begins with an underscore).

So this behavior is specific to: 1) workspace modules (as opposed to modules that are installed in the Python environment), 2. a non-private workspace module that imports a symbol from a private workspace submodule and re-exports it as public.

This has nothing to do with type stubs, so that was a complete red herring.

StellaHuang95 commented 7 months ago

This is not supported yet due to perf considerations, but we have added it to our backlog for future enhancements. I am going to close this one as duplicate since it's a subitem of https://github.com/microsoft/pylance-release/issues/5434. Please track that ticket for progress. The order in which we address backlog items depends on their priority and our current bandwidth. We will provide an update below that ticket once it's resolved. Thanks for your patience.

nineteendo commented 7 months ago

This still doesn't explain the behaviour in the stdlib, let's track that further in #5731.