Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pylance outputs incorrect inferred type list | Any even when the type annotation is list #4936

Closed
rk-terence opened this issue Oct 7, 2023 · 2 comments
Assignees
Labels
needs repro Issue has not been reproduced yet

Comments

@rk-terence
Copy link

Environment data

  • Language Server version: v2023.10.10
  • OS and version: macOS 14 Sonoma
  • Python version: 3.10.13 (main, Aug 24 2023, 22:36:46) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin, not Anaconda

Code Snippet

import typing


def tolist_func(array) -> list:
    # even with annotation type `list`, the inferred type of result is `Any | list`, which is strange for me
    result: list = array.tolist()
    return result


def tolist_func_with_any(array: typing.Any) -> list:
    # When I annotate the type of `array` to be `typing.Any`, the inferred type of result is `list`, which I think is correct.
    result: list = array.tolist()
    return result

Repro Steps

  1. Open a new folder in VS Code, and create a new file with the above code snippet as content.
  2. Hover on the variable result in function tolist_func.
  3. Hover on the variable result in function tolist_func_with_any.

Expected behavior

I expect all of the inferred types of the two variables to be list.

Actual behavior

For the function tolist_func, the variable result's inferred type is Any | list, while the second one is list.

image image

This is strange for me. As far as I know, when not annotated, the array will implicitly has Any type, so the two inferred types should consent. And I think the correct inferred type should be list.

Logs

2023-10-07 10:51:25.702 [info] [Info  - 10:51:25] (25836) Pylance language server 2023.10.10 (pyright 45942a15) starting
2023-10-07 10:51:25.703 [info] [Info  - 10:51:25] (25836) Server root directory: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist
2023-10-07 10:51:25.703 [info] [Info  - 10:51:25] (25836) Starting service instance "mre-pylance-type-inference-bug"
2023-10-07 10:51:25.717 [info] (25836) No configuration file found.
2023-10-07 10:51:25.717 [info] (25836) No pyproject.toml file found.
2023-10-07 10:51:25.717 [info] [Info  - 10:51:25] (25836) Setting pythonPath for service "mre-pylance-type-inference-bug": "/opt/homebrew/bin/python3.10"
2023-10-07 10:51:25.717 [info] [Info  - 10:51:25] (25836) Setting environmentName for service "mre-pylance-type-inference-bug": "3.10.13 (global)"
2023-10-07 10:51:25.719 [info] [Warn  - 10:51:25] (25836) stubPath /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/typings is not a valid directory.
2023-10-07 10:51:25.740 [info] [Info  - 10:51:25] (25836) Assuming Python version 3.10
2023-10-07 10:51:25.740 [info] (25836) Assuming Python platform Darwin
2023-10-07 10:51:25.792 [info] [Info  - 10:51:25] (25836) Search paths for /Users/gz/Documents/tmp/mre-pylance-type-inference-bug
2023-10-07 10:51:25.792 [info] [Info  - 10:51:25] (25836)   /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib
2023-10-07 10:51:25.792 [info] [Info  - 10:51:25] (25836)   /Users/gz/Documents/tmp/mre-pylance-type-inference-bug
2023-10-07 10:51:25.792 [info] [Info  - 10:51:25] (25836)   /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/typings
2023-10-07 10:51:25.792 [info] [Info  - 10:51:25] (25836)   /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stubs/...
2023-10-07 10:51:25.792 [info] [Info  - 10:51:25] (25836)   /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/bundled/stubs
2023-10-07 10:51:25.792 [info] [Info  - 10:51:25] (25836)   /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10
2023-10-07 10:51:25.792 [info] [Info  - 10:51:25] (25836)   /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload
2023-10-07 10:51:25.792 [info] [Info  - 10:51:25] (25836)   /opt/homebrew/lib/python3.10/site-packages
2023-10-07 10:51:25.792 [info] [Info  - 10:51:25] (25836) Adding fs watcher for library directories:
 /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10
/opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload
/opt/homebrew/lib/python3.10/site-packages
2023-10-07 10:51:25.792 [info] [Info  - 10:51:25] (25836) Adding fs watcher for directories:
 /Users/gz/Documents/tmp/mre-pylance-type-inference-bug
2023-10-07 10:51:25.792 [info] (25836) Searching for source files
2023-10-07 10:51:25.793 [info] [Info  - 10:51:25] (25836) Found 1 source file
2023-10-07 10:51:25.829 [info] (25836) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (5ms)
2023-10-07 10:51:25.867 [info] (25836) [FG] parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 1ms] (37ms)
2023-10-07 10:51:25.887 [info] (25836) [FG] binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi (20ms)
2023-10-07 10:51:25.888 [info] (25836) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (1ms)
2023-10-07 10:51:26.066 [info] (25836) [BG(1)] getSemanticTokens full at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py ...
2023-10-07 10:51:26.066 [info] (25836) [BG(1)]   parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (51ms)
2023-10-07 10:51:26.105 [info] (25836) [IDX(2)] scan packages /Users/gz/Documents/tmp/mre-pylance-type-inference-bug ...
2023-10-07 10:51:26.106 [info] (25836) [IDX(2)]   read stdlib indices (14ms)
2023-10-07 10:51:26.108 [info] (25836) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 1ms] (41ms)
2023-10-07 10:51:26.125 [info] (25836) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi (16ms)
2023-10-07 10:51:26.125 [info] (25836) [BG(1)]   binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (1ms)
2023-10-07 10:51:26.141 [info] (25836) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing.pyi [fs read 0ms] (12ms)
2023-10-07 10:51:26.145 [info] (25836) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing.pyi (4ms)
2023-10-07 10:51:26.148 [info] (25836) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 0ms] (2ms)
2023-10-07 10:51:26.150 [info] (25836) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing_extensions.pyi (1ms)
2023-10-07 10:51:26.160 [info] (25836) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 1ms] (2ms)
2023-10-07 10:51:26.161 [info] (25836) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (1ms)
2023-10-07 10:51:26.164 [info] (25836) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/types.pyi [fs read 0ms] (4ms)
2023-10-07 10:51:26.166 [info] (25836) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/types.pyi (2ms)
2023-10-07 10:51:26.173 [info] (25836) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/abc.pyi [fs read 0ms] (0ms)
2023-10-07 10:51:26.174 [info] (25836) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/abc.pyi (1ms)
2023-10-07 10:51:26.178 [info] (25836) [BG(1)] getSemanticTokens full at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (164ms)
2023-10-07 10:51:26.179 [info] (25836) [BG(1)] getSemanticTokens range 0:0 - 11:0 at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (1ms)
2023-10-07 10:51:26.222 [info] (25836) [BG(1)] analyzing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py ...
2023-10-07 10:51:26.222 [info] (25836) [BG(1)]   checking: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py ...
2023-10-07 10:51:26.222 [info] (25836) [BG(1)]     parsing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py [fs read 0ms] (30ms)
2023-10-07 10:51:26.230 [info] (25836) [BG(1)]     binding: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py (8ms)
2023-10-07 10:51:26.235 [info] (25836) [BG(1)]   checking: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (56ms)
2023-10-07 10:51:26.235 [info] (25836) [BG(1)] analyzing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (56ms)
2023-10-07 10:51:26.237 [info] (25836) [BG(1)] indexing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py [found 2] (0ms)
2023-10-07 10:51:26.388 [info] (25836) [IDX(2)] scan packages /Users/gz/Documents/tmp/mre-pylance-type-inference-bug (297ms)
2023-10-07 10:51:26.407 [info] (25836) [IDX(2)] index packages /Users/gz/Documents/tmp/mre-pylance-type-inference-bug ...
2023-10-07 10:51:26.407 [info] (25836) [IDX(2)]   index execution environment /Users/gz/Documents/tmp/mre-pylance-type-inference-bug ...
2023-10-07 10:51:26.407 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/concurrent/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 10:51:26.407 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/distutils/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 10:51:26.407 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/idlelib/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 10:51:26.407 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib2to3/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 10:51:26.407 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pydoc_data/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 10:51:26.408 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/test/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 10:51:26.408 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/turtledemo/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 10:51:26.408 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 10:51:26.408 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/wsgiref/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 10:51:26.449 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xml/__init__.py ...
2023-10-07 10:51:26.449 [info] (25836) [IDX(2)]       parsing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xml/__init__.py [fs read 0ms] (41ms)
2023-10-07 10:51:26.474 [info] (25836) [IDX(2)]       parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 1ms] (24ms)
2023-10-07 10:51:26.480 [info] (25836) [IDX(2)]       binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi (6ms)
2023-10-07 10:51:26.480 [info] (25836) [IDX(2)]       binding: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xml/__init__.py (0ms)
2023-10-07 10:51:26.480 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xml/__init__.py [found 0] (73ms)
2023-10-07 10:51:26.480 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xmlrpc/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 10:51:26.481 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/lib/python3.10/site-packages/pip/__init__.py ...
2023-10-07 10:51:26.481 [info] (25836) [IDX(2)]       parsing: /opt/homebrew/lib/python3.10/site-packages/pip/__init__.py [fs read 0ms] (0ms)
2023-10-07 10:51:26.481 [info] (25836) [IDX(2)]       binding: /opt/homebrew/lib/python3.10/site-packages/pip/__init__.py (0ms)
2023-10-07 10:51:26.481 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/lib/python3.10/site-packages/pip/__init__.py [found 2] (1ms)
2023-10-07 10:51:26.482 [info] (25836) [IDX(2)]     indexing: /opt/homebrew/lib/python3.10/site-packages/wheel/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 10:51:26.482 [info] (25836) [IDX(2)]   index execution environment /Users/gz/Documents/tmp/mre-pylance-type-inference-bug [found 98 in 4 files] (76ms)
2023-10-07 10:51:26.482 [info] (25836) [IDX(2)] index packages /Users/gz/Documents/tmp/mre-pylance-type-inference-bug [found 98 in 1 exec envs] (78ms)
2023-10-07 10:51:29.628 [info] (25836) [FG] parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing.pyi [fs read 0ms] (8ms)
2023-10-07 10:51:29.632 [info] (25836) [FG] binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing.pyi (3ms)
2023-10-07 10:51:29.635 [info] (25836) [FG] parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 0ms] (2ms)
2023-10-07 10:51:29.637 [info] (25836) [FG] binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing_extensions.pyi (2ms)
2023-10-07 10:51:29.648 [info] (25836) [FG] parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 0ms] (4ms)
2023-10-07 10:51:29.649 [info] (25836) [FG] binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (1ms)
2023-10-07 10:51:29.652 [info] (25836) [FG] parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/types.pyi [fs read 0ms] (3ms)
2023-10-07 10:51:29.654 [info] (25836) [FG] binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/types.pyi (2ms)
2023-10-07 10:51:29.657 [info] (25836) [FG] parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/abc.pyi [fs read 0ms] (0ms)
2023-10-07 10:51:29.658 [info] (25836) [FG] binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/abc.pyi (1ms)
2023-10-07 10:51:35.480 [info] (25836) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (2ms)
2023-10-07 10:51:35.481 [info] (25836) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:35.783 [info] (25836) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:35.783 [info] (25836) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:35.835 [info] (25836) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (1ms)
2023-10-07 10:51:35.835 [info] (25836) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:35.990 [info] (25836) [FG] parsing: /var/folders/my/fsnt8tmn2kj_j69ghb7np5jh0000gn/T/pyright-25836-IxkzXigBCo3C/builtins-25836-c5R9roHgs2y4-.py [fs read 0ms] (23ms)
2023-10-07 10:51:36.004 [info] (25836) [FG] binding: /var/folders/my/fsnt8tmn2kj_j69ghb7np5jh0000gn/T/pyright-25836-IxkzXigBCo3C/builtins-25836-c5R9roHgs2y4-.py (13ms)
2023-10-07 10:51:36.009 [info] (25836) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:36.009 [info] (25836) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:36.013 [info] (25836) [BG(1)] getSemanticTokens delta previousResultId:1696647086125 at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py ...
2023-10-07 10:51:36.013 [info] (25836) [BG(1)]   parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (1ms)
2023-10-07 10:51:36.013 [info] (25836) [BG(1)]   binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:36.018 [info] (25836) [BG(1)] getSemanticTokens delta previousResultId:1696647086125 at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (5ms)
2023-10-07 10:51:36.274 [info] (25836) [BG(1)] analyzing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py ...
2023-10-07 10:51:36.274 [info] (25836) [BG(1)]   parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:36.274 [info] (25836) [BG(1)]   binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (1ms)
2023-10-07 10:51:36.279 [info] (25836) [BG(1)]   checking: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (5ms)
2023-10-07 10:51:36.279 [info] (25836) [BG(1)] analyzing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (6ms)
2023-10-07 10:51:36.280 [info] (25836) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:36.280 [info] (25836) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:36.280 [info] (25836) [BG(1)] indexing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py [found 2] (0ms)
2023-10-07 10:51:36.475 [info] (25836) [BG(1)] getSemanticTokens delta previousResultId:1696647096013 at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.002 [info] (25836) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (1ms)
2023-10-07 10:51:38.002 [info] (25836) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.286 [info] (25836) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.286 [info] (25836) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.339 [info] (25836) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.339 [info] (25836) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.419 [info] (25836) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.419 [info] (25836) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.453 [info] (25836) [BG(1)] getSemanticTokens delta previousResultId:1696647096475 at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py ...
2023-10-07 10:51:38.453 [info] (25836) [BG(1)]   parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.453 [info] (25836) [BG(1)]   binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.456 [info] (25836) [BG(1)] getSemanticTokens delta previousResultId:1696647096475 at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (3ms)
2023-10-07 10:51:38.778 [info] (25836) [BG(1)] analyzing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py ...
2023-10-07 10:51:38.778 [info] (25836) [BG(1)]   parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (1ms)
2023-10-07 10:51:38.778 [info] (25836) [BG(1)]   binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.786 [info] (25836) [BG(1)]   checking: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (9ms)
2023-10-07 10:51:38.786 [info] (25836) [BG(1)] analyzing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (10ms)
2023-10-07 10:51:38.787 [info] (25836) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (1ms)
2023-10-07 10:51:38.787 [info] (25836) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 10:51:38.787 [info] (25836) [BG(1)] indexing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py [found 2] (1ms)
2023-10-07 10:51:38.974 [info] (25836) [BG(1)] getSemanticTokens delta previousResultId:1696647098453 at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (1ms)
2023-10-07 10:51:39.429 [info] [Info  - 10:51:39] (25836) SourceFile: Received fs event 'change' for path '/Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py'
2023-10-07 10:51:39.451 [info] (25836) [BG(1)] indexing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py [found 2] (0ms)
2023-10-07 11:03:35.139 [info] (25836) WorkspaceFactory 0 clear
2023-10-07 11:03:35.143 [info] (Client) Experiment 'python.createEnvironment.trigger' is active
2023-10-07 11:03:35.143 [info] (Client) Experiment 'pythonPromptNewFormatterExt' is active
2023-10-07 11:03:35.143 [info] (Client) Experiment 'pythonPromptNewToolsExt' is active
2023-10-07 11:03:35.143 [info] (Client) Experiment 'pythonTerminalEnvVarActivationcf' is active
2023-10-07 11:03:35.143 [info] (Client) Experiment 'pythonTestAdapter' is active
2023-10-07 11:03:36.285 [info] [Info  - 11:03:36] (25857) Pylance language server 2023.10.10 (pyright 45942a15) starting
2023-10-07 11:03:36.285 [info] [Info  - 11:03:36] (25857) Server root directory: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist
2023-10-07 11:03:36.287 [info] [Info  - 11:03:36] (25857) Starting service instance "mre-pylance-type-inference-bug"
2023-10-07 11:03:36.303 [info] (25857) No configuration file found.
2023-10-07 11:03:36.303 [info] (25857) No pyproject.toml file found.
2023-10-07 11:03:36.303 [info] [Info  - 11:03:36] (25857) Setting pythonPath for service "mre-pylance-type-inference-bug": "/opt/homebrew/bin/python3.10"
2023-10-07 11:03:36.303 [info] [Info  - 11:03:36] (25857) Setting environmentName for service "mre-pylance-type-inference-bug": "3.10.13 (global)"
2023-10-07 11:03:36.304 [info] [Warn  - 11:03:36] (25857) stubPath /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/typings is not a valid directory.
2023-10-07 11:03:36.323 [info] [Info  - 11:03:36] (25857) Assuming Python version 3.10
2023-10-07 11:03:36.323 [info] (25857) Assuming Python platform Darwin
2023-10-07 11:03:36.375 [info] [Info  - 11:03:36] (25857) Search paths for /Users/gz/Documents/tmp/mre-pylance-type-inference-bug
2023-10-07 11:03:36.376 [info] [Info  - 11:03:36] (25857)   /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib
2023-10-07 11:03:36.376 [info] [Info  - 11:03:36] (25857)   /Users/gz/Documents/tmp/mre-pylance-type-inference-bug
2023-10-07 11:03:36.376 [info] [Info  - 11:03:36] (25857)   /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/typings
2023-10-07 11:03:36.376 [info] [Info  - 11:03:36] (25857)   /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stubs/...
2023-10-07 11:03:36.376 [info] [Info  - 11:03:36] (25857)   /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/bundled/stubs
2023-10-07 11:03:36.376 [info] [Info  - 11:03:36] (25857)   /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10
2023-10-07 11:03:36.376 [info] [Info  - 11:03:36] (25857)   /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload
2023-10-07 11:03:36.376 [info] [Info  - 11:03:36] (25857)   /opt/homebrew/lib/python3.10/site-packages
2023-10-07 11:03:36.376 [info] [Info  - 11:03:36] (25857) Adding fs watcher for library directories:
 /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10
/opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload
/opt/homebrew/lib/python3.10/site-packages
2023-10-07 11:03:36.376 [info] [Info  - 11:03:36] (25857) Adding fs watcher for directories:
 /Users/gz/Documents/tmp/mre-pylance-type-inference-bug
2023-10-07 11:03:36.376 [info] (25857) Searching for source files
2023-10-07 11:03:36.378 [info] [Info  - 11:03:36] (25857) Found 1 source file
2023-10-07 11:03:36.411 [info] (25857) [FG] parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (5ms)
2023-10-07 11:03:36.447 [info] (25857) [FG] parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 1ms] (35ms)
2023-10-07 11:03:36.465 [info] (25857) [FG] binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi (18ms)
2023-10-07 11:03:36.466 [info] (25857) [FG] binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 11:03:36.650 [info] (25857) [BG(1)] getSemanticTokens full at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py ...
2023-10-07 11:03:36.650 [info] (25857) [BG(1)]   parsing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (51ms)
2023-10-07 11:03:36.675 [info] (25857) [IDX(2)] scan packages /Users/gz/Documents/tmp/mre-pylance-type-inference-bug ...
2023-10-07 11:03:36.675 [info] (25857) [IDX(2)]   read stdlib indices (10ms)
2023-10-07 11:03:36.687 [info] (25857) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 2ms] (37ms)
2023-10-07 11:03:36.703 [info] (25857) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi (16ms)
2023-10-07 11:03:36.703 [info] (25857) [BG(1)]   binding: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 11:03:36.719 [info] (25857) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing.pyi [fs read 1ms] (13ms)
2023-10-07 11:03:36.723 [info] (25857) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing.pyi (3ms)
2023-10-07 11:03:36.726 [info] (25857) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 0ms] (2ms)
2023-10-07 11:03:36.727 [info] (25857) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing_extensions.pyi (1ms)
2023-10-07 11:03:36.737 [info] (25857) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 0ms] (2ms)
2023-10-07 11:03:36.738 [info] (25857) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (1ms)
2023-10-07 11:03:36.742 [info] (25857) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/types.pyi [fs read 0ms] (4ms)
2023-10-07 11:03:36.744 [info] (25857) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/types.pyi (2ms)
2023-10-07 11:03:36.751 [info] (25857) [BG(1)]   parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/abc.pyi [fs read 0ms] (0ms)
2023-10-07 11:03:36.752 [info] (25857) [BG(1)]   binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/abc.pyi (1ms)
2023-10-07 11:03:36.756 [info] (25857) [BG(1)] getSemanticTokens full at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (156ms)
2023-10-07 11:03:36.757 [info] (25857) [BG(1)] getSemanticTokens range 0:0 - 11:0 at /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (0ms)
2023-10-07 11:03:36.797 [info] (25857) [BG(1)] analyzing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py ...
2023-10-07 11:03:36.797 [info] (25857) [BG(1)]   checking: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py ...
2023-10-07 11:03:36.797 [info] (25857) [BG(1)]     parsing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py [fs read 0ms] (27ms)
2023-10-07 11:03:36.805 [info] (25857) [BG(1)]     binding: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py (8ms)
2023-10-07 11:03:36.810 [info] (25857) [BG(1)]   checking: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (53ms)
2023-10-07 11:03:36.810 [info] (25857) [BG(1)] analyzing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py (53ms)
2023-10-07 11:03:36.812 [info] (25857) [BG(1)] indexing: /Users/gz/Documents/tmp/mre-pylance-type-inference-bug/test.py [found 2] (0ms)
2023-10-07 11:03:36.947 [info] (25857) [IDX(2)] scan packages /Users/gz/Documents/tmp/mre-pylance-type-inference-bug (282ms)
2023-10-07 11:03:36.966 [info] (25857) [IDX(2)] index packages /Users/gz/Documents/tmp/mre-pylance-type-inference-bug ...
2023-10-07 11:03:36.966 [info] (25857) [IDX(2)]   index execution environment /Users/gz/Documents/tmp/mre-pylance-type-inference-bug ...
2023-10-07 11:03:36.966 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/concurrent/__init__.py [skipped: no '__all__' defined] (1ms)
2023-10-07 11:03:36.966 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/distutils/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 11:03:36.966 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/idlelib/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 11:03:36.966 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib2to3/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 11:03:36.966 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pydoc_data/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 11:03:36.966 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/test/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 11:03:36.966 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/turtledemo/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 11:03:36.966 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 11:03:36.966 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/wsgiref/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 11:03:37.008 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xml/__init__.py ...
2023-10-07 11:03:37.008 [info] (25857) [IDX(2)]       parsing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xml/__init__.py [fs read 1ms] (41ms)
2023-10-07 11:03:37.032 [info] (25857) [IDX(2)]       parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 1ms] (24ms)
2023-10-07 11:03:37.038 [info] (25857) [IDX(2)]       binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/builtins.pyi (6ms)
2023-10-07 11:03:37.038 [info] (25857) [IDX(2)]       binding: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xml/__init__.py (0ms)
2023-10-07 11:03:37.038 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xml/__init__.py [found 0] (72ms)
2023-10-07 11:03:37.039 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/xmlrpc/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 11:03:37.039 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/lib/python3.10/site-packages/pip/__init__.py ...
2023-10-07 11:03:37.039 [info] (25857) [IDX(2)]       parsing: /opt/homebrew/lib/python3.10/site-packages/pip/__init__.py [fs read 0ms] (1ms)
2023-10-07 11:03:37.039 [info] (25857) [IDX(2)]       binding: /opt/homebrew/lib/python3.10/site-packages/pip/__init__.py (0ms)
2023-10-07 11:03:37.039 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/lib/python3.10/site-packages/pip/__init__.py [found 2] (1ms)
2023-10-07 11:03:37.039 [info] (25857) [IDX(2)]     indexing: /opt/homebrew/lib/python3.10/site-packages/wheel/__init__.py [skipped: no '__all__' defined] (0ms)
2023-10-07 11:03:37.040 [info] (25857) [IDX(2)]   index execution environment /Users/gz/Documents/tmp/mre-pylance-type-inference-bug [found 98 in 4 files] (75ms)
2023-10-07 11:03:37.040 [info] (25857) [IDX(2)] index packages /Users/gz/Documents/tmp/mre-pylance-type-inference-bug [found 98 in 1 exec envs] (77ms)
2023-10-07 11:04:21.066 [info] (25857) [FG] parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing.pyi [fs read 3ms] (25ms)
2023-10-07 11:04:21.071 [info] (25857) [FG] binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing.pyi (5ms)
2023-10-07 11:04:21.077 [info] (25857) [FG] parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 0ms] (3ms)
2023-10-07 11:04:21.079 [info] (25857) [FG] binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/typing_extensions.pyi (3ms)
2023-10-07 11:04:21.092 [info] (25857) [FG] parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 1ms] (3ms)
2023-10-07 11:04:21.093 [info] (25857) [FG] binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (1ms)
2023-10-07 11:04:21.098 [info] (25857) [FG] parsing: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/types.pyi [fs read 1ms] (5ms)
2023-10-07 11:04:21.101 [info] (25857) [FG] binding: /Users/gz/.vscode/extensions/ms-python.vscode-pylance-2023.10.10/dist/typeshed-fallback/stdlib/types.pyi (3ms)

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Oct 7, 2023
@erictraut
Copy link
Contributor

This behavior is intended, so I don't consider it a bug. I can understand why this appears inconsistent, but there's a good reason for it.

Pyright (the type checker upon which pylance is built) distinguishes between explicit Any and implicit Any. It refers to the latter as Unknown when type checking is enabled. For more details, refer to this documentation. In most cases, pyright treats Any and Unknown the same, but in strict type checking mode, it allows you to be notified specifically when you attempt to use an Unknown type. The assumption is that if you have explicitly specified Any, you have acknowledged that you're foregoing type safety, but if you have an Unknown you will want to be told about it. For this reason, pyright retains an Unknown when narrowing a type for assignment. In your first example, the type of the parameter array is Unknown because you haven't provided any type annotation for it. That means the type of the expression array.tolist() is also Unknown. When assigning this to the variable result, its type is narrowed from list to list | Unknown. This preserves the Unknown so if you use this value subsequently and you have strict type checking mode enabled, you will be informed about its use.

When you disable type checking (i.e. set typeCheckingMode to "off") pylance displays Unknown as Any even though internally the type tracked by pright is Unknown.

@rk-terence
Copy link
Author

This behavior is intended, so I don't consider it a bug. I can understand why this appears inconsistent, but there's a good reason for it.

Pyright (the type checker upon which pylance is built) distinguishes between explicit Any and implicit Any. It refers to the latter as Unknown when type checking is enabled. For more details, refer to this documentation. In most cases, pyright treats Any and Unknown the same, but in strict type checking mode, it allows you to be notified specifically when you attempt to use an Unknown type. The assumption is that if you have explicitly specified Any, you have acknowledged that you're foregoing type safety, but if you have an Unknown you will want to be told about it. For this reason, pyright retains an Unknown when narrowing a type for assignment. In your first example, the type of the parameter array is Unknown because you haven't provided any type annotation for it. That means the type of the expression array.tolist() is also Unknown. When assigning this to the variable result, its type is narrowed from list to list | Unknown. This preserves the Unknown so if you use this value subsequently and you have strict type checking mode enabled, you will be informed about its use.

When you disable type checking (i.e. set typeCheckingMode to "off") pylance displays Unknown as Any even though internally the type tracked by pright is Unknown.

Thank you so much for clarifying this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro Issue has not been reproduced yet
Projects
None yet
Development

No branches or pull requests

3 participants