I have a class that should mimic behaviour of other class but with some flavour. For example Kotlin have delegation pattern (https://www.baeldung.com/kotlin/delegation-pattern) as a part of language to do such thing. But for Python when I try code below:
from dataclasses import dataclass
from typing import Generic, T
@dataclass
class Wrapper(T, Generic[T]):
__value__: T
def __getattr__(self, item):
return getattr(self.__value__, item)
# also need to delegate all magick methods
def __len__(self):
return self.__value__.__len__()
def try_some_funny_things(self):
setattr(__builtins__, "True", False)
funny_string = Wrapper[str]()
funny_string. # I want typehints as for str class here
I get the following error:
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
What are my final purposes:
- Make PyCharm/Pylint (other typechecker) show fields/methods hints
- Do not delegate all magic methods manually to value field
Any suggestion how can I do something like this in Python 3?