Derived from the flake8-pyi linter.
What it does#
Checks for methods that are annotated with a fixed return type, which
should instead be returning
Why is this bad?#
If methods like
__enter__ are annotated with a fixed return
type, and the class is subclassed, type checkers will not be able to infer
the correct return type.
class Shape: def set_scale(self, scale: float) -> Shape: self.scale = scale return self class Circle(Shape): def set_radius(self, radius: float) -> Circle: self.radius = radius return self # This returns `Shape`, not `Circle`. Circle().set_scale(0.5) # Thus, this expression is invalid, as `Shape` has no attribute `set_radius`. Circle().set_scale(0.5).set_radius(2.7)
Specifically, this check enforces that the return type of the following
- In-place binary operations, like
__aenter__, if those methods return the class name.
__iter__methods that return
Iterator, despite the class inheriting directly from
__aiter__methods that return
AsyncIterator, despite the class inheriting directly from