Skip to content

Incorrectly inferred Pandas indexing return type #851

@kormalev

Description

@kormalev

Summary

ty incorrectly thinks Pandas boolean indexing returns Series.

Reproducible example:

import pandas as pd


def filter_data(data: pd.DataFrame) -> pd.DataFrame:
    return data[(data["a"] == 1)]


data = pd.DataFrame(
    {
        "a": [1, 2, 3, 4],
        "b": [4, 3, 2, 1],
    }
)

print(filter_data(data))
> ty version
ty 0.0.1-alpha.14 (3ececb07e 2025-07-08)
> ty check pandas-demo.py
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
error[invalid-return-type]: Return type does not match returned value
 --> pandas-demo.py:4:40
  |
4 | def filter_data(data: pd.DataFrame) -> pd.DataFrame:
  |                                        ------------ Expected `DataFrame` because of return type
5 |     return data[data["a"] == 1]
  |            ^^^^^^^^^^^^^^^^^^^^ expected `DataFrame`, found `Series[Any]`
  |
info: rule `invalid-return-type` is enabled by default

Found 1 diagnostic

Pandas version is 2.3.0

Version

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