facebook / pyre-check

Performant type-checking for python.
https://pyre-check.org/
MIT License
6.86k stars 437 forks source link

Incorrect handling of list multiplication results with Union types #932

Open nARN opened 1 month ago

nARN commented 1 month ago

Pyre Bug

Bug description When a list is made by multiplying another list pyre incorrectly rejects it if the type is declared as a wider, but still inclusive union

Reproduction steps

l: list[int | None]
l = [None] * 42  # Incompatible variable type [9]: l is declared to have type `List[Optional[int]]` but is used as type `List[None]`.

# The following passes type checking
l = [None]
l = [None, None]
l = [None for _ in range(42)]

Same behaviour is observed with other union types, not necessarily including None

Expected behavior [None] * 42 should be a valid value for list[int | None] and pass type checking

Logs Please include any relevant logs here:

$ pyre
ƛ Found 1 type error!
src/test.py:2:0 Incompatible variable type [9]: l is declared to have type `List[Optional[int]]` but is used as type `List[None]`.

Please run your reproduction steps followed by pyre rage > pyre_rage.log, and upload the file here:

pyre_rage.log

Additional context I tried the repro in an freshly created venv with only pyre_check-0.9.22 installed on top of the initial content