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

Pattern matching list literals yields incorrect type check errors #2475

Closed
MC-DeltaT opened this issue Mar 15, 2022 · 2 comments
Closed

Pattern matching list literals yields incorrect type check errors #2475

MC-DeltaT opened this issue Mar 15, 2022 · 2 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@MC-DeltaT
Copy link

MC-DeltaT commented Mar 15, 2022

If a list literal is used directly as the argument to a match statement, then Pylance seems to want a wildcard case, or else it thinks the match will fall through, even if the other cases cover all possible types.
If the list is bound to a variable instead, then no type check error occurs, as expected.

Environment data

  • Language Server version: 2022.3.1
  • OS and version: Windows 10 build 19044.1586
  • Python version: 3.10.2

Expected behaviour

The code passes the type check with no errors regardless of whether the list is a variable or literal.

Actual behaviour

Pylance reports an error:

Function with declared type of "int" must return value
  Type "None" cannot be assigned to type "int"

Logs

[Info  - 12:30:04 pm] (10944) Pylance language server 2022.3.1 (pyright bd47ac0b) starting
[Info  - 12:30:04 pm] (10944) Server root directory: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist
[Info  - 12:30:04 pm] (10944) No pyproject.toml file found.
[Info  - 12:30:04 pm] (10944) Setting pythonPath for service "<default>": "C:\Users\Reece Jones\AppData\Local\Programs\Python\Python310\python.exe"
[Warn  - 12:30:04 pm] (10944) stubPath typings is not a valid directory.
[Info  - 12:30:04 pm] (10944) Assuming Python platform Windows
[Info  - 12:30:04 pm] (10944) Search paths for <default>
[Info  - 12:30:04 pm] (10944)   c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib
[Info  - 12:30:04 pm] (10944)   typings
[Info  - 12:30:04 pm] (10944)   c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stubs\...
[Info  - 12:30:04 pm] (10944)   c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\bundled\stubs
[Info  - 12:30:04 pm] (10944) Searching for source files
[Info  - 12:30:04 pm] (10944) No source files found.
(10944) [IDX(FG)] index libraries  (index) ...
(10944) [IDX(FG)]   read stdlib indices (38ms)
(10944) [IDX(FG)] index libraries  (index) [succeed] (38ms)
(10944) [FG] parsing: c:\Users\Reece Jones\Desktop\test.py (12ms)
(10944) [FG] parsing: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 3ms] (104ms)
(10944) [FG] binding: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\builtins.pyi (33ms)
(10944) [FG] binding: c:\Users\Reece Jones\Desktop\test.py (0ms)
[Info  - 12:30:05 pm] (10944) Background analysis(1) root directory: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist
[Info  - 12:30:05 pm] (10944) Background analysis(1) started
(10944) Background analysis message: setConfigOptions
(10944) Background analysis message: setImportResolver
(10944) Background analysis message: ensurePartialStubPackages
(10944) Background analysis message: setTrackedFiles
(10944) Background analysis message: markAllFilesDirty
(10944) Background analysis message: setFileOpened
(10944) Background analysis message: getDiagnosticsForRange
(10944) Background analysis message: getSemanticTokens full
(10944) [BG(1)] getSemanticTokens full at c:\Users\Reece Jones\Desktop\test.py ...
(10944) [BG(1)]   parsing: c:\Users\Reece Jones\Desktop\test.py (11ms)
(10944) [BG(1)]   parsing: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 2ms] (99ms)
(10944) [BG(1)]   binding: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\builtins.pyi (36ms)
(10944) [BG(1)]   binding: c:\Users\Reece Jones\Desktop\test.py (1ms)
(10944) [BG(1)]   parsing: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\typing_extensions.pyi [fs read 2ms] (7ms)
(10944) [BG(1)]   binding: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\typing_extensions.pyi (1ms)
(10944) [BG(1)]   parsing: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\typing.pyi [fs read 0ms] (35ms)
(10944) [BG(1)]   binding: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\typing.pyi (11ms)
(10944) [BG(1)]   parsing: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi [fs read 0ms] (7ms)
(10944) [BG(1)]   binding: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi (1ms)
(10944) [BG(1)]   parsing: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\types.pyi [fs read 1ms] (24ms)
(10944) [BG(1)]   binding: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\types.pyi (6ms)
(10944) [BG(1)]   parsing: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\abc.pyi [fs read 0ms] (2ms)
(10944) [BG(1)]   binding: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\abc.pyi (0ms)
(10944) [BG(1)] getSemanticTokens full at c:\Users\Reece Jones\Desktop\test.py (263ms)
(10944) Background analysis message: analyze
(10944) [BG(1)] analyzing: c:\Users\Reece Jones\Desktop\test.py ...
(10944) [BG(1)]   checking: c:\Users\Reece Jones\Desktop\test.py ...
(10944) [BG(1)]     parsing: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\collections\abc.pyi [fs read 1ms] (1ms)
(10944) [BG(1)]     binding: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\collections\abc.pyi ...
(10944) [BG(1)]       parsing: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\_collections_abc.pyi [fs read 1ms] (2ms)
(10944) [BG(1)]       binding: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\_collections_abc.pyi (1ms)
(10944) [BG(1)]     binding: c:\Users\Reece Jones\.vscode\extensions\ms-python.vscode-pylance-2022.3.1\dist\typeshed-fallback\stdlib\collections\abc.pyi (4ms)
(10944) [BG(1)]   checking: c:\Users\Reece Jones\Desktop\test.py (25ms)
(10944) [BG(1)] analyzing: c:\Users\Reece Jones\Desktop\test.py (25ms)
(10944) Background analysis message: getDiagnosticsForRange
(10944) Background analysis message: getSemanticTokens range
(10944) [BG(1)] getSemanticTokens range 0:0 - 6:0 at c:\Users\Reece Jones\Desktop\test.py (0ms)
(10944) Background analysis message: resumeAnalysis

Code Snippet / Additional information

Causes error:

def test() -> int:
    match [10]:
        case [value]:
            return value
        case [*values]:
            return values[0]

Doesn't cause error:

def test() -> int:
    l = [10]
    match l:
        case [value]:
            return value
        case [*values]:
            return values[0]
@erictraut
Copy link
Contributor

Thanks for the bug report. This will be fixed in the next release.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Mar 15, 2022
@bschnurr
Copy link
Member

This issue has been fixed in version 2022.3.2, which we've just released. You can find the changelog here: CHANGELOG.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants