VSCode Python/Pylance 扩展未在引用中列出方法的动态使用

问题描述 投票:0回答:1

问题描述

当我使用 Pylance 作为 Python 语言服务器时,它不会向我显示使用这些方法的参考。

我将 TestClass1 对象传递给 TestClass2 对象。

如果我要转到classes1.py 文件并点击“查找所有引用”,我只是获取该文件内的引用(这是方法的定义)。

Faulty references to the method in classes1.py

src/folder1/classes1.py:
class TestClass1():
    def __init__(self, word):
        self.word = word
        
    def print(self):
        print(f"Word: {self.word}")
src/folder2/classes2.py:
class TestClass2:
    def __init__(self, printer):
        self.printer = printer

    def testprint(self):
        self.printer.print()
        
main.py:
from src.folder1.classes1 import TestClass1
from src.folder2.classes2 import TestClass2

printer = TestClass1("Hello World")
def main():
    test = TestClass2(printer)
    test.testprint()

if __name__ == "__main__":
    main()

所有文件路径均相对于项目文件夹。

Project Structure

来自 Python 语言服务器 (Pylance) 的调试日志

2024-01-18 23:45:03.318 [info] [Info  - 11:45:03 PM] (21748) Pylance language server 2023.12.1 (pyright a78e2b10) starting
2024-01-18 23:45:03.318 [info] [Info  - 11:45:03 PM] (21748) Server root directory: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist
2024-01-18 23:45:03.329 [info] [Info  - 11:45:03 PM] (21748) Starting service instance "test"
2024-01-18 23:45:03.383 [info] (21748) No configuration file found.
2024-01-18 23:45:03.385 [info] (21748) No pyproject.toml file found.
2024-01-18 23:45:03.385 [info] [Info  - 11:45:03 PM] (21748) Setting pythonPath for service "test": "c:\Daten\VSCode\Python\test\.venv\Scripts\python.exe"
2024-01-18 23:45:03.386 [info] [Info  - 11:45:03 PM] (21748) Setting environmentName for service "test": "3.12.0 (.venv venv)"
2024-01-18 23:45:03.522 [info] [Info  - 11:45:03 PM] (21748) Assuming Python version 3.12
2024-01-18 23:45:03.522 [info] (21748) Assuming Python platform Windows
2024-01-18 23:45:03.708 [info] [Info  - 11:45:03 PM] (21748) Search paths for C:\Daten\VSCode\Python\test
2024-01-18 23:45:03.708 [info] [Info  - 11:45:03 PM] (21748)   C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib
2024-01-18 23:45:03.709 [info] [Info  - 11:45:03 PM] (21748)   C:\Daten\VSCode\Python\test
2024-01-18 23:45:03.709 [info] [Info  - 11:45:03 PM] (21748)   C:\Daten\VSCode\Python\test\src
2024-01-18 23:45:03.709 [info] [Info  - 11:45:03 PM] (21748)   C:\Daten\VSCode\Python\test\typings
2024-01-18 23:45:03.709 [info] [Info  - 11:45:03 PM] (21748)   C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stubs\...
2024-01-18 23:45:03.710 [info] [Info  - 11:45:03 PM] (21748)   C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\bundled\stubs
2024-01-18 23:45:03.710 [info] [Info  - 11:45:03 PM] (21748)   C:\Daten\Programs\Python312\DLLs
2024-01-18 23:45:03.710 [info] [Info  - 11:45:03 PM] (21748)   C:\Daten\Programs\Python312\Lib
2024-01-18 23:45:03.710 [info] [Info  - 11:45:03 PM] (21748)   C:\Daten\Programs\Python312
2024-01-18 23:45:03.711 [info] [Info  - 11:45:03 PM] (21748)   C:\Daten\VSCode\Python\test\.venv
2024-01-18 23:45:03.711 [info] [Info  - 11:45:03 PM] (21748)   C:\Daten\VSCode\Python\test\.venv\Lib\site-packages
2024-01-18 23:45:03.711 [info] [Info  - 11:45:03 PM] (21748) Adding fs watcher for library directories:
 C:\Daten\Programs\Python312\DLLs
C:\Daten\Programs\Python312\Lib
C:\Daten\Programs\Python312
C:\Daten\VSCode\Python\test\.venv
C:\Daten\VSCode\Python\test\.venv\Lib\site-packages
2024-01-18 23:45:03.712 [info] [Info  - 11:45:03 PM] (21748) Adding fs watcher for directories:
 C:\Daten\VSCode\Python\test
2024-01-18 23:45:03.713 [info] (21748) Searching for source files
2024-01-18 23:45:03.722 [info] [Info  - 11:45:03 PM] (21748) Found 3 source files
2024-01-18 23:45:03.722 [info] (21748) pytest configurations: {"message":"script","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-01-18 23:45:03.723 [info] (21748) pytest configurations: {"message":"script","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-01-18 23:45:03.788 [info] (21748) [FG] parsing: C:\Daten\VSCode\Python\test\src\folder1\classes1.py (17ms)
2024-01-18 23:45:03.885 [info] (21748) [FG] parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 3ms] (95ms)
2024-01-18 23:45:03.923 [info] (21748) [FG] binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\builtins.pyi (38ms)
2024-01-18 23:45:03.925 [info] (21748) [FG] binding: C:\Daten\VSCode\Python\test\src\folder1\classes1.py (2ms)
2024-01-18 23:45:04.266 [info] FEHLER: Der Prozess "27548" wurde nicht gefunden.

2024-01-18 23:45:04.277 [info] (21748) pytest configurations: {"message":"request cancelled","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-01-18 23:45:04.312 [info] (21748) [IDX(2)] scan packages C:\Daten\VSCode\Python\test ...
2024-01-18 23:45:04.312 [info] (21748) [IDX(2)]   read stdlib indices (43ms)
2024-01-18 23:45:04.375 [info] (21748) [BG(1)] analyzing: C:\Daten\VSCode\Python\test\src\folder1\classes1.py ...
2024-01-18 23:45:04.375 [info] (21748) [BG(1)]   parsing: C:\Daten\VSCode\Python\test\src\folder1\classes1.py (95ms)
2024-01-18 23:45:04.474 [info] (21748) [BG(1)]   parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 4ms] (98ms)
2024-01-18 23:45:04.503 [info] (21748) [BG(1)]   binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\builtins.pyi (28ms)
2024-01-18 23:45:04.504 [info] (21748) [BG(1)]   binding: C:\Daten\VSCode\Python\test\src\folder1\classes1.py (0ms)
2024-01-18 23:45:04.553 [info] (21748) [BG(1)]   checking: C:\Daten\VSCode\Python\test\src\folder1\classes1.py ...
2024-01-18 23:45:04.553 [info] (21748) [BG(1)]     parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing.pyi [fs read 1ms] (32ms)
2024-01-18 23:45:04.567 [info] (21748) [BG(1)]     binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing.pyi (10ms)
2024-01-18 23:45:04.575 [info] (21748) [BG(1)]     parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing_extensions.pyi [fs read 0ms] (5ms)
2024-01-18 23:45:04.578 [info] (21748) [BG(1)]     binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing_extensions.pyi (3ms)
2024-01-18 23:45:04.614 [info] (21748) [BG(1)]     parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi [fs read 1ms] (11ms)
2024-01-18 23:45:04.614 [info] (21748) [BG(1)]     binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi (3ms)
2024-01-18 23:45:04.623 [info] (21748) [BG(1)]     parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\types.pyi [fs read 1ms] (10ms)
2024-01-18 23:45:04.633 [info] (21748) [BG(1)]     binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\types.pyi (10ms)
2024-01-18 23:45:04.644 [info] (21748) [BG(1)]     parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\abc.pyi [fs read 1ms] (2ms)
2024-01-18 23:45:04.645 [info] (21748) [BG(1)]     binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\abc.pyi (1ms)
2024-01-18 23:45:04.677 [info] (21748) [BG(1)]   checking: C:\Daten\VSCode\Python\test\src\folder1\classes1.py (173ms)
2024-01-18 23:45:04.677 [info] (21748) [BG(1)] analyzing: C:\Daten\VSCode\Python\test\src\folder1\classes1.py (398ms)
2024-01-18 23:45:04.682 [info] (21748) [BG(1)] getSemanticTokens full at C:\Daten\VSCode\Python\test\src\folder1\classes1.py (4ms)
2024-01-18 23:45:04.684 [info] (21748) [BG(1)] getSemanticTokens range 0:0 - 5:35 at C:\Daten\VSCode\Python\test\src\folder1\classes1.py (2ms)
2024-01-18 23:45:04.717 [info] (21748) [FG] parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing.pyi [fs read 1ms] (24ms)
2024-01-18 23:45:04.728 [info] (21748) [FG] binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing.pyi (10ms)
2024-01-18 23:45:04.737 [info] (21748) [FG] parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing_extensions.pyi [fs read 1ms] (7ms)
2024-01-18 23:45:04.740 [info] (21748) [FG] binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing_extensions.pyi (3ms)
2024-01-18 23:45:04.771 [info] (21748) [FG] parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi [fs read 1ms] (9ms)
2024-01-18 23:45:04.771 [info] (21748) [FG] binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi (2ms)
2024-01-18 23:45:04.781 [info] (21748) [FG] parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\types.pyi [fs read 1ms] (10ms)
2024-01-18 23:45:04.789 [info] (21748) [FG] binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\types.pyi (7ms)
2024-01-18 23:45:04.795 [info] (21748) [FG] parsing: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\abc.pyi [fs read 0ms] (1ms)
2024-01-18 23:45:04.797 [info] (21748) [FG] binding: C:\Users\mschee1\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\abc.pyi (2ms)
2024-01-18 23:45:04.803 [info] (21748) [BG(1)] indexing: C:\Daten\VSCode\Python\test\main.py ...
2024-01-18 23:45:04.803 [info] (21748) [BG(1)]   parsing: C:\Daten\VSCode\Python\test\main.py [fs read 1ms] (7ms)
2024-01-18 23:45:04.803 [info] (21748) [BG(1)]   binding: C:\Daten\VSCode\Python\test\main.py (0ms)
2024-01-18 23:45:04.804 [info] (21748) [BG(1)] indexing: C:\Daten\VSCode\Python\test\main.py [found 2] (8ms)
2024-01-18 23:45:04.804 [info] (21748) [BG(1)] indexing: C:\Daten\VSCode\Python\test\src\folder1\classes1.py [found 1] (1ms)
2024-01-18 23:45:04.804 [info] (21748) [BG(1)] indexing: C:\Daten\VSCode\Python\test\src\folder2\classes2.py ...
2024-01-18 23:45:04.805 [info] (21748) [BG(1)]   parsing: C:\Daten\VSCode\Python\test\src\folder2\classes2.py [fs read 1ms] (5ms)
2024-01-18 23:45:04.805 [info] (21748) [BG(1)]   binding: C:\Daten\VSCode\Python\test\src\folder2\classes2.py (0ms)
2024-01-18 23:45:04.805 [info] (21748) [BG(1)] indexing: C:\Daten\VSCode\Python\test\src\folder2\classes2.py [found 1] (6ms)
2024-01-18 23:45:04.806 [info] (21748) pytest configurations: {"message":"script","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-01-18 23:45:05.059 [info] (21748) IntelliCode model c:\Users\mschee1\.vscode\extensions\visualstudioexptteam.vscodeintellicode-1.2.30\dist\bundledModels\python_intellisense-members-lstm-pylance
2024-01-18 23:45:05.198 [info] (21748) [IDX(2)] scan packages C:\Daten\VSCode\Python\test (904ms)
2024-01-18 23:45:05.266 [info] (21748) [IDX(2)] index packages C:\Daten\VSCode\Python\test ...
2024-01-18 23:45:05.266 [info] (21748) [IDX(2)]   index execution environment C:\Daten\VSCode\Python\test ...
2024-01-18 23:45:05.267 [info] (21748) [IDX(2)]     indexing: C:\Daten\Programs\Python312\Lib\concurrent\__init__.py [skipped: no '__all__' defined] (1ms)
2024-01-18 23:45:05.267 [info] (21748) [IDX(2)]     indexing: C:\Daten\Programs\Python312\Lib\idlelib\__init__.py [skipped: no '__all__' defined] (0ms)
2024-01-18 23:45:05.267 [info] (21748) [IDX(2)]     indexing: C:\Daten\Programs\Python312\Lib\lib2to3\__init__.py [skipped: no '__all__' defined] (1ms)
2024-01-18 23:45:05.267 [info] (21748) [IDX(2)]     indexing: C:\Daten\Programs\Python312\Lib\pydoc_data\__init__.py [skipped: no '__all__' defined] (0ms)
2024-01-18 23:45:05.268 [info] (21748) [IDX(2)]     indexing: C:\Daten\Programs\Python312\Lib\test\__init__.py [skipped: no '__all__' defined] (1ms)
2024-01-18 23:45:05.268 [info] (21748) [IDX(2)]     indexing: C:\Daten\Programs\Python312\Lib\turtledemo\__init__.py [skipped: no '__all__' defined] (0ms)
2024-01-18 23:45:05.269 [info] (21748) [IDX(2)]     indexing: C:\Daten\Programs\Python312\Lib\urllib\__init__.py [skipped: no '__all__' defined] (1ms)
2024-01-18 23:45:05.270 [info] (21748) [IDX(2)]     indexing: C:\Daten\Programs\Python312\Lib\wsgiref\__init__.py [skipped: no '__all__' defined] (1ms)
2024-01-18 23:45:05.270 [info] (21748) [IDX(2)]     indexing: C:\Daten\Programs\Python312\Lib\xmlrpc\__init__.py [skipped: no '__all__' defined] (0ms)
2024-01-18 23:45:05.271 [info] (21748) [IDX(2)]     indexing: C:\Daten\Programs\Python312\Lib\__phello__\__init__.py [skipped: no '__all__' defined] (1ms)
2024-01-18 23:45:05.271 [info] (21748) [IDX(2)]     indexing: C:\Daten\VSCode\Python\test\.venv\Lib\site-packages\shiboken6_generator\__init__.py [skipped: no '__all__' defined] (0ms)
2024-01-18 23:45:05.272 [info] (21748) [IDX(2)]   index execution environment C:\Daten\VSCode\Python\test [found 23 in 4 files] (18ms)
2024-01-18 23:45:05.272 [info] (21748) [IDX(2)] index packages C:\Daten\VSCode\Python\test [found 23 in 1 exec envs] (23ms)
2024-01-18 23:45:05.771 [info] (21748) Loading ONNX runtime...
2024-01-18 23:45:05.772 [info] (21748) Loaded ONNX runtime. Creating IntelliCode session...
2024-01-18 23:45:05.933 [info] 2024-01-18 23:45:05.924077 [I:onnxruntime:, inference_session.cc:328 operator()] Flush-to-zero and denormal-as-zero are off

2024-01-18 23:45:05.933 [info] 2024-01-18 23:45:05.925846 [I:onnxruntime:, inference_session.cc:336 ConstructorCommon] Creating and using per session threadpools since use_per_session_threads_ is true
2024-01-18 23:45:05.925986 [I:onnxruntime:, inference_session.cc:354 ConstructorCommon] Dynamic block base set to 0

2024-01-18 23:45:06.000 [info] 2024-01-18 23:45:05.992547 [I:onnxruntime:, inference_session.cc:1400 Initialize] Initializing session.

2024-01-18 23:45:06.000 [info] 2024-01-18 23:45:05.992816 [I:onnxruntime:, inference_session.cc:1437 Initialize] Adding default CPU execution provider.

2024-01-18 23:45:06.103 [info] 2024-01-18 23:45:06.095013 [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-01-18 23:45:06.122 [info] 2024-01-18 23:45:06.114061 [I:onnxruntime:, graph.cc:3619 CleanUnusedInitializersAndNodeArgs] Removing NodeArg 'rnn/transpose'. It is no longer used by any node.

2024-01-18 23:45:06.122 [info] 2024-01-18 23:45:06.114316 [I:onnxruntime:, graph.cc:3619 CleanUnusedInitializersAndNodeArgs] Removing NodeArg 'Gather_out0'. It is no longer used by any node.
2024-01-18 23:45:06.114458 [I:onnxruntime:, graph.cc:3619 CleanUnusedInitializersAndNodeArgs] Removing NodeArg 'Transpose_out0'. It is no longer used by any node.

2024-01-18 23:45:06.191 [info] 2024-01-18 23:45:06.183287 [V:onnxruntime:, session_state.cc:1142 VerifyEachNodeIsAssignedToAnEp] Node placements
2024-01-18 23:45:06.183828 [V:onnxruntime:, session_state.cc:1145 VerifyEachNodeIsAssignedToAnEp]  All nodes placed on [CPUExecutionProvider]. Number of nodes: 56

2024-01-18 23:45:06.210 [info] 2024-01-18 23:45:06.202187 [V:onnxruntime:, session_state.cc:126 CreateGraphInfo] SaveMLValueNameIndexMapping

2024-01-18 23:45:06.210 [info] 2024-01-18 23:45:06.202907 [V:onnxruntime:, session_state.cc:172 CreateGraphInfo] Done saving OrtValue mappings.

2024-01-18 23:45:06.211 [info] 2024-01-18 23:45:06.203577 [I:onnxruntime:, allocation_planner.cc:2401 CreateGraphPartitioner] Use DeviceBasedPartition as default

2024-01-18 23:45:06.215 [info] 2024-01-18 23:45:06.207844 [I:onnxruntime:, session_state_utils.cc:201 SaveInitializedTensors] Saving initialized tensors.

2024-01-18 23:45:06.273 [info] 2024-01-18 23:45:06.263265 [I:onnxruntime:, session_state_utils.cc:345 SaveInitializedTensors] Done saving initialized tensors

2024-01-18 23:45:06.287 [info] 2024-01-18 23:45:06.279448 [I:onnxruntime:, inference_session.cc:1774 Initialize] Session successfully initialized.

2024-01-18 23:45:06.288 [info] (21748) Created IntelliCode session.
2024-01-18 23:45:06.289 [info] (21748) Initialize deeplearning succeeded
2024-01-18 23:45:15.387 [info] (21748) [FG] parsing: C:\Daten\VSCode\Python\test\main.py [fs read 0ms] (6ms)
2024-01-18 23:45:15.389 [info] (21748) [FG] binding: C:\Daten\VSCode\Python\test\main.py (1ms)
2024-01-18 23:45:15.395 [info] (21748) [FG] parsing: C:\Daten\VSCode\Python\test\src\folder2\classes2.py [fs read 1ms] (2ms)
2024-01-18 23:45:15.396 [info] (21748) [FG] binding: C:\Daten\VSCode\Python\test\src\folder2\classes2.py (0ms)

当我切换到绝地语言服务器时,它工作得很好。但另一方面,绝地武士并不是真正可用,因为我在尝试重命名符号时遇到错误。在将代码重构为新符号名称的每个文件中,它只是在代码之间添加新行。

Expected Result from Pylance

我使用了一些设置,但无论我操作哪个设置,都不起作用。

{
    "python.languageServer": "Pylance",
    "python.analysis.autoImportCompletions": true,
    "python.analysis.typeCheckingMode": "basic"
}
visual-studio-code intellisense pylance
1个回答
0
投票

我收到了 Pylance Devs 的回复:

Pylance 将 TestClass2 中的 self.printer.print() 调用显示为 引用 TestClass1.print() 您需要告诉它打印机 应该始终是通过类型注释的 TestClass1 的实例。 例如:

class TestClass2:
    def __init__(self, printer: TestClass1):

如果没有这个,Pylance 会将打印机视为 Any 类型。它不假设 这只是因为您对 TestClass2() 的唯一调用传递了一个 TestClass1对象,因此只能初始化TestClass2 通过传入 TestClass1 对象。

因此这不是一个错误,而是设计使然。

© www.soinside.com 2019 - 2024. All rights reserved.