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 infers Literal[] values instead of matching standard types #5295

Closed
hrunting opened this issue Dec 20, 2023 · 1 comment
Closed

Pylance infers Literal[] values instead of matching standard types #5295

hrunting opened this issue Dec 20, 2023 · 1 comment
Assignees

Comments

@hrunting
Copy link

This appears to be a similar issue to #4633

Environment data

  • Language Server version: 2023.12.1
  • OS and version: Windows 11 23H2
  • Python version (& distribution if applicable, e.g. Anaconda): 3.12.1 (microsoft store)

Code Snippet


q = deque([("button", "broadcaster", False)])

a = str("foo")
b = str("bar")
c = len(q) == 0
item = (a, b, c)

q.append(item)

Repro Steps

Once the code is loaded, view that the call to q.append(item) throws a general type issue.

Expected behavior

The code reports no type error, because q is obviously a deque handling entries of type tuple[str, str, bool].

Actual behavior

The code results in the following type error:

  "tuple[str, str, bool]" is incompatible with "tuple[Literal['button'], Literal['broadcaster'], Literal[False]]"
    Tuple entry 1 is incorrect type
      "str" cannot be assigned to type "Literal['button']" Pylance(reportGeneralTypeIssues)

Logs

There's not anything specific to this error in the logs. I've captured the parts specific to the environment and the specific file being analyzed.

2023-12-20 13:24:30.226 [info] (13404) No configuration file found.
2023-12-20 13:24:30.227 [info] (13404) pyproject.toml file found at C:\Users\hrunting\Code\advent-of-code.
2023-12-20 13:24:30.227 [info] [Info  - 1:24:30 PM] (13404) Setting pythonPath for service "advent-of-code": "C:\Users\hrunting\AppData\Local\Microsoft\WindowsApps\python3.12.exe"
2023-12-20 13:24:30.227 [info] [Info  - 1:24:30 PM] (13404) Setting environmentName for service "advent-of-code": "3.12.1 (microsoft store)"
2023-12-20 13:24:30.228 [info] [Info  - 1:24:30 PM] (13404) Loading pyproject.toml file at C:\Users\hrunting\Code\advent-of-code\pyproject.toml
2023-12-20 13:24:30.229 [info] [Info  - 1:24:30 PM] (13404) Pyproject file "C:\Users\hrunting\Code\advent-of-code\pyproject.toml" has no "[tool.pyright]" section.
2023-12-20 13:24:30.328 [info] [Info  - 1:24:30 PM] (13404) Assuming Python version 3.12
2023-12-20 13:24:30.329 [info] (13404) Assuming Python platform Windows
2023-12-20 13:24:30.458 [info] [Info  - 1:24:30 PM] (13404) Search paths for C:\Users\hrunting\Code\advent-of-code
2023-12-20 13:24:30.459 [info] [Info  - 1:24:30 PM] (13404)   C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib
2023-12-20 13:24:30.459 [info] [Info  - 1:24:30 PM] (13404)   C:\Users\hrunting\Code\advent-of-code
2023-12-20 13:24:30.459 [info] [Info  - 1:24:30 PM] (13404)   C:\Users\hrunting\Code\advent-of-code\src
2023-12-20 13:24:30.459 [info] [Info  - 1:24:30 PM] (13404)   C:\Users\hrunting\Code\advent-of-code\lib\python
2023-12-20 13:24:30.459 [info] [Info  - 1:24:30 PM] (13404)   C:\Users\hrunting\Code\advent-of-code\typings
2023-12-20 13:24:30.459 [info] [Info  - 1:24:30 PM] (13404)   C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stubs\...
2023-12-20 13:24:30.460 [info] [Info  - 1:24:30 PM] (13404)   C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\bundled\stubs
2023-12-20 13:24:30.460 [info] [Info  - 1:24:30 PM] (13404)   C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\DLLs
2023-12-20 13:24:30.460 [info] [Info  - 1:24:30 PM] (13404)   C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\Lib
2023-12-20 13:24:30.460 [info] [Info  - 1:24:30 PM] (13404)   C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0
2023-12-20 13:24:30.460 [info] [Info  - 1:24:30 PM] (13404)   C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\Lib\site-packages
2023-12-20 13:24:30.460 [info] [Info  - 1:24:30 PM] (13404) Adding fs watcher for library directories:
 C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\DLLs
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\Lib
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\Lib\site-packages
2023-12-20 13:24:30.461 [info] [Info  - 1:24:30 PM] (13404) Adding fs watcher for directories:
 C:\Users\hrunting\Code\advent-of-code
2023-12-20 13:24:30.461 [info] (13404) Searching for source files
2023-12-20 13:24:30.622 [info] [Info  - 1:24:30 PM] (13404) Found 207 source files
2023-12-20 13:24:30.641 [info] (13404) pytest configurations: {"message":"script","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
2023-12-20 13:24:30.685 [info] (13404) [BG(1)] analyzing: C:\Users\hrunting\Code\advent-of-code\src\2023\20\test.py ...
2023-12-20 13:24:30.685 [info] (13404) [BG(1)]   parsing: C:\Users\hrunting\Code\advent-of-code\src\2023\20\test.py (29ms)
2023-12-20 13:24:30.730 [info] (13404) [BG(1)]   parsing: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 0ms] (44ms)
2023-12-20 13:24:30.741 [info] (13404) [BG(1)]   binding: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\builtins.pyi (11ms)
2023-12-20 13:24:30.741 [info] (13404) [BG(1)]   binding: C:\Users\hrunting\Code\advent-of-code\src\2023\20\test.py (0ms)
2023-12-20 13:24:30.755 [info] (13404) [BG(1)]   checking: C:\Users\hrunting\Code\advent-of-code\src\2023\20\test.py ...
2023-12-20 13:24:30.756 [info] (13404) [BG(1)]     parsing: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing.pyi [fs read 0ms] (11ms)
2023-12-20 13:24:30.760 [info] (13404) [BG(1)]     binding: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing.pyi (2ms)
2023-12-20 13:24:30.764 [info] (13404) [BG(1)]     parsing: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing_extensions.pyi [fs read 0ms] (3ms)
2023-12-20 13:24:30.765 [info] (13404) [BG(1)]     binding: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\typing_extensions.pyi (1ms)
2023-12-20 13:24:30.774 [info] (13404) [BG(1)]     parsing: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi [fs read 0ms] (6ms)
2023-12-20 13:24:30.775 [info] (13404) [BG(1)]     binding: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi (1ms)
2023-12-20 13:24:30.784 [info] (13404) [BG(1)]     parsing: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\types.pyi [fs read 0ms] (9ms)
2023-12-20 13:24:30.787 [info] (13404) [BG(1)]     binding: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\types.pyi (3ms)
2023-12-20 13:24:30.789 [info] (13404) [BG(1)]     parsing: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\abc.pyi [fs read 0ms] (0ms)
2023-12-20 13:24:30.790 [info] (13404) [BG(1)]     binding: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\abc.pyi (0ms)
2023-12-20 13:24:30.796 [info] (13404) [BG(1)]     parsing: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\collections\__init__.pyi [fs read 0ms] (6ms)
2023-12-20 13:24:30.799 [info] (13404) [BG(1)]     binding: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\collections\__init__.pyi (3ms)
2023-12-20 13:24:30.799 [info] (13404) [BG(1)]     parsing: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\collections\abc.pyi [fs read 0ms] (0ms)
2023-12-20 13:24:30.800 [info] (13404) [BG(1)]     binding: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\collections\abc.pyi ...
2023-12-20 13:24:30.800 [info] (13404) [BG(1)]       parsing: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\_collections_abc.pyi [fs read 0ms] (0ms)
2023-12-20 13:24:30.801 [info] (13404) [BG(1)]       binding: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\_collections_abc.pyi (1ms)
2023-12-20 13:24:30.801 [info] (13404) [BG(1)]     binding: C:\Users\hrunting\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib\collections\abc.pyi (2ms)
2023-12-20 13:24:30.810 [info] (13404) [BG(1)]   checking: C:\Users\hrunting\Code\advent-of-code\src\2023\20\test.py (69ms)
2023-12-20 13:24:30.810 [info] [Info  - 1:24:30 PM] (13404) Heap stats: total_heap_size=76MB, used_heap_size=65MB, total_physical_size=76MB, total_available_size=4028MB, heap_size_limit=4096MB
2023-12-20 13:24:30.810 [info] (13404) [BG(1)] analyzing: C:\Users\hrunting\Code\advent-of-code\src\2023\20\test.py (153ms)
2023-12-20 13:24:30.812 [info] (13404) pytest configurations: {"message":"script","classes":["Test"],"files":["test_*.py","*_test.py"],"functions":["test"]}
...
2023-12-20 13:24:31.424 [info] (13404) [BG(1)] indexing: C:\Users\hrunting\Code\advent-of-code\src\2023\20\test.py [found 5] (0ms)
2023-12-20 13:24:31.424 [info] [Info  - 1:24:31 PM] (13404) Heap stats: total_heap_size=97MB, used_heap_size=78MB, total_physical_size=97MB, total_available_size=4015MB, heap_size_limit=4096MB
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Dec 20, 2023
@erictraut
Copy link
Contributor

Pyright (the type checker upon which pylance is built) is working as designed here. This isn't a bug. Refer to this documentation for information about how pyright infers types from expressions. In particular, here's the relevant section on inference for tuples.

Any inference strategy employed by a type checker will inevitably produce results that are incorrect in some situations. Pyright's inference behavior has been tuned over many years to produce the best results most of the time. At this point, we're unlikely to change these rules because many typed Python code bases that use pyright for type checking assume the current rules.

In your code sample, the recommended approach is to provide an explicit type declaration for variable q. Pyright then uses bidirectional type inference, and it's able to infer the type that you would like in this situation.

q: deque[tuple[str, str, bool]] = deque([("button", "broadcaster", False)])

@debonte debonte added by design and removed needs repro Issue has not been reproduced yet labels Dec 20, 2023
@debonte debonte closed this as completed Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants