Skip to content

Variable value verified with a tuple and not a set or a list #9810

Answered by erictraut
adrien-lsh asked this question in Q&A
Discussion options

You must be logged in to vote

This behavior is intended. Literal values are retained when inferring the type of a tuple expression but are not when inferring the type of a list, dict or set expression. This is because list, dict and set objects are mutable. If literals were retained when inferring their types, it would be very inconvenient. For example, if you had the statement x = [1], you probably wouldn't want the type of x to be inferred as list[Literal[1]]. For more details about pyright's inference behaviors for container expressions, refer to this documentation.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@adrien-lsh
Comment options

Answer selected by adrien-lsh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants