Relevant older questions:
- Class properties in Python 3.11+
- What are some similar alternatives to using classmethod and property decorators together?
Out of all of them, the following answer was most appealing to me. A small rewrite + example (I am using Python 3.12+):
from functools import update_wrapper
from typing import Callable
class classprop[T]:
def __init__(self, method: Callable[..., T]):
self.method = method
update_wrapper(self, method)
def __get__(self, obj, cls=None) -> T:
if cls is None:
cls = type(obj)
return self.method(cls)
class MyClass:
@classprop
def hello(self) -> str:
return "world"
x = MyClass()
x.hello
This raises a type error (silenced in the original answer):
Argument of type "Self@classprop[T@classprop]" cannot be assigned to
parameter "wrapper" of type "(**_PWrapper@update_wrapper) -> _RWrapper@update_wrapper"
in function "update_wrapper"
Type "Self@classprop[T@classprop]" is incompatible with
type "(**_PWrapper@update_wrapper) -> _RWrapper@update_wrapper"
Is there a way to address this type error, instead of silencing it? (i.e. is it raising a valid concern?) What is the error even saying?