microsoft / pylance-release

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

Pylance(reportUndefinedVariable) for self-referential class methods #5190

Closed nqhq-lou closed 11 months ago

nqhq-lou commented 11 months ago

Environment data

Code Snippet

class MyClass:
    def __init__(self,):
        pass
    def myfunc(self,) -> MyClass:
        return self

Expected behavior

Pylance should not report an undefined variable error for a class method that returns an instance of the same class.

Actual behavior

Pylance reports an undefined variable error for a class method that returns an instance of the same class.

image

Logs

2023-12-01 16:18:13.011 [info] [Info  - 4:18:13 PM] (38036) Pylance language server 2023.11.10 (pyright 088ebaa5) starting
2023-12-01 16:18:13.012 [info] [Info  - 4:18:13 PM] (38036) Server root directory: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist
2023-12-01 16:18:13.012 [info] [Info  - 4:18:13 PM] (38036) Starting service instance "pylance_issue"
2023-12-01 16:18:13.060 [info] (38036) No configuration file found.
2023-12-01 16:18:13.060 [info] (38036) No pyproject.toml file found.
2023-12-01 16:18:13.061 [info] [Info  - 4:18:13 PM] (38036) Setting environmentName for service "pylance_issue": "C:\Users\zekun\anaconda3\python.exe"
2023-12-01 16:18:13.065 [info] [Error - 4:18:13 PM] (38036) venvPath C:\Users\zekun\.pyl is not a valid directory.
2023-12-01 16:18:13.065 [info] (38036) Assuming Python platform Windows
2023-12-01 16:18:13.309 [info] [Info  - 4:18:13 PM] (38036) Search paths for C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue
2023-12-01 16:18:13.310 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib
2023-12-01 16:18:13.311 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue
2023-12-01 16:18:13.311 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\${workspaceRoot}\src
2023-12-01 16:18:13.312 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\typings
2023-12-01 16:18:13.312 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stubs\...
2023-12-01 16:18:13.312 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\bundled\stubs
2023-12-01 16:18:13.313 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\miniconda3\DLLs
2023-12-01 16:18:13.313 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\miniconda3\Lib
2023-12-01 16:18:13.313 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\miniconda3
2023-12-01 16:18:13.313 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\miniconda3\Lib\site-packages
2023-12-01 16:18:13.314 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\miniconda3\Lib\site-packages\win32
2023-12-01 16:18:13.315 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\miniconda3\Lib\site-packages\win32\lib
2023-12-01 16:18:13.316 [info] [Info  - 4:18:13 PM] (38036)   C:\Users\zekun\miniconda3\Lib\site-packages\pythonwin
2023-12-01 16:18:13.316 [info] (38036) Searching for source files
2023-12-01 16:18:13.316 [info] [Info  - 4:18:13 PM] (38036) Found 1 source file
2023-12-01 16:18:13.317 [info] (38036) pytest configurations: {"message":"script","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2023-12-01 16:18:13.317 [info] (38036) pytest configurations: {"message":"script","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2023-12-01 16:18:13.339 [info] (38036) [FG] parsing: C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\return_class.py (21ms)
2023-12-01 16:18:13.422 [info] (38036) [FG] parsing: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 3ms] (81ms)
2023-12-01 16:18:13.455 [info] (38036) [FG] binding: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\builtins.pyi (32ms)
2023-12-01 16:18:13.455 [info] (38036) [FG] binding: C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\return_class.py (1ms)
2023-12-01 16:18:13.470 [info] (38036) IntelliCode model c:\Users\zekun\.vscode\extensions\visualstudioexptteam.vscodeintellicode-1.2.30\dist\bundledModels\python_intellisense-members-lstm-pylance
2023-12-01 16:18:13.840 [info] (38036) [IDX(2)] scan packages C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue ...
2023-12-01 16:18:13.840 [info] (38036) [IDX(2)]   read stdlib indices (28ms)
2023-12-01 16:18:14.053 [info] ����: û���ҵ����� "37980"��

2023-12-01 16:18:14.057 [info] (38036) pytest configurations: {"message":"request cancelled","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2023-12-01 16:18:14.450 [info] (38036) [BG(1)] getSemanticTokens full at C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\return_class.py ...
2023-12-01 16:18:14.450 [info] (38036) [BG(1)]   parsing: C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\return_class.py (194ms)
2023-12-01 16:18:14.451 [info] (38036) [BG(1)]   parsing: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 5ms] (158ms)
2023-12-01 16:18:14.451 [info] (38036) [BG(1)]   binding: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\builtins.pyi (34ms)
2023-12-01 16:18:14.451 [info] (38036) [BG(1)]   binding: C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\return_class.py (1ms)
2023-12-01 16:18:14.451 [info] (38036) [BG(1)]   parsing: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\typing.pyi [fs read 1ms] (37ms)
2023-12-01 16:18:14.465 [info] (38036) Loading ONNX runtime...
2023-12-01 16:18:14.466 [info] (38036) Loaded ONNX runtime. Creating IntelliCode session...
2023-12-01 16:18:14.502 [info] (38036) [BG(1)]   binding: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\typing.pyi (12ms)
2023-12-01 16:18:14.503 [info] (38036) [BG(1)]   parsing: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\typing_extensions.pyi [fs read 1ms] (5ms)
2023-12-01 16:18:14.503 [info] (38036) [BG(1)]   binding: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\typing_extensions.pyi (4ms)
2023-12-01 16:18:14.510 [info] (38036) [BG(1)]   parsing: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi [fs read 1ms] (6ms)
2023-12-01 16:18:14.512 [info] (38036) [BG(1)]   binding: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi (2ms)
2023-12-01 16:18:14.534 [info] (38036) [BG(1)]   parsing: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\types.pyi [fs read 1ms] (21ms)
2023-12-01 16:18:14.557 [info] (38036) [BG(1)]   binding: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\types.pyi (7ms)
2023-12-01 16:18:14.559 [info] (38036) [BG(1)]   parsing: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\abc.pyi [fs read 1ms] (2ms)
2023-12-01 16:18:14.559 [info] (38036) [BG(1)]   binding: C:\Users\zekun\.vscode\extensions\ms-python.vscode-pylance-2023.11.10\dist\typeshed-fallback\stdlib\abc.pyi (1ms)
2023-12-01 16:18:14.560 [info] (38036) [BG(1)] getSemanticTokens full at C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\return_class.py (550ms)
2023-12-01 16:18:14.718 [info] 2023-12-01 16:18:14.714095 [I:onnxruntime:, inference_session.cc:328 operator()] Flush-to-zero and denormal-as-zero are off

2023-12-01 16:18:14.718 [info] 2023-12-01 16:18:14.716014 [I:onnxruntime:, inference_session.cc:336 ConstructorCommon] Creating and using per session threadpools since use_per_session_threads_ is true
2023-12-01 16:18:14.716185 [I:onnxruntime:, inference_session.cc:354 ConstructorCommon] Dynamic block base set to 0

2023-12-01 16:18:14.782 [info] 2023-12-01 16:18:14.779503 [I:onnxruntime:, inference_session.cc:1400 Initialize] Initializing session.

2023-12-01 16:18:14.782 [info] 2023-12-01 16:18:14.779696 [I:onnxruntime:, inference_session.cc:1437 Initialize] Adding default CPU execution provider.

2023-12-01 16:18:14.849 [info] 2023-12-01 16:18:14.846983 [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.

2023-12-01 16:18:14.862 [info] 2023-12-01 16:18:14.859195 [I:onnxruntime:, graph.cc:3619 CleanUnusedInitializersAndNodeArgs] Removing NodeArg 'rnn/transpose'. It is no longer used by any node.

2023-12-01 16:18:14.862 [info] 2023-12-01 16:18:14.859356 [I:onnxruntime:, graph.cc:3619 CleanUnusedInitializersAndNodeArgs] Removing NodeArg 'Gather_out0'. It is no longer used by any node.
2023-12-01 16:18:14.859468 [I:onnxruntime:, graph.cc:3619 CleanUnusedInitializersAndNodeArgs] Removing NodeArg 'Transpose_out0'. It is no longer used by any node.

2023-12-01 16:18:14.908 [info] 2023-12-01 16:18:14.905239 [V:onnxruntime:, session_state.cc:1142 VerifyEachNodeIsAssignedToAnEp] Node placements

2023-12-01 16:18:14.908 [info] 2023-12-01 16:18:14.905456 [V:onnxruntime:, session_state.cc:1145 VerifyEachNodeIsAssignedToAnEp]  All nodes placed on [CPUExecutionProvider]. Number of nodes: 56

2023-12-01 16:18:14.919 [info] 2023-12-01 16:18:14.916723 [V:onnxruntime:, session_state.cc:126 CreateGraphInfo] SaveMLValueNameIndexMapping

2023-12-01 16:18:14.919 [info] 2023-12-01 16:18:14.917144 [V:onnxruntime:, session_state.cc:172 CreateGraphInfo] Done saving OrtValue mappings.

2023-12-01 16:18:14.922 [info] 2023-12-01 16:18:14.919355 [I:onnxruntime:, allocation_planner.cc:2401 CreateGraphPartitioner] Use DeviceBasedPartition as default

2023-12-01 16:18:14.928 [info] 2023-12-01 16:18:14.925553 [I:onnxruntime:, session_state_utils.cc:201 SaveInitializedTensors] Saving initialized tensors.

2023-12-01 16:18:14.962 [info] 2023-12-01 16:18:14.959534 [I:onnxruntime:, session_state_utils.cc:345 SaveInitializedTensors] Done saving initialized tensors

2023-12-01 16:18:14.970 [info] 2023-12-01 16:18:14.966801 [I:onnxruntime:, inference_session.cc:1774 Initialize] Session successfully initialized.

2023-12-01 16:18:14.971 [info] (38036) Created IntelliCode session.
2023-12-01 16:18:14.972 [info] (38036) Initialize deeplearning succeeded
2023-12-01 16:18:14.975 [info] (38036) [BG(1)] analyzing: C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\return_class.py ...
2023-12-01 16:18:14.975 [info] (38036) [BG(1)]   checking: C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\return_class.py (16ms)
2023-12-01 16:18:14.975 [info] (38036) [BG(1)] analyzing: C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\return_class.py (16ms)
2023-12-01 16:18:14.976 [info] (38036) [BG(1)] getSemanticTokens range 0:0 - 5:0 at C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\return_class.py (1ms)
2023-12-01 16:18:14.976 [info] (38036) pytest configurations: {"message":"script","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2023-12-01 16:18:14.979 [info] (38036) [BG(1)] indexing: C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue\return_class.py [found 1] (1ms)
2023-12-01 16:18:15.510 [info] (38036) [IDX(2)] scan packages C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue (1697ms)
2023-12-01 16:18:15.634 [info] (38036) [IDX(2)] index packages C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue ...
2023-12-01 16:18:15.634 [info] (38036) [IDX(2)]   index execution environment C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue ...
2023-12-01 16:18:15.634 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\concurrent\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.635 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\distutils\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.635 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\idlelib\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.635 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\lib2to3\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.635 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\pydoc_data\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.636 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\test\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.636 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\turtledemo\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.636 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\urllib\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.637 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\wsgiref\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.637 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\xmlrpc\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.637 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\adodbapi\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.638 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\asttokens\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.638 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\babel\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.638 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\backcall\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.639 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\brotli\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.639 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\conda_content_trust\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.640 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\conda_env\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.640 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\conda_package_handling\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.640 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\conda_package_streaming\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.640 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\ipython_genutils\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.640 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\ipywidgets\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.641 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\jedi\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.641 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\jupyterlab\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.641 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\jupyterlab_lsp\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.642 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\jupyterlab_pygments\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.642 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\jupyterlab_widgets\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.642 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\jupyter_console\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.642 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\jupyter_lsp\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.643 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\jupyter_server_fileid\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.643 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\jupyter_server_ydoc\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.643 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\jupyter_ydoc\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.644 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\lxml\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.644 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\matplotlib_inline\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.644 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\menuinst\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.644 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\mkl\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.645 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\mkl_random\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.645 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\nbclassic\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.645 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\nbclient\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.646 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\nbconvert\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.646 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\nb_conda_kernels\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.646 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\notebook\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.646 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\notebook_shim\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.647 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\parso\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.647 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\PyQt5\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.647 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\qtconsole\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.648 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\sipbuild\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.648 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\stack_data\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.648 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\tinycss2\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.648 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\tlz\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.649 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\toolz\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.649 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\webencodings\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.649 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\websocket\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.650 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\wheel\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.650 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\widgetsnbextension\__init__.py [skipped: no '__all__' defined] (1ms)
2023-12-01 16:18:15.650 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\winpty\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.651 [info] (38036) [IDX(2)]     indexing: C:\Users\zekun\miniconda3\Lib\site-packages\pythonwin\pywin\__init__.py [skipped: no '__all__' defined] (0ms)
2023-12-01 16:18:15.668 [info] (38036) [IDX(2)]   index execution environment C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue [found 2343 in 83 files] (32ms)
2023-12-01 16:18:15.668 [info] (38036) [IDX(2)] index packages C:\Users\zekun\OneDrive\RWTH\GroupTheory\pylance_issue [found 2343 in 1 exec envs] (73ms)
erictraut commented 11 months ago

If you run the code in your example, you will receive a runtime exception from Python ("NameError: name 'MyClass' is not defined"). Pylance is warning you that your code will crash, so it's correct in generating an error here.

If you want to use a not-yet-declared identifier in a type annotation, you can do one of two things:

  1. Enclose the type annotation in quotes.

    class MyClass:
    def __init__(self):
        pass
    
    def myfunc(self) -> "MyClass":
        return self
  2. Add a from __future__ import annotations to the top of your file. This tells the Python interpreter to defer evaluation of your annotations.
    
    from __future__ import annotations

class MyClass: def init(self): pass

def myfunc(self) -> MyClass:
    return self
nqhq-lou commented 11 months ago

If you run the code in your example, you will receive a runtime exception from Python ("NameError: name 'MyClass' is not defined"). Pylance is warning you that your code will crash, so it's correct in generating an error here.

If you want to use a not-yet-declared identifier in a type annotation, you can do one of two things:

  1. Enclose the type annotation in quotes.
class MyClass:
    def __init__(self):
        pass

    def myfunc(self) -> "MyClass":
        return self
  1. Add a from __future__ import annotations to the top of your file. This tells the Python interpreter to defer evaluation of your annotations.
from __future__ import annotations

class MyClass:
    def __init__(self):
        pass

    def myfunc(self) -> MyClass:
        return self

Thanks! My question is solve:D