custom-type-var-return
What it does
Section titled “What it does”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.
Why it matters
Section titled “Why it matters”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.
Example
Section titled “Example”...from typing import TypeVarT = TypeVar("T")... def f(self: T) -> T: return self
from typing import Any...Configuration
Section titled “Configuration”python: lint: rules: custom-type-var-return: warn # off (default), warn, errorEnabled 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.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| ruff | PYI019 |
full |