Python: S5864 FP using type[Enum]

  • SonarQube Server Enterprise Edition v2025.4.3 (113915)
  • Deployed using Docker

I’m getting a false positive for Python static code analysis when trying to dynamically look up the value of a specific key for Enum subclasses. Here’s a simplified example that clearly shows the false positive:

from enum import Enum

def enum_value(enum_class: type[Enum], key: str):
    return enum_class[key]  # FP S5864 on this line

The last line of this file triggers python:S5864 with the error description:

Fix this “_getitem_” operation; Previous type checks suggest that “enum_class” does not have this method.

This is a false positive as enum_class is declared to be a subclass of Enum, and all Enum classes support dynamic key lookup using __getitem__: enum — Support for enumerations — Python 3.14.0 documentation

Hi,

Thanks for this report. We have a ticket in the backlog that I believe matches this. I would give you the link, but the project is private. Here’s the description tho:

 
Ann