S5644 False positive in Python - no `__get_item__` method (actually a subclass generic type hint)

Getting a false positive on S5644 when I subclass using generic type hints based on the new generic syntax in Python 3.12.

(This is a variation of this ticket, which according to Jira was fixed in sonarlint 3.13, but I’m still getting the error in sonarlint 4.12.)

As far as I can tell, the problem is that I’m using the new syntax for generics introduced in Python 3.12.

Here’s a spot in our codebase where it’s not crying foul (first line is from here, second is from here):

# Using old, define-a-typevar-separately-and-subclass-from-`Generic` syntax
class AvatarMixin(Generic[AvatarT]): ...

# No error here
class DocIntegrationAvatarEndpoint(AvatarMixin[DocIntegrationAvatar]): ...

And here’s a spot where it is (first line is from here, second is from here):

# Using new, define-the-typevar-in-line syntax
class BaseGroupingComponent[ValuesType: str | int | BaseGroupingComponent[Any]]: ...

# False positive error here ('Fix this code; "BaseGroupingComponent" does not have a "__getitem__" method.')
class ContextLineGroupingComponent(BaseGroupingComponent[str]): ...

My details:

  • Operating system: MacOS Sonoma 14.6.1
  • SonarLint plugin version: 4.12.0
  • Programming language you’re coding in: Python 3.13
  • VSCode version: 1.95.3
  • Is connected mode used: No
1 Like

Hello @lobsterkatie,

Thank you for reporting this false positive. Indeed, the new syntax for generics is not supported by this rule, which leads to the false positive. We are currently working on our type inference engine to better model generics and we plan to address this in the upcoming weeks. I have created SONARPY-2370 to track this.

Cheers,
Guillaume