pyright icon indicating copy to clipboard operation
pyright copied to clipboard

typing.Literal with tuple argument not understood

Open dcnieho opened this issue 6 months ago • 3 comments

Environment data

  • VSCode version: 1.91.1
  • Pylance version: 2024.8.1
  • OS and version: Windows 11, Version 10.0.22631
  • Python version: CPython 3.10.9, VSCode extension 2024.12.2

Code Snippet

import typing

values = ('a','b','c')
t = typing.Literal[values]

def get_values() -> tuple[int]:
    return (1,2,3)
t1 = typing.Literal[get_values()]

Repro Steps

put code in empty .py file, see Pylance analysis output

Expected behavior

No squiggles, Pylance correctly deduces t as typing.Literal['a','b','c'] and t1 as typing.Literal[1,2,3] (or, given that that is runtime, at least no squiggle)

Actual behavior

When run, code works correctly (you can instantiate a typing.Literal with a tuple of values), but Pylance provides yellow squiggles under values and get_values() with the following errors:

Variable not allowed in type expression Pylance[reportInvalidTypeForm](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportInvalidTypeForm)
Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum valuePylance[reportInvalidTypeForm](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportInvalidTypeForm)
(variable) values: Any

and

Call expression not allowed in type expression Pylance[reportInvalidTypeForm](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportInvalidTypeForm)
Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum valuePylance[reportInvalidTypeForm](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportInvalidTypeForm)
(function) def get_values() -> tuple[int]

respectively

dcnieho avatar Aug 07 '24 21:08 dcnieho