prefer-self-type-annotation
Prefer Self for self-returning methods and alternate constructors.
Why
Self preserves the concrete subclass type when an inherited method returns its receiver or constructs through its class receiver.
Fix
Import Self from typing (or typing_extensions on Python before 3.11) and use it as the return annotation.
Examples
from typing import final
class Builder: @final def set_name(self, name: str) -> "Builder": self.name = name return selffrom typing import Self, final
class Builder: @final def set_name(self, name: str) -> Self: self.name = name return self