pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Treat `False` as `0` and `True` as `1` in subscripts

Open grievejia opened this issue 9 months ago • 3 comments

Describe the Bug

Minimal repro:

t = ("a", "b")
reveal_type(t[False])
reveal_type(t[True])

Expected: Reveals Literal["a"] and Literal["b"] Actual: Reveals Literal["a", "b"]

Sandbox Link

https://pyrefly.org/sandbox/?code=C4AgvCAUBECG0BoTQEbQJQCgBOBTAbrrADYD6wAngA66TADaAYiQM64C6WehJ51tDACrYArh3RA

(Only applicable for extension issues) IDE Information

No response

grievejia avatar May 18 '25 16:05 grievejia

Is this a common pattern? Right now the tuple subscripting stuff matches on the AST node as opposed to the evaluated type of the slice, but we could probably change that.

yangdanny97 avatar May 19 '25 14:05 yangdanny97

Right now the tuple subscripting stuff matches on the AST node as opposed to the evaluated type of the slice

That seems undesirable. We probably need to change it to be literal type based instead. @ndmitchell agreed.

grievejia avatar May 19 '25 15:05 grievejia

it depends on whether the __getitem__ implementation calls key.__index__():

>>> False.__index__()
0
>>> True.__index__()
1

and this is usually the case for sequence-like types

jorenham avatar May 19 '25 22:05 jorenham