core.common: add test for classproperty
This commit is contained in:
parent
245ad22057
commit
5ec357915b
1 changed files with 17 additions and 2 deletions
|
@ -5,6 +5,7 @@ from pathlib import Path
|
||||||
from typing import (
|
from typing import (
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
Callable,
|
Callable,
|
||||||
|
Generic,
|
||||||
Iterable,
|
Iterable,
|
||||||
List,
|
List,
|
||||||
Sequence,
|
Sequence,
|
||||||
|
@ -109,11 +110,12 @@ def get_files(
|
||||||
return tuple(paths)
|
return tuple(paths)
|
||||||
|
|
||||||
|
|
||||||
from typing import Callable, Generic, TypeVar
|
|
||||||
|
|
||||||
_R = TypeVar('_R')
|
_R = TypeVar('_R')
|
||||||
|
|
||||||
|
|
||||||
# https://stackoverflow.com/a/5192374/706389
|
# 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]):
|
class classproperty(Generic[_R]):
|
||||||
def __init__(self, f: Callable[..., _R]) -> None:
|
def __init__(self, f: Callable[..., _R]) -> None:
|
||||||
self.f = f
|
self.f = f
|
||||||
|
@ -122,6 +124,19 @@ class classproperty(Generic[_R]):
|
||||||
return self.f(cls)
|
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..
|
# hmm, this doesn't really work with mypy well..
|
||||||
# https://github.com/python/mypy/issues/6244
|
# https://github.com/python/mypy/issues/6244
|
||||||
# class staticproperty(Generic[_R]):
|
# class staticproperty(Generic[_R]):
|
||||||
|
|
Loading…
Add table
Reference in a new issue