Skip to content

generic-function

Pythonstyleno fixdefault: offpython/ruff

Reports a function outside a class whose parameters use a module-level TypeVar, ParamSpec or TypeVarTuple, when the target_version has type parameters (3.12 and later).

Since Python 3.12 a function declares its type variables itself, def first[T](xs: list[T]) -> T:, so the variable is scoped to the function and a module-level declaration is not needed.

To fix, declare the type parameters on the function.

Reported
...
from typing import TypeVar
T = TypeVar("T")
def f(x: T) -> T:
return x
def g() -> T:
...
python:
lint:
rules:
generic-function: warn # off (default), warn, error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line generic-function 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 UP047 full