Describe the Bug
Indexing into Any narrows a type:
from typing import Any, reveal_type
values: list[float] = []
frame: Any = object()
reveal_type(frame)
frame["start"] = values
reveal_type(frame["start"])
frame["end"] = frame["start"].shift(-1) # ERROR: Object of class 'list' has no attribute `shift` [missing-attribute]
This code is obviously bad and won't run, but it shows the error. A value typed as Any should retain Any behavior through indexed access.
I observed this error with replace-imports-with-any, in particular with geopandas. Essentially the real world code was something like:
distance: ndarray[...] = ...
frame = geopandas.GeoDataFrame()
frame["column_a"] = distance
frame["column_b"] = frame["column_a"].shift(-1) # ERROR: [missing-attribute]
I have geopandas.* in replace-imports-with-any. I think pyrefly sees that frame["column_a"] got assigned an ndarray, and then later thinks frame["column_a"] produces the same ndarray, when geopandas converts it to a Series, of which shift is valid.
Mypy reveals both frame and frame["start"] as Any and reports no issues.
Sandbox Link
https://pyrefly.org/sandbox/?project=v2.lZQxD4IwEIX_ygUGcIDGld0ZwwokIBRoopS0h_Hne23FgBoSJ5Lj3qXt--79G5mbsLFoLEvWURphSYfM6ZadIisTI6GC49R4t1KHtoVq9psXHrGosPDMBDf3R_uqbaXkY-t0nz2xHkSHYXQ8APhwyrI0SyB16yQ7cHEQmNMHMJDvo4QaUYnLjBwqK64gvwlNq9JH71_lt_U-nF0BXq9IoUfM9DNFK7EQF6NPqcphQJx0wtgil6pnfGStbDTbKBgYyut7LWxgb8eBtKhqGrvD3KS45mhLhK6pOAhj_piuohEY1daeNXq7rDwB
(Only applicable for extension issues) IDE Information
No response
Describe the Bug
Indexing into
Anynarrows a type:This code is obviously bad and won't run, but it shows the error. A value typed as
Anyshould retainAnybehavior through indexed access.I observed this error with
replace-imports-with-any, in particular withgeopandas. Essentially the real world code was something like:I have
geopandas.*inreplace-imports-with-any. I thinkpyreflysees thatframe["column_a"]got assigned anndarray, and then later thinksframe["column_a"]produces the samendarray, whengeopandasconverts it to aSeries, of whichshiftis valid.Mypy reveals both
frameandframe["start"]asAnyand reports no issues.Sandbox Link
https://pyrefly.org/sandbox/?project=v2.lZQxD4IwEIX_ygUGcIDGld0ZwwokIBRoopS0h_Hne23FgBoSJ5Lj3qXt--79G5mbsLFoLEvWURphSYfM6ZadIisTI6GC49R4t1KHtoVq9psXHrGosPDMBDf3R_uqbaXkY-t0nz2xHkSHYXQ8APhwyrI0SyB16yQ7cHEQmNMHMJDvo4QaUYnLjBwqK64gvwlNq9JH71_lt_U-nF0BXq9IoUfM9DNFK7EQF6NPqcphQJx0wtgil6pnfGStbDTbKBgYyut7LWxgb8eBtKhqGrvD3KS45mhLhK6pOAhj_piuohEY1daeNXq7rDwB
(Only applicable for extension issues) IDE Information
No response