You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using complex Literal types to define the key-value types of a dictionary, Pylance reports an "Illegal type annotation" error if the Literal types are not defined as separate type aliases.
Environment data
Language Server version: Pylance language server 2023.4.20 (pyright 64a33975)
OS and version: macOS Monterey 12.6 (21G115)
Python version (& distribution if applicable, e.g. Anaconda): 3.11.2 (installed via homebrew)
Code Snippet
fromtypingimportLiteralFoo, Bar=int, strd: dict[Foo, Bar] = {1: "a", 2: "b"}
GoodFoo=Literal[1, 2]
GoodBar=Literal["a", "b"]
good_d: dict[GoodFoo, GoodBar] = {1: "a", 2: "b"}
SameFoo, SameBar=Literal[1, 2], Literal["a", "b"]
same_d: dict[SameFoo, SameBar] = {1: "a", 2: "b"}
# Illegal type annotation: variable not allowed unless it is a type alias Pylance reportGeneralTypeIssues
Repro Steps
I offer two options but there might be better ones:
use correct type definition instead of the last one in the file.
(compromise) report error when user reassigns a type definition
Expected behavior
No error on assigning multiple type definitions with tuple destructuring
This behavior is correct. You cannot use variables in type annotations. In your example, SameFoo and SameBar are variables.
You are allowed to use type aliases in type annotations. Python doesn't (yet) have a dedicated syntax for declaring type annotations, as most languages do. Traditionally, a type alias is defined using a simple assignment statement where the LHS is a simple name and the RHS is an expression that is a legal type annotation. An example is:
SameBar=Literal["a", "b"]
Note that tuple-based assignments are not legal type alias declarations, which is why the symbols SameFoo and SameBar in your code sample above are considered regular variables and not type aliases.
PEP 613 introduced the notion of an "explicit type alias" using the symbol typing.TypeAlias.
SameBar: TypeAlias=Literal["a", "b"]
PEP 695, which was just accepted by the Python Steering Council for inclusion in Python 3.12, introduces a dedicated syntax for declaring type aliases. It takes the form:
type SameBar=Literal["a", "b"]
Pylance already has support for PEP 695, but it is obviously not yet supported by the Python interpreter.
When using complex Literal types to define the key-value types of a dictionary, Pylance reports an "Illegal type annotation" error if the Literal types are not defined as separate type aliases.
Environment data
Code Snippet
Repro Steps
I offer two options but there might be better ones:
Expected behavior
No error on assigning multiple type definitions with tuple destructuring
Actual behavior
Pylance reported an error
Logs
log.txt (same as #4260)
The text was updated successfully, but these errors were encountered: