Skip to content

[pyright] DataFrame.columns becomes Never by pyright after assignment and raise in loop #1560

@cmp0xff

Description

@cmp0xff

Describe the bug

After an assignment and a raise in a loop, DataFrame.columns becomes Never by pyright. See the minimal example below.

It is likely a pyright bug, but I can't make an example that is independent of pandas and pandas-stubs.

To Reproduce

Provide a minimal runnable pandas example that is not properly checked by the stubs.

from typing_extensions import reveal_type
import pandas as pd

df = pd.DataFrame()

df.columns = [str(c).upper() for c in df.columns]

reveal_type(df.columns)  # Index[str], fine

for _ in ():
    if "1" in df.columns:
        raise

reveal_type(df.columns)  # Never

Indicate which type checker you are using

pyright. mypy does not have this problem.

Show the error message received from that type checker while checking your example

ttest.py
  ttest.py:8:13 - information: Type of "df.columns" is "Index[str]"
  ttest.py:14:13 - information: Type of "df.columns" is "Never"
0 errors, 0 warnings, 2 informations

Please complete the following information

  • OS: Windows
  • OS Version: 11
  • python version: 3.10.11
  • version of type checker:
  • version of installed pandas-stubs: main, b4c3b56

Metadata

Metadata

Assignees

No one assigned

    Labels

    DataFrameDataFrame data structureIndexingRelated to indexing on series/frames, not to indexes themselvespyright bug

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions