Skip to content

generic-class

Pythonstyleno fixdefault: offpython/ruff

Reports a class that has Generic[...] among its bases when the target_version has type parameters (3.12 and later).

Since Python 3.12 a class declares its type variables in the header, class Box[T]:, and the TypeVar and the Generic base are no longer needed. The old form needs a separate declaration that can be mistaken for an unrelated variable.

To fix, move the type variables to the class header.

Reported
...
from typing import Generic, TypeVar
T = TypeVar("T")
class A(Generic[T]):
pass
from typing import TypeVar
...
python:
lint:
rules:
generic-class: warn # off (default), warn, error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line generic-class in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
ruff UP046 full