Closed lyj0309 closed 1 month ago
Pyright is working correctly here.
The issue has to do with the way bidirectional type inference works in a static type checker like pyright.
The call to random.choice
is expected to return a type compatible with str
, since that's the type of the value required to satisfy a call to test
. If random.choice
returns a str
, then the input must be of type SupportsLenAndGetItem[str]
, since str
is a subtype of Sequence[str]
. The value data
in this case is not compatible with SupportsLenAndGetItem[str]
, so an error is reported to that effect.
Describe the bug I am encountering an incorrect type checking error when using random.choice on a list of tuples and passing the result to a function that expects a str.
Steps to Reproduce:
Create a list of tuples as follows:
Define a function that expects a str:
Use random.choice to select a random tuple from the list and pass it to the function:
Expected Behavior:
The type checker should raise an error indicating that the argument type tuple[str, str] cannot be assigned to the parameter type str in the test function.
Actual Behavior:
The type checker incorrectly reports an error on the use of random.choice(data) with the following message:
Code or Screenshots
![image](https://github.com/microsoft/pyright/assets/50474995/2454cf70-842d-46b8-a274-ad5890584dc5)