Skip to content

Commit

Permalink
core.common: add test for classproperty
Browse files Browse the repository at this point in the history
  • Loading branch information
karlicoss committed Aug 17, 2024
1 parent 245ad22 commit 5ec3579
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions my/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import (
TYPE_CHECKING,
Callable,
Generic,
Iterable,
List,
Sequence,
Expand Down Expand Up @@ -109,11 +110,12 @@ def wrap(p: Path) -> Path:
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
Expand All @@ -122,6 +124,19 @@ def __get__(self, obj, cls) -> _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]):
Expand Down

0 comments on commit 5ec3579

Please sign in to comment.