Skip to content

custom-type-var-return

Pythonstyleno fixdefault: offpython/ruff

Reports a method whose self parameter is annotated with a type variable that the return annotation uses too, as in def f(self: T) -> T.

typing.Self (Python 3.11) states “the type of this instance” without declaring a type variable, and it is the form that type checkers and readers know. The type variable has to be declared, bound and imported for the same meaning.

To fix, drop the annotation of self and return Self.

Reported
...
from typing import TypeVar
T = TypeVar("T")
...
def f(self: T) -> T:
return self
from typing import Any
...
python:
lint:
rules:
custom-type-var-return: warn # off (default), warn, error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line custom-type-var-return 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 PYI019 full