Skip to content

Matching generic return type of SupportsGetItem #21731

Description

@cmp0xff

Bug Report

mypy does not match the generic return type of SupportsGetItem properly.

This was proposed in astral-sh/ty#3988 (comment).

To Reproduce

https://mypy-play.net/?gist=b8bb3085f195314a6c7c89ed2bdd2576

from __future__ import annotations

from typing import TYPE_CHECKING, Any, TypeVar

if TYPE_CHECKING:
    from _typeshed import SupportsGetItem

class Series: ...

class DataFrame:
    def __getitem__(self, key: object) -> Series:
        return Series()

T = TypeVar("T")

def f(x: SupportsGetItem[Any, T]) -> T:
    if isinstance(x, DataFrame):
        # return Series()
        return 1  # false error-free
    else:
        return x[0]

Expected Behaviour

Report an error about return type

Actual Behaviour

No error

Your Environment

mypy playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions