Код: Выделить всё
from __future__ import annotations
class M:
def set_width(self, width: int)->M:
self.width = width
return self
def set_height(self, height: int)->M:
self.height = height
return self
Код: Выделить всё
box = M().set_width(5).set_height(10)
Код: Выделить всё
class M3D(M):
def set_depth(self, depth: int) -> M3D:
self.depth = depth
return self
Код: Выделить всё
cube = M3D().set_width(2).set_height(3).set_depth(5)
Код: Выделить всё
_test_typeanotations.py:21: error: "M" has no attribute "set_depth"; maybe "set_width"
Это также актуально для специальных методов, например __enter__ традиционно возвращает self, поэтому было бы неплохо иметь способ указать это без необходимости даже упоминать об этом в подклассах.
Подробнее здесь: https://stackoverflow.com/questions/665 ... turns-self