I have a use case where I would like to define a common function in a base class, but annotate the return type from an overridden property in a subclass. This is a silly example, but clearly demonstrates what I would like to do:
from typing import Any class BaseCls: RETURNS_TYPE: Any = NotImplemented # This is what I am not sure how to do, this does not work # Have tried a few different things, but hoping someone just knows def cast_to_return_type(self: 'BaseCls', value: Any) -> 'BaseCls.RETURNS_TYPE': return some_work_that_returns_self_RETURNS_TYPE() class IntCls(BaseCls): RETURNS_TYPE = int class StrCls(BaseCls): RETURNS_TYPE = str How can I type hint the return value of cast_to_return_type so that a a regular old IDE would be able to detect what the return type should be when doing StrCls().cast_to_return_type(some_var) (tried on pycharm and vscode, neither knew what to do any decided Any was the return type, which makes sense if it cant infer the child class value)