-
Hi everyone! I'm trying to type a decorator that returns a Wrapper class of another class. The idea is that from typing import Type, TypeVar, Generic
_T = TypeVar("_T")
class Wrapper(Generic[_T]):
wrap: Type[_T]
enhanced: bool = True
def __init__(self, cls: Type[_T]) -> None:
self.wrap = cls
def __call__(self, *args, **kwargs): ...
def enhance(cls: Type[_T]) -> Type[_T]:
return Wrapper(cls) # type: ignore
class X:
...
EnhanceX = enhance(X)
reveal_type(EnhanceX)
reveal_type(EnhanceX.enhanced) The user using the class doesn't really need to know that it has been "enhanced", as it should work as it was before. My initial idea was to extend There's also a possibility where I'm doing something silly with what I'm trying to do, but it should make sense for my use case 😊 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
I don't undestand this one. In your current example class X:
class_prop = 1
EnhanceX = enhance(X) 1, I can't find similarities between them. Can you please provide more details? |
Beta Was this translation helpful? Give feedback.
I don't undestand this one.
In your current example
X
andEnhanceX
are two very different things:1,
X
is a type,EnhanceX
is an instance2. any (potential)
X.class_prop
calls won't workI can't find similarities between them. Can you please provide more details?