Skip to content

Indexed assignment incorrectly narrows Any values #4488

Description

@dillydill123

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions