From 5ec357915bbe13a664392ca245d996d00e27acb8 Mon Sep 17 00:00:00 2001 From: karlicoss Date: Sat, 17 Aug 2024 12:59:03 +0100 Subject: [PATCH] core.common: add test for classproperty --- my/core/common.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/my/core/common.py b/my/core/common.py index 225ff2c..dcd1074 100644 --- a/my/core/common.py +++ b/my/core/common.py @@ -5,6 +5,7 @@ from pathlib import Path from typing import ( TYPE_CHECKING, Callable, + Generic, Iterable, List, Sequence, @@ -109,11 +110,12 @@ def get_files( return tuple(paths) -from typing import Callable, Generic, TypeVar - _R = TypeVar('_R') + # https://stackoverflow.com/a/5192374/706389 +# NOTE: it was added to stdlib in 3.9 and then deprecated in 3.11 +# seems that the suggested solution is to use custom decorator? class classproperty(Generic[_R]): def __init__(self, f: Callable[..., _R]) -> None: self.f = f @@ -122,6 +124,19 @@ class classproperty(Generic[_R]): return self.f(cls) +def test_classproperty() -> None: + from .compat import assert_type + + class C: + @classproperty + def prop(cls) -> str: + return 'hello' + + res = C.prop + assert res == 'hello' + assert_type(res, str) + + # hmm, this doesn't really work with mypy well.. # https://github.com/python/mypy/issues/6244 # class staticproperty(Generic[_R]):