Skip to content

Incorrect inference of Never after empty loop, properties and use of Generic #11178

@Dr-Irv

Description

@Dr-Irv

Describe the bug

pyright infers a property as Never after the property is referenced in an empty loop and the property is returning a Generic

Code or Screenshots

from typing import Generic, TypeVar, reveal_type

T = TypeVar("T", str, int)


class Index(Generic[T]):
    def __init__(self, thelist: list[T]):
        self.thelist = thelist

    def __contains__(self, item: T) -> bool:
        return item in self.thelist


class DataFrame:
    @property
    def columns(self) -> Index[str]: ...
    @columns.setter
    def columns(self, cols: list[str]) -> None: ...


df = DataFrame()
df.columns = ["a", "b", "c"]
reveal_type(df.columns)  # Expected: Index[str]

for _ in ():
    if "abc" in df.columns:
        raise ValueError("not there")

reveal_type(df.columns)  # Expected: Index[str]  gets Never

pyright reports:

tstgennever.py
tstgennever.py:23:13 - information: Type of "df.columns" is "Index[str]"
tstgennever.py:29:13 - information: Type of "df.columns" is "Never"

Note that if the property is not a generic (i.e., just a list), it works fine. If the loop does not have the raise it works fine as well.

VS Code extension or command-line
python 3.11.14
pyrright 1.1.407 on command line with default settings

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions