mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-22 08:15:36 +08:00
986 B
986 B
What it does
Checks for enum classes that are also generic.
Why is this bad?
Enum classes cannot be generic. Python does not support generic enums: attempting to create one will
either result in an immediate TypeError at runtime, or will create a class that cannot be
specialized in the way that a normal generic class can.
Examples
[environment]
python-version = "3.12"
from enum import Enum
from typing import Generic, TypeVar
T = TypeVar("T")
# enum class cannot be generic (class creation fails with `TypeError`)
class E[T](Enum): # error
A = 1
# enum class cannot be generic (class creation fails with `TypeError`)
class F(Enum, Generic[T]): # error
A = 1
# enum class cannot be generic -- the class creation does not immediately fail...
class G(Generic[T], Enum): # error
A = 1
# ...but this raises `KeyError`:
x: G[int]