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
Python version (& distribution if applicable, e.g. Anaconda): 3.12.1 (microsoft store)
Code Snippet
q = deque([("button", "broadcaster", False)])
a = str("foo")
b = str("bar")
c = len(q) == 0
item = (a, b, c)
q.append(item)
Repro Steps
Once the code is loaded, view that the call to q.append(item) throws a general type issue.
Expected behavior
The code reports no type error, because q is obviously a deque handling entries of type tuple[str, str, bool].
Actual behavior
The code results in the following type error:
"tuple[str, str, bool]" is incompatible with "tuple[Literal['button'], Literal['broadcaster'], Literal[False]]"
Tuple entry 1 is incorrect type
"str" cannot be assigned to type "Literal['button']" Pylance(reportGeneralTypeIssues)
Logs
There's not anything specific to this error in the logs. I've captured the parts specific to the environment and the specific file being analyzed.
Pyright (the type checker upon which pylance is built) is working as designed here. This isn't a bug. Refer to this documentation for information about how pyright infers types from expressions. In particular, here's the relevant section on inference for tuples.
Any inference strategy employed by a type checker will inevitably produce results that are incorrect in some situations. Pyright's inference behavior has been tuned over many years to produce the best results most of the time. At this point, we're unlikely to change these rules because many typed Python code bases that use pyright for type checking assume the current rules.
In your code sample, the recommended approach is to provide an explicit type declaration for variable q. Pyright then uses bidirectional type inference, and it's able to infer the type that you would like in this situation.
This appears to be a similar issue to #4633
Environment data
Code Snippet
Repro Steps
Once the code is loaded, view that the call to
q.append(item)
throws a general type issue.Expected behavior
The code reports no type error, because
q
is obviously adeque
handling entries of typetuple[str, str, bool]
.Actual behavior
The code results in the following type error:
Logs
There's not anything specific to this error in the logs. I've captured the parts specific to the environment and the specific file being analyzed.
The text was updated successfully, but these errors were encountered: