S5644 "Class" does not have a "__getitem__" method for user-defined generic class

Product: SonarCloud
Python Version: 3.7

from typing import Generic
from typing import TypeVar

T = TypeVar("T")

class Foo(Generic[T]):
    pass

foo = Foo[str]()

will incorrectly trigger: “Foo” does not have a __getitem__ method.

Hi @Dan_Felicetta ,

Welcome to our community!
It looks like I cannot reproduce the FP you reported: sonarcloud reproducer.

Could you please clarify if something else is needed to reproduce the false positive?

hey @Andrea_Guarino …sorry I think I might have misread where it was triggering. Can you try this:

from typing import Generic
from typing import TypeVar

T = TypeVar("T")

class Foo(Generic[T]):
    pass

class Bar(Foo[T]):
    pass

and see if it triggers on the definition of the Bar class?

sorry…it looks like it’s related specifically to typing.Iterable. so this should reproduce:

from typing import Iterable
from typing import TypeVar

T = TypeVar("T")

class Foo(Iterable[T]):
    pass

class Bar(Foo[T]):
    pass

Hello @Dan_Felicetta,

Thanks for the reproducer, I managed to reproduce the false positive. I created this ticket to track it. Hopefully we can fix this soon.

Cheers,
Guillaume

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.