propert 0.1.2__tar.gz → 0.1.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {propert-0.1.2/src/propert.egg-info → propert-0.1.4}/PKG-INFO +1 -1
- {propert-0.1.2 → propert-0.1.4}/pyproject.toml +1 -1
- {propert-0.1.2 → propert-0.1.4}/src/propert/base.py +51 -17
- {propert-0.1.2 → propert-0.1.4}/src/propert/types.py +4 -6
- {propert-0.1.2 → propert-0.1.4/src/propert.egg-info}/PKG-INFO +1 -1
- {propert-0.1.2 → propert-0.1.4}/tests/test_classproperties.py +26 -0
- {propert-0.1.2 → propert-0.1.4}/tests/test_mypy.py +0 -1
- {propert-0.1.2 → propert-0.1.4}/LICENSE +0 -0
- {propert-0.1.2 → propert-0.1.4}/README.md +0 -0
- {propert-0.1.2 → propert-0.1.4}/setup.cfg +0 -0
- {propert-0.1.2 → propert-0.1.4}/src/propert/__init__.py +0 -0
- {propert-0.1.2 → propert-0.1.4}/src/propert/autoproperty.py +0 -0
- {propert-0.1.2 → propert-0.1.4}/src/propert/classproperty.py +0 -0
- {propert-0.1.2 → propert-0.1.4}/src/propert/common.py +0 -0
- {propert-0.1.2 → propert-0.1.4}/src/propert/meta.py +0 -0
- {propert-0.1.2 → propert-0.1.4}/src/propert/shorthands.py +0 -0
- {propert-0.1.2 → propert-0.1.4}/src/propert/staticproperty.py +0 -0
- {propert-0.1.2 → propert-0.1.4}/src/propert.egg-info/SOURCES.txt +0 -0
- {propert-0.1.2 → propert-0.1.4}/src/propert.egg-info/dependency_links.txt +0 -0
- {propert-0.1.2 → propert-0.1.4}/src/propert.egg-info/requires.txt +0 -0
- {propert-0.1.2 → propert-0.1.4}/src/propert.egg-info/top_level.txt +0 -0
- {propert-0.1.2 → propert-0.1.4}/tests/test_metas.py +0 -0
- {propert-0.1.2 → propert-0.1.4}/tests/test_shorthands.py +0 -0
- {propert-0.1.2 → propert-0.1.4}/tests/test_staticproperties.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: propert
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: @propert - Static and class-level properties in python
|
|
5
5
|
Project-URL: Homepage, https://github.com/maddinkunze/propert
|
|
6
6
|
Project-URL: Documentation, https://github.com/maddinkunze/propert/blob/main/README.md
|
|
@@ -15,7 +15,9 @@ class _base_property(Generic[T, R, S]):
|
|
|
15
15
|
self._mcls_setter_enabled: bool|None = None
|
|
16
16
|
self._mcls_deleter_enabled: bool|None = None
|
|
17
17
|
|
|
18
|
-
self.
|
|
18
|
+
self._cached_value_map: dict[Any, R] = {}
|
|
19
|
+
self._cached_value_default = cache_default
|
|
20
|
+
self._cached_value_defaults_read = set[Any]()
|
|
19
21
|
|
|
20
22
|
self._set_getter(getter)
|
|
21
23
|
self._set_setter(setter, can_be_none=True)
|
|
@@ -71,15 +73,21 @@ class _base_property(Generic[T, R, S]):
|
|
|
71
73
|
if not self._getter:
|
|
72
74
|
raise AttributeError("Property cannot be retrieved because no getter was declared")
|
|
73
75
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
cache_key = self._get_cache_key(instance, owner)
|
|
77
|
+
if self._cache and (cache_key in self._cached_value_map):
|
|
78
|
+
return self._cached_value_map[cache_key]
|
|
76
79
|
|
|
77
|
-
|
|
80
|
+
if self._cache and (cache_key not in self._cached_value_defaults_read) and (not isinstance(self._cached_value_default, _NoValueT)):
|
|
81
|
+
self._cached_value_defaults_read.add(cache_key)
|
|
82
|
+
self._cached_value_map[cache_key] = self._cached_value_default
|
|
83
|
+
return self._cached_value_default
|
|
84
|
+
|
|
85
|
+
value = cast(R, self._getter(*self._get_args(instance, owner)))
|
|
78
86
|
|
|
79
87
|
if self._cache:
|
|
80
|
-
self.
|
|
88
|
+
self._cached_value_map[cache_key] = value
|
|
81
89
|
|
|
82
|
-
return
|
|
90
|
+
return value
|
|
83
91
|
|
|
84
92
|
def __get__(self, instance: T|None, owner: Type[T]) -> R:
|
|
85
93
|
return self._get(instance, owner)
|
|
@@ -102,13 +110,15 @@ class _base_property(Generic[T, R, S]):
|
|
|
102
110
|
def _set(self, instance: T|None, owner: Type[T], value: S) -> None:
|
|
103
111
|
if not self._setter:
|
|
104
112
|
raise AttributeError("Property cannot be set, because no setter was declared")
|
|
105
|
-
|
|
106
|
-
if self._cache and (
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
113
|
+
new_value = self._setter(*self._get_args(instance, owner), value)
|
|
114
|
+
if self._cache and (new_value is not _NoValue):
|
|
115
|
+
cache_key = self._get_cache_key(instance, owner)
|
|
116
|
+
if new_value is _CacheReset:
|
|
117
|
+
self._reset_cache(cache_key)
|
|
118
|
+
else:
|
|
119
|
+
self._cached_value_map[cache_key] = new_value
|
|
120
|
+
|
|
121
|
+
def __set__(self, instance: T, value: S) -> None: # type: ignore[misc]
|
|
112
122
|
return self._set(instance, type(instance), value)
|
|
113
123
|
|
|
114
124
|
def deleter(self, func: _ArgCallable) -> Self:
|
|
@@ -131,11 +141,28 @@ class _base_property(Generic[T, R, S]):
|
|
|
131
141
|
raise AttributeError("Property cannot be deleted because no deleter was declared")
|
|
132
142
|
|
|
133
143
|
if reset_cache is True:
|
|
134
|
-
self.
|
|
144
|
+
cache_key = self._get_cache_key(instance, owner)
|
|
145
|
+
self._reset_cache(cache_key)
|
|
135
146
|
|
|
136
|
-
def __delete__(self, instance: T) -> None:
|
|
147
|
+
def __delete__(self, instance: T) -> None: # type: ignore[misc]
|
|
137
148
|
self._del(instance, type(instance))
|
|
138
149
|
|
|
150
|
+
def _reset_cache(self, key: Any) -> None:
|
|
151
|
+
if key is None:
|
|
152
|
+
self._cached_value_map.clear()
|
|
153
|
+
return
|
|
154
|
+
|
|
155
|
+
if key not in self._cached_value_map:
|
|
156
|
+
return
|
|
157
|
+
del self._cached_value_map[key]
|
|
158
|
+
|
|
159
|
+
def _get_cache_key(self, instance: T|None, owner: Type[T]|None) -> Any:
|
|
160
|
+
if owner is None:
|
|
161
|
+
return None
|
|
162
|
+
if self._pass_class:
|
|
163
|
+
return owner
|
|
164
|
+
return None
|
|
165
|
+
|
|
139
166
|
def __set_name__(self, owner: Type[T], name: str) -> None:
|
|
140
167
|
self._mcls_setter_enabled = _check_feature_enabled(owner, _propert_setters_name)
|
|
141
168
|
self._mcls_deleter_enabled = _check_feature_enabled(owner, _propert_deleters_name)
|
|
@@ -160,5 +187,12 @@ class _base_property(Generic[T, R, S]):
|
|
|
160
187
|
class _cached_property_mixin(Generic[R]):
|
|
161
188
|
NO_VALUE = _NoValue
|
|
162
189
|
CACHE_RESET = _CacheReset
|
|
163
|
-
def reset_cache(self) -> None:
|
|
164
|
-
|
|
190
|
+
def reset_cache(self, cls: Type[T]|None) -> None:
|
|
191
|
+
"""
|
|
192
|
+
Reset this properties cache for the given class
|
|
193
|
+
If you pass None, the cache for all classes (including subclasses and parent classes) will be reset
|
|
194
|
+
"""
|
|
195
|
+
if not isinstance(self, _base_property):
|
|
196
|
+
return
|
|
197
|
+
cache_key = self._get_cache_key(None, cls)
|
|
198
|
+
self._reset_cache(cache_key)
|
|
@@ -2,13 +2,11 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
from typing_extensions import Type, TypeVar, TypeAliasType, ParamSpec, Callable, Concatenate, Any, TYPE_CHECKING
|
|
4
4
|
|
|
5
|
-
T = TypeVar("T")
|
|
6
|
-
R = TypeVar("R")
|
|
7
|
-
S = TypeVar("S")
|
|
5
|
+
T = TypeVar("T", covariant=True)
|
|
6
|
+
R = TypeVar("R", covariant=True)
|
|
7
|
+
S = TypeVar("S", contravariant=True)
|
|
8
8
|
P = TypeVar("P")
|
|
9
|
-
|
|
10
|
-
#Setter = TypeVar("Setter")
|
|
11
|
-
#Deleter = TypeVar("Deleter")
|
|
9
|
+
|
|
12
10
|
_ArgCallable = Callable[..., Any]
|
|
13
11
|
Params = ParamSpec("Params")
|
|
14
12
|
_AnyAction = TypeVar("_AnyAction")
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: propert
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: @propert - Static and class-level properties in python
|
|
5
5
|
Project-URL: Homepage, https://github.com/maddinkunze/propert
|
|
6
6
|
Project-URL: Documentation, https://github.com/maddinkunze/propert/blob/main/README.md
|
|
@@ -48,6 +48,32 @@ def test_cached_classproperty() -> None:
|
|
|
48
48
|
del Test.class_prop # should not reset, because the currently stored value is >0
|
|
49
49
|
assert Test.class_prop == 100
|
|
50
50
|
|
|
51
|
+
def test_inherited_classproperty() -> None:
|
|
52
|
+
class Test(PropertBase):
|
|
53
|
+
internal = 0
|
|
54
|
+
@classproperty
|
|
55
|
+
def class_prop(cls) -> int:
|
|
56
|
+
return cls.internal
|
|
57
|
+
|
|
58
|
+
class SubTest(Test):
|
|
59
|
+
internal = 100
|
|
60
|
+
|
|
61
|
+
assert Test.class_prop == 0
|
|
62
|
+
assert SubTest.class_prop == 100
|
|
63
|
+
|
|
64
|
+
def test_inherited_cached_classproperty() -> None:
|
|
65
|
+
class Test(PropertBase):
|
|
66
|
+
internal = 0
|
|
67
|
+
@classproperty(cache=True)
|
|
68
|
+
def class_prop(cls) -> int:
|
|
69
|
+
return cls.internal
|
|
70
|
+
|
|
71
|
+
class SubTest(Test):
|
|
72
|
+
internal = 100
|
|
73
|
+
|
|
74
|
+
assert Test.class_prop == 0
|
|
75
|
+
assert SubTest.class_prop == 100
|
|
76
|
+
|
|
51
77
|
def test_introspected_classproperty() -> None:
|
|
52
78
|
class Test(PropertBase):
|
|
53
79
|
internal = 0
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|