Skip to content

Bug: __class_getitem__ not recognised #14111

Description

@samsja

Bug Report

I am experienced a very weird behavior (which I am now pretty sure is a bug) with __class_getitem__.

To Reproduce

from abc import abstractmethod
from typing import Iterable


class A(Iterable):
    @abstractmethod
    def __class_getitem__(cls, item):
        ...


class B(A):
    def f(self, field: str):

        self.__class__[str]

>>> attribute.py:14: error: Value of type "Type[B]" is not indexable  [index]

Here I expect that mypy know that __class__ is bounded by the class B and therefore implement __class_getitem__.

The weirdest part is that removing the parameter in f remove the problem

from abc import abstractmethod
from typing import Iterable


class A(Iterable):
    @abstractmethod
    def __class_getitem__(cls, item):
        ...


class B(A):
    def f(self):

        self.__class__[str]

>>> Success: no issues found in 1 source file

Expected Behavior

mypy recognize that class can be indexed

Actual Behavior

mypy said it is no indexable even though B implement __class_getitem__

Your Environment

  • Mypy version used: 0.990

  • Mypy command-line flags: None

  • Mypy configuration options from mypy.ini (and other config files): plugins = "pydantic.mypy"

  • Python version used: 3.9

FYI: a more advance setup in this PR docarray/docarray#803

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong
    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