Skip to content

False positive E1136: unsubscriptable-object when generic class defines __class_getitem__ #10360

Open
@couteau

Description

@couteau

Bug description

possibly related to #3488

when a generic class includes a definition of __class_getitem__, instances created from an annotated class are flagged as "unsubscriptable-object" even when the class itself also includes a __getitem__ method.

Example test.py file:

"""__class_getitem__ test"""
from typing import TypeVar, Generic

T = TypeVar("T")

class Test(Generic[T]):
    """Test class"""
    def __class_getitem__(cls, *args):
        return super().__class_getitem__(*args)

    def __init__(self, iterable):
        super().__init__()
        self._list: list[T] = list(iterable)

    def __getitem__(self, index):
        return self._list[index]

t = Test[int]([1, 2, 3])

print(t[1])

Command used

pylint -E test.py

Pylint output

************* Module test
test.py:21:6: E1136: Value 't' is unsubscriptable (unsubscriptable-object)

Expected behavior

the instance t is subscriptable, so no error should be reported

Pylint version

pylint 3.3.4
astroid 3.3.8
Python 3.9.21 | packaged by conda-forge | (main, Dec  5 2024, 13:47:18)
[Clang 18.1.8 ]

OS / Environment

MacOS, ARM (M1), running Sequoia

Metadata

Metadata

Assignees

No one assigned

    Labels

    False Positive 🦟A message is emitted but nothing is wrong with the codeNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions