-
|
Code sample in pyright playground from typing import Literal
type DiffLine = tuple[Literal[" ", "+", "-"], str]
diff: list[DiffLine] = []
reveal_type(diff) # Type of "diff" is "list[tuple[Literal[' ', '+', '-'], str]]"
changes = {change for change, _ in diff}
reveal_type(changes) # set[str]I'm surprised that |
Beta Was this translation helpful? Give feedback.
Answered by
erictraut
Aug 21, 2025
Replies: 1 comment
-
|
This is expected behavior. Literal values are not retained when inferring the type of a list, dict, or set expression. You can override the default inference rules by adding a type annotation on the target variable. Pyright will then use bidirectional type inference. changes: set[Literal[" ", "+", "-"]] = {change for change, _ in diff} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
injust
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is expected behavior. Literal values are not retained when inferring the type of a list, dict, or set expression. You can override the default inference rules by adding a type annotation on the target variable. Pyright will then use bidirectional type inference.