Inferring Literal types not always done #1534
Labels
addressed in next version
Issue is fixed and will appear in next published version
bug
Something isn't working
Describe the bug
If you loop over values corresponding to a
Literal
type, thenpyright
doesn't infer that the values in the loop are from theLiteral
typeTo Reproduce
See example below. pyright reports issues with the assignment of
d1
andd2
. Note thatmypy
reports an issue with the assignment ofd1
but notd2
Expected behavior
Ideally, neither line would be flagged. But it would be at least an improvement if
pyright
didn't report the error on the assignment ofd2
thatmypy
acceptsScreenshots or Code
VS Code extension or command-line
pyright 1.1.114 command line (also 1.1.112 in VS Code)
Additional context
This kind of code is kind of ugly, because you have to specify the possible values twice - in the type declaration and in the list. Ideally, there would be ONE place to show the possible values.
A possible workaround is to use
Enum
, as follows, butpyright
complains here about the hack (found at python/typing#535 (comment)) in thereturn
ofchoices
(as does mypy):The text was updated successfully, but these errors were encountered: