microsoft / pylance-release

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

type check code unreachable #6158

Closed pierreBodineau closed 2 months ago

pierreBodineau commented 2 months ago

Environment data

Code Snippet

def foo(bar: str):
    if not isinstance(bar, str):
        raise TypeError

Expected behavior

I'm doing a type check, so the code under the if clause is relevant. It shouldn't be shown as unreachable.

Actual behavior

image

Logs

2024-07-18 15:13:55.324 [info] [Info  - 3:13:55 PM] (22332) Pylance language server 2024.7.1 (pyright version 1.1.369, commit 1168be71) starting
2024-07-18 15:13:55.330 [info] [Info  - 3:13:55 PM] (22332) Server root directory: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist
2024-07-18 15:13:55.432 [info] [Info  - 3:13:55 PM] (22332) Starting service instance "<default>"
2024-07-18 15:13:55.509 [info] (22332) No pyproject.toml file found.
2024-07-18 15:13:55.514 [info] [Info  - 3:13:55 PM] (22332) Setting pythonPath for service "<default>": "c:\Users\xxx\AppData\Local\miniconda3\python.exe"
2024-07-18 15:13:55.517 [info] [Info  - 3:13:55 PM] (22332) Setting environmentName for service "<default>": "3.12.1 (base conda)"
2024-07-18 15:13:55.518 [info] [Info  - 3:13:55 PM] (22332) No include entries specified; assuming \<default workspace root>
2024-07-18 15:13:55.519 [info] [Info  - 3:13:55 PM] (22332) Auto-excluding **/node_modules
2024-07-18 15:13:55.520 [info] [Info  - 3:13:55 PM] (22332) Auto-excluding **/__pycache__
2024-07-18 15:13:55.521 [info] [Info  - 3:13:55 PM] (22332) Auto-excluding **/.*
2024-07-18 15:13:55.978 [info] [Info  - 3:13:55 PM] (22332) Assuming Python version 3.12.1.final.0
2024-07-18 15:13:55.978 [info] (22332) Assuming Python platform Windows
2024-07-18 15:13:56.529 [info] [Info  - 3:13:56 PM] (22332) Search paths for file:///%3Cdefault%20workspace%20root%3E
2024-07-18 15:13:56.532 [info] [Info  - 3:13:56 PM] (22332)   c:\Users\xxx\.vscode\extensions\ms-python.vscode-pylance-2024.7.1\dist\typeshed-fallback\stdlib
2024-07-18 15:13:56.534 [info] [Info  - 3:13:56 PM] (22332)   \<default workspace root>
2024-07-18 15:13:56.535 [info] [Info  - 3:13:56 PM] (22332)   \<default workspace root>\typings
2024-07-18 15:13:56.537 [info] [Info  - 3:13:56 PM] (22332)   c:\Users\xxx\.vscode\extensions\ms-python.vscode-pylance-2024.7.1\dist\typeshed-fallback\stubs\...
2024-07-18 15:13:56.537 [info] [Info  - 3:13:56 PM] (22332)   c:\Users\xxx\.vscode\extensions\ms-python.vscode-pylance-2024.7.1\dist\bundled\stubs
2024-07-18 15:13:56.538 [info] [Info  - 3:13:56 PM] (22332)   c:\Users\xxx\AppData\Local\miniconda3\DLLs
2024-07-18 15:13:56.538 [info] [Info  - 3:13:56 PM] (22332)   c:\Users\xxx\AppData\Local\miniconda3\Lib
2024-07-18 15:13:56.539 [info] [Info  - 3:13:56 PM] (22332)   c:\Users\xxx\AppData\Local\miniconda3
2024-07-18 15:13:56.546 [info] [Info  - 3:13:56 PM] (22332)   c:\Users\xxx\AppData\Local\miniconda3\Lib\site-packages
2024-07-18 15:13:56.554 [info] [Info  - 3:13:56 PM] (22332) Adding fs watcher for library directories:
 file:///c%3A/Users/xxx/AppData/Local/miniconda3/DLLs
file:///c%3A/Users/xxx/AppData/Local/miniconda3/Lib
file:///c%3A/Users/xxx/AppData/Local/miniconda3
file:///c%3A/Users/xxx/AppData/Local/miniconda3/Lib/site-packages
2024-07-18 15:13:56.556 [info] [Info  - 3:13:56 PM] (22332) Adding fs watcher for directories:
 file:///%3Cdefault%20workspace%20root%3E
2024-07-18 15:13:56.557 [info] (22332) Searching for source files
2024-07-18 15:13:56.558 [info] [Error - 3:13:56 PM] (22332) File or directory "\<default workspace root>" does not exist.
2024-07-18 15:13:56.558 [info] [Info  - 3:13:56 PM] (22332) No source files found.
2024-07-18 15:13:56.617 [info] (22332) pytest configurations: {"message":"request cancelled","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-07-18 15:13:56.618 [info] (22332) pytest configurations: {"message":"script","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-07-18 15:13:56.685 [info] (22332) [IDX(FG)] index libraries file:///%3Cdefault%20workspace%20root%3E (index) ...
2024-07-18 15:13:56.685 [info] (22332) [IDX(FG)]   read stdlib indices (61ms)
2024-07-18 15:13:56.696 [info] (22332) [IDX(FG)] index libraries file:///%3Cdefault%20workspace%20root%3E (index) [succeed] (72ms)
2024-07-18 15:13:56.704 [info] (22332) Server settings returned for workspace: undefined: {
  "autoSearchPaths": true,
  "disableLanguageServices": false,
  "openFilesOnly": true,
  "useLibraryCodeForTypes": true,
  "watchForSourceChanges": true,
  "watchForLibraryChanges": true,
  "watchForConfigChanges": true,
  "typeCheckingMode": "off",
  "diagnosticSeverityOverrides": {
    "reportShadowedImports": "warning"
  },
  "logLevel": "log",
  "autoImportCompletions": false,
  "indexing": false,
  "completeFunctionParens": false,
  "enableExtractCodeAction": true,
  "indexOptions": {
    "packageDepths": [
      [
        "sklearn",
        2,
        false
      ],
      [
        "matplotlib",
        2,
        false
      ],
      [
        "scipy",
        2,
        false
      ],
      [
        "django",
        2,
        false
      ],
      [
        "flask",
        2,
        false
      ],
      [
        "fastapi",
        2,
        false
      ]
    ],
    "userFileIndexingLimit": 2000
  },
  "variableInlayTypeHints": false,
  "callArgumentNameInlayHints": "off",
  "functionReturnInlayTypeHints": false,
  "pytestParametersInlayTypeHints": false,
  "importFormat": "absolute",
  "includeFileSpecs": [],
  "excludeFileSpecs": [],
  "ignoreFileSpecs": [],
  "formatOnType": true,
  "taskListTokens": [],
  "enablePytestSupport": true,
  "gotoDefinitionInStringLiteral": true,
  "remapDiagnostics": false,
  "intelliCodeEnabled": true,
  "pythonPath": {
    "_key": "c:\\users\\xxx\\appdata\\local\\miniconda3\\python.exe",
    "_filePath": "c:\\Users\\xxx\\AppData\\Local\\miniconda3\\python.exe",
    "_query": "",
    "_fragment": "",
    "_originalString": "file:///c%3A/Users/xxx/AppData/Local/miniconda3/python.exe",
    "_isCaseSensitive": false
  },
  "pythonEnvironmentName": "3.12.1 (base conda)",
  "fixAll": [],
  "extraCommitChars": false,
  "autoFormatStrings": false,
  "autoIndent": true,
  "supportRestructuredText": true,
  "collectModuleInfo": false,
  "nodeExecutable": "",
  "editorIndentConfig": {
    "defaultTabSequence": "    ",
    "tabSize": 4
  }
}
2024-07-18 15:13:56.718 [info] (22332) No pyproject.toml file found.
2024-07-18 15:13:56.718 [info] [Info  - 3:13:56 PM] (22332) Setting pythonPath for service "<default>": "c:\Users\xxx\AppData\Local\miniconda3\python.exe"
2024-07-18 15:13:56.719 [info] [Info  - 3:13:56 PM] (22332) Setting environmentName for service "<default>": "3.12.1 (base conda)"
2024-07-18 15:13:56.719 [info] [Info  - 3:13:56 PM] (22332) No include entries specified; assuming \<default workspace root>
2024-07-18 15:13:56.720 [info] [Info  - 3:13:56 PM] (22332) Auto-excluding **/node_modules
2024-07-18 15:13:56.720 [info] [Info  - 3:13:56 PM] (22332) Auto-excluding **/__pycache__
2024-07-18 15:13:56.721 [info] [Info  - 3:13:56 PM] (22332) Auto-excluding **/.*
2024-07-18 15:13:56.974 [info] [Info  - 3:13:56 PM] (22332) Assuming Python version 3.12.1.final.0
2024-07-18 15:13:56.975 [info] (22332) Assuming Python platform Windows
2024-07-18 15:13:57.406 [info] [Info  - 3:13:57 PM] (22332) Search paths for file:///%3Cdefault%20workspace%20root%3E
2024-07-18 15:13:57.409 [info] [Info  - 3:13:57 PM] (22332)   c:\Users\xxx\.vscode\extensions\ms-python.vscode-pylance-2024.7.1\dist\typeshed-fallback\stdlib
2024-07-18 15:13:57.414 [info] [Info  - 3:13:57 PM] (22332)   \<default workspace root>
2024-07-18 15:13:57.416 [info] [Info  - 3:13:57 PM] (22332)   \<default workspace root>\typings
2024-07-18 15:13:57.418 [info] [Info  - 3:13:57 PM] (22332)   c:\Users\xxx\.vscode\extensions\ms-python.vscode-pylance-2024.7.1\dist\typeshed-fallback\stubs\...
2024-07-18 15:13:57.418 [info] [Info  - 3:13:57 PM] (22332)   c:\Users\xxx\.vscode\extensions\ms-python.vscode-pylance-2024.7.1\dist\bundled\stubs
2024-07-18 15:13:57.419 [info] [Info  - 3:13:57 PM] (22332)   c:\Users\xxx\AppData\Local\miniconda3\DLLs
2024-07-18 15:13:57.420 [info] [Info  - 3:13:57 PM] (22332)   c:\Users\xxx\AppData\Local\miniconda3\Lib
2024-07-18 15:13:57.420 [info] [Info  - 3:13:57 PM] (22332)   c:\Users\xxx\AppData\Local\miniconda3
2024-07-18 15:13:57.421 [info] [Info  - 3:13:57 PM] (22332)   c:\Users\xxx\AppData\Local\miniconda3\Lib\site-packages
2024-07-18 15:13:57.422 [info] [Info  - 3:13:57 PM] (22332) Adding fs watcher for library directories:
 file:///c%3A/Users/xxx/AppData/Local/miniconda3/DLLs
file:///c%3A/Users/xxx/AppData/Local/miniconda3/Lib
file:///c%3A/Users/xxx/AppData/Local/miniconda3
file:///c%3A/Users/xxx/AppData/Local/miniconda3/Lib/site-packages
2024-07-18 15:13:57.423 [info] [Info  - 3:13:57 PM] (22332) Adding fs watcher for directories:
 file:///%3Cdefault%20workspace%20root%3E
2024-07-18 15:13:57.435 [info] (22332) Searching for source files
2024-07-18 15:13:57.437 [info] [Error - 3:13:57 PM] (22332) File or directory "\<default workspace root>" does not exist.
2024-07-18 15:13:57.437 [info] [Info  - 3:13:57 PM] (22332) No source files found.
2024-07-18 15:13:57.439 [info] (22332) [IDX(FG)] index libraries file:///%3Cdefault%20workspace%20root%3E (index) ...
2024-07-18 15:13:57.440 [info] (22332) [IDX(FG)]   read stdlib indices (1ms)
2024-07-18 15:13:57.440 [info] (22332) [IDX(FG)] index libraries file:///%3Cdefault%20workspace%20root%3E (index) [succeed] (4ms)
2024-07-18 15:13:57.566 [info] (22332) Attempting to resolve using local imports: __builtins__
2024-07-18 15:13:57.568 [info] (22332) Attempting to resolve using root path 'file:///c%3A/Users/xxx/dev/norsyspy-dev'
2024-07-18 15:13:57.570 [info] (22332) [FG] parsing: file:///c%3A/Users/xxx/dev/norsyspy-dev/test.py (143ms)
2024-07-18 15:13:57.765 [info] (22332) [FG] parsing: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 8ms] (194ms)
2024-07-18 15:13:57.862 [info] (22332) [FG] binding: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/builtins.pyi (98ms)
2024-07-18 15:13:57.865 [info] (22332) [FG] binding: file:///c%3A/Users/xxx/dev/norsyspy-dev/test.py (2ms)
2024-07-18 15:13:57.872 [info] (22332) pytest configurations: {"message":"spawn c:\\Users\\xxx\\AppData\\Local\\miniconda3\\python.exe ENOENT","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-07-18 15:13:57.888 [info] [Info  - 3:13:57 PM] (22332) Background analysis(1) root directory: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist
2024-07-18 15:13:57.891 [info] [Info  - 3:13:57 PM] (22332) Background analysis(1) started
2024-07-18 15:13:57.891 [info] (22332) pytest configurations: {"message":"request cancelled","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-07-18 15:13:57.891 [info] (22332) pytest configurations: {"message":"spawn c:\\Users\\xxx\\AppData\\Local\\miniconda3\\python.exe ENOENT","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-07-18 15:13:58.217 [info] (22332) pytest configurations: {"message":"spawn c:\\Users\\xxx\\AppData\\Local\\miniconda3\\python.exe ENOENT","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2024-07-18 15:13:58.386 [info] (22332) Attempting to resolve using local imports: __builtins__
2024-07-18 15:13:58.388 [info] (22332) Attempting to resolve using root path 'file:///c%3A/Users/xxx/dev/norsyspy-dev'
2024-07-18 15:13:58.403 [info] (22332) [BG(1)] analyzing: file:///c%3A/Users/xxx/dev/norsyspy-dev/test.py ...
2024-07-18 15:13:58.404 [info] (22332) [BG(1)]   parsing: file:///c%3A/Users/xxx/dev/norsyspy-dev/test.py (171ms)
2024-07-18 15:13:58.559 [info] (22332) [BG(1)]   parsing: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 14ms] (157ms)
2024-07-18 15:13:58.635 [info] (22332) [BG(1)]   binding: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/builtins.pyi (71ms)
2024-07-18 15:13:58.636 [info] (22332) [BG(1)]   binding: file:///c%3A/Users/xxx/dev/norsyspy-dev/test.py (1ms)
2024-07-18 15:13:58.727 [info] (22332) [BG(1)]   checking: file:///c%3A/Users/xxx/dev/norsyspy-dev/test.py ...
2024-07-18 15:13:58.728 [info] (22332) [BG(1)]     parsing: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/typing.pyi [fs read 2ms] (51ms)
2024-07-18 15:13:58.750 [info] (22332) [BG(1)]     binding: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/typing.pyi (21ms)
2024-07-18 15:13:58.816 [info] (22332) [BG(1)]     parsing: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/types.pyi [fs read 5ms] (36ms)
2024-07-18 15:13:58.839 [info] (22332) [BG(1)]     binding: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/types.pyi (22ms)
2024-07-18 15:13:58.869 [info] (22332) [BG(1)]     parsing: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 2ms] (12ms)
2024-07-18 15:13:58.874 [info] (22332) [BG(1)]     binding: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (8ms)
2024-07-18 15:13:58.900 [info] (22332) [BG(1)]     parsing: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/abc.pyi [fs read 4ms] (7ms)
2024-07-18 15:13:58.901 [info] (22332) [BG(1)]     binding: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/abc.pyi (2ms)
2024-07-18 15:13:58.930 [info] (22332) [BG(1)]     parsing: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 9ms] (21ms)
2024-07-18 15:13:58.939 [info] (22332) [BG(1)]     binding: file:///c%3A/Users/xxx/.vscode/extensions/ms-python.vscode-pylance-2024.7.1/dist/typeshed-fallback/stdlib/typing_extensions.pyi (9ms)
2024-07-18 15:13:58.999 [info] (22332) [BG(1)]   checking: file:///c%3A/Users/xxx/dev/norsyspy-dev/test.py (361ms)
2024-07-18 15:13:58.999 [info] [Info  - 3:13:58 PM] (22332) Heap stats: total_heap_size=51MB, used_heap_size=38MB, cross_worker_used_heap_size=38MB, total_physical_size=51MB, total_available_size=4056MB, heap_size_limit=4096MB
2024-07-18 15:13:59.000 [info] (22332) [BG(1)] analyzing: file:///c%3A/Users/xxx/dev/norsyspy-dev/test.py (779ms)
2024-07-18 15:13:59.013 [info] (22332) [BG(1)] SemanticTokens range 0:0 - 4:0 at file:///c%3A/Users/xxx/dev/norsyspy-dev/test.py (5ms)
2024-07-18 15:13:59.015 [info] (22332) [BG(1)] SemanticTokens full at file:///c%3A/Users/xxx/dev/norsyspy-dev/test.py (2ms)
2024-07-18 15:14:01.037 [info] (22332) [BG(1)] SemanticTokens delta previousResultId:1721330039001 at file:///c%3A/Users/xxx/dev/norsyspy-dev/test.py (0ms)
rchiodo commented 2 months ago

Thanks for the issue but this is by design. Your type indicates the value can only be a str. If you want Pylance to think that it's possible it's not a string, you'd do something like so:

def foo(bar: Any):
    if not isinstance(bar, str):
        raise TypeError

For more information see this similar issue: https://github.com/microsoft/pylance-release/issues/6131#issuecomment-2227140366

debonte commented 2 months ago

Also see https://github.com/microsoft/pylance-release/issues/6106, which proposes the ability to disable our unreachability hints.

And https://github.com/microsoft/pylance-release/issues/5647 which is an earlier issue discussing reachability from a library author's perspective.

pierreBodineau commented 2 months ago

Thanks for the issue but this is by design. Your type indicates the value can only be a str.

I understand that, but python being a dinamically typed language I think these guard clauses can still relevant.

If you want Pylance to think that it's possible it's not a string, you'd do something like so:

def foo(bar: Any):
    if not isinstance(bar, str):
        raise TypeError

That would be very counterproductive!

For more information see this similar issue: #6131 (comment)

Similar, but very different though, as it's not raising an error.

Also see #6106, which proposes the ability to disable our unreachability hints.

And #5647 which is an earlier issue discussing reachability from a library author's perspective.

Oh yeah, these are completely my points! Sorry for not having found them by myself before creating this duplicate issue.

Thank you both for your quick answers!