Skip to content

Inference question: empty list and .append() call #8066

Answered by erictraut
jrheard-seesaw asked this question in Q&A
Discussion options

You must be logged in to vote

Pyright's type inference philosophy is based on the principle that the type of an object is established at the time it is constructed. You can't, for example, say that a list object starts its life as a list[str] and later change your mind and decide that you want it to be a list[str | int]. All subsequent operations are validated based on the type of an object that it had when it was created. The methods that can be safely called on that object are based on its type. Its type is not influenced by the methods that are called on it; that would be backward.

The same principle applies here. At the time the list is constructed, its type is list[Unknown] because there is no additional context …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jrheard-seesaw
Comment options

Answer selected by jrheard-seesaw
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