propert 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,397 @@
1
+ from typing_extensions import Generic, Self, Literal, Callable, Any, TYPE_CHECKING, overload
2
+
3
+ from .base import _base_property, _cached_property_mixin
4
+ from .types import (
5
+ T, R, S, _NoValueT, _NoValue,
6
+
7
+ _instancemethod_getter_default, _instancemethod_setter_default, _instancemethod_deleter_default,
8
+ _instancemethod_getter_cache, _instancemethod_setter_cache, _instancemethod_deleter_cache,
9
+ _instancemethod_getter_introspect, _instancemethod_setter_introspect, _instancemethod_deleter_introspect,
10
+ _instancemethod_getter_cache_introspect, _instancemethod_setter_cache_introspect, _instancemethod_deleter_cache_introspect,
11
+
12
+ _classmethod_getter_default, _classmethod_setter_default, _classmethod_deleter_default,
13
+ _classmethod_getter_cache, _classmethod_setter_cache, _classmethod_deleter_cache,
14
+ _classmethod_getter_introspect, _classmethod_setter_introspect, _classmethod_deleter_introspect,
15
+ _classmethod_getter_cache_introspect, _classmethod_setter_cache_introspect, _classmethod_deleter_cache_introspect,
16
+ )
17
+
18
+ class _classproperty(Generic[T, R, S], _base_property[T, R, S], pass_class=True, special_unwrap=classmethod):
19
+ if TYPE_CHECKING:
20
+ @overload
21
+ def __init__(self,
22
+ getter: _classmethod_getter_default[T, R],
23
+ setter: _classmethod_setter_default[T, S]|None=None,
24
+ deleter: _classmethod_deleter_default[T]|None=None,
25
+ *,
26
+ cache_default: R|_NoValueT=_NoValue,
27
+ check_metaclass: bool=True,
28
+ ) -> None: ...
29
+
30
+ @overload
31
+ def __init__(self,
32
+ getter: _instancemethod_getter_default[T, R],
33
+ setter: _instancemethod_setter_default[T, S]|None=None,
34
+ deleter: _instancemethod_deleter_default[T]|None=None,
35
+ *,
36
+ cache_default: R|_NoValueT=_NoValue,
37
+ check_metaclass: bool=True,
38
+ ) -> None: ...
39
+
40
+ def __init__(self, *args: Any, **kwargs: Any) -> None: ...
41
+
42
+ @overload
43
+ def getter(self, getter: _classmethod_getter_default[T, R]) -> Self: ...
44
+
45
+ @overload
46
+ def getter(self, getter: _instancemethod_getter_default[T, R]) -> Self: ...
47
+
48
+ def getter(self, *args: Any, **kwargs: Any) -> Self: ...
49
+
50
+ @overload
51
+ def setter(self, setter: _classmethod_setter_default[T, S]) -> Self: ...
52
+
53
+ @overload
54
+ def setter(self, setter: _instancemethod_setter_default[T, S]) -> Self: ...
55
+
56
+ def setter(self, *args: Any, **kwargs: Any) -> Self: ...
57
+
58
+ @overload
59
+ def deleter(self, deleter: _classmethod_deleter_default[T]) -> Self: ...
60
+
61
+ @overload
62
+ def deleter(self, deleter: _instancemethod_deleter_default[T]) -> Self: ...
63
+
64
+ def deleter(self, *args: Any, **kwargs: Any) -> Self: ...
65
+
66
+ class cached_classproperty(Generic[T, R, S], _base_property[T, R, S], _cached_property_mixin[R], pass_class=True, cache=True, special_unwrap=classmethod):
67
+ if TYPE_CHECKING:
68
+ @overload
69
+ def __init__(self,
70
+ getter: _classmethod_getter_cache[T, R],
71
+ setter: _classmethod_setter_cache[T, R, S]|None=None,
72
+ deleter: _classmethod_deleter_cache[T]|None=None,
73
+ *,
74
+ cache_default: R|_NoValueT=_NoValue,
75
+ check_metaclass: bool=True,
76
+ ) -> None: ...
77
+
78
+ @overload
79
+ def __init__(self,
80
+ getter: _instancemethod_getter_cache[T, R],
81
+ setter: _instancemethod_setter_cache[T, R, S]|None=None,
82
+ deleter: _instancemethod_deleter_cache[T]|None=None,
83
+ *,
84
+ cache_default: R|_NoValueT=_NoValue,
85
+ check_metaclass: bool=True,
86
+ ) -> None: ...
87
+
88
+ def __init__(self, *args: Any, **kwargs: Any) -> None: ...
89
+
90
+ @overload
91
+ def getter(self, getter: _classmethod_getter_cache[T, R]) -> Self: ...
92
+
93
+ @overload
94
+ def getter(self, getter: _instancemethod_getter_cache[T, R]) -> Self: ...
95
+
96
+ def getter(self, *args: Any, **kwargs: Any) -> Self: ...
97
+
98
+ @overload
99
+ def setter(self, setter: _classmethod_setter_cache[T, R, S]) -> Self: ...
100
+
101
+ @overload
102
+ def setter(self, setter: _instancemethod_setter_cache[T, R, S]) -> Self: ...
103
+
104
+ def setter(self, *args: Any, **kwargs: Any) -> Self: ...
105
+
106
+ @overload
107
+ def deleter(self, deleter: _classmethod_deleter_cache[T]) -> Self: ...
108
+
109
+ @overload
110
+ def deleter(self, deleter: _instancemethod_deleter_cache[T]) -> Self: ...
111
+
112
+ def deleter(self, *args: Any, **kwargs: Any) -> Self: ...
113
+
114
+ class introspected_classproperty(Generic[T, R, S], _base_property[T, R, S], pass_class=True, pass_self=True, special_unwrap=classmethod):
115
+ if TYPE_CHECKING:
116
+ @overload
117
+ def __init__(self,
118
+ getter: _classmethod_getter_introspect[T, R, Self],
119
+ setter: _classmethod_setter_introspect[T, S, Self],
120
+ deleter: _classmethod_deleter_introspect[T, Self],
121
+ *,
122
+ cache_default: R|_NoValueT=_NoValue,
123
+ check_metaclass: bool=True,
124
+ ) -> None: ...
125
+
126
+ @overload
127
+ def __init__(self,
128
+ getter: _instancemethod_getter_introspect[T, R, Self],
129
+ setter: _instancemethod_setter_introspect[T, S, Self],
130
+ deleter: _instancemethod_deleter_introspect[T, Self],
131
+ *,
132
+ cache_default: R|_NoValueT=_NoValue,
133
+ check_metaclass: bool=True,
134
+ ) -> None: ...
135
+
136
+ def __init__(self, *args: Any, **kwargs: Any) -> None: ...
137
+
138
+ @overload
139
+ def getter(self, getter: _classmethod_getter_introspect[T, R, Self]) -> Self: ...
140
+
141
+ @overload
142
+ def getter(self, getter: _instancemethod_getter_introspect[T, R, Self]) -> Self: ...
143
+
144
+ def getter(self, *args: Any, **kwargs: Any) -> Self: ...
145
+
146
+ @overload
147
+ def setter(self, setter: _classmethod_setter_introspect[T, S, Self]) -> Self: ...
148
+
149
+ @overload
150
+ def setter(self, setter: _instancemethod_setter_introspect[T, S, Self]) -> Self: ...
151
+
152
+ def setter(self, *args: Any, **kwargs: Any) -> Self: ...
153
+
154
+ @overload
155
+ def deleter(self, deleter: _classmethod_deleter_introspect[T, Self]) -> Self: ...
156
+
157
+ @overload
158
+ def deleter(self, deleter: _instancemethod_deleter_introspect[T, Self]) -> Self: ...
159
+
160
+ def deleter(self, *args: Any, **kwargs: Any) -> Self: ...
161
+
162
+ class introspected_cached_classproperty(Generic[T, R, S], _base_property[T, R, S], _cached_property_mixin[R], pass_class=True, pass_self=True, cache=True, special_unwrap=classmethod):
163
+ if TYPE_CHECKING:
164
+ @overload
165
+ def __init__(self,
166
+ getter: _classmethod_getter_cache_introspect[T, R, Self],
167
+ setter: _classmethod_setter_cache_introspect[T, R, S, Self],
168
+ deleter: _classmethod_deleter_cache_introspect[T, Self],
169
+ *,
170
+ cache_default: R|_NoValueT=_NoValue,
171
+ check_metaclass: bool=True,
172
+ ) -> None: ...
173
+
174
+ @overload
175
+ def __init__(self,
176
+ getter: _instancemethod_getter_cache_introspect[T, R, Self],
177
+ setter: _instancemethod_setter_cache_introspect[T, R, S, Self],
178
+ deleter: _instancemethod_deleter_cache_introspect[T, Self],
179
+ *,
180
+ cache_default: R|_NoValueT=_NoValue,
181
+ check_metaclass: bool=True,
182
+ ) -> None: ...
183
+
184
+ def __init__(self, *args: Any, **kwargs: Any) -> None: ...
185
+
186
+ @overload
187
+ def getter(self, getter: _classmethod_getter_cache_introspect[T, R, Self]) -> Self: ...
188
+
189
+ @overload
190
+ def getter(self, getter: _instancemethod_getter_cache_introspect[T, R, Self]) -> Self: ...
191
+
192
+ def getter(self, *args: Any, **kwargs: Any) -> Self: ...
193
+
194
+ @overload
195
+ def setter(self, setter: _classmethod_setter_cache_introspect[T, R, S, Self]) -> Self: ...
196
+
197
+ @overload
198
+ def setter(self, setter: _instancemethod_setter_cache_introspect[T, R, S, Self]) -> Self: ...
199
+
200
+ def setter(self, *args: Any, **kwargs: Any) -> Self: ...
201
+
202
+ @overload
203
+ def deleter(self, deleter: _classmethod_deleter_cache_introspect[T, Self]) -> Self: ...
204
+
205
+ @overload
206
+ def deleter(self, deleter: _instancemethod_deleter_cache_introspect[T, Self]) -> Self: ...
207
+
208
+ def deleter(self, *args: Any, **kwargs: Any) -> Self: ...
209
+
210
+ # Classmethod definitions
211
+ @overload
212
+ def classproperty(
213
+ getter: _classmethod_getter_default[T, R],
214
+ setter: _classmethod_setter_default[T, S]|None = None,
215
+ deleter: _classmethod_deleter_default[T]|None = None,
216
+ *,
217
+ cache: Literal[False] = False,
218
+ cache_default: _NoValueT = _NoValue,
219
+ introspect: Literal[False] = False,
220
+ check_metaclass: bool = True,
221
+ ) -> _classproperty[T, R, S]:
222
+ """Create a classproperty"""
223
+
224
+ @overload
225
+ def classproperty(
226
+ getter: _classmethod_getter_cache[T, R],
227
+ setter: _classmethod_setter_cache[T, R, S]|None = None,
228
+ deleter: _classmethod_deleter_cache[T]|None = None,
229
+ *,
230
+ cache: Literal[True],
231
+ cache_default: R|_NoValueT = _NoValue,
232
+ introspect: Literal[False] = False,
233
+ check_metaclass: bool = True,
234
+ ) -> cached_classproperty[T, R, S]:
235
+ """Create a cached classproperty"""
236
+
237
+ @overload
238
+ def classproperty(
239
+ getter: _classmethod_getter_introspect[T, R, introspected_classproperty[T, R, S]],
240
+ setter: _classmethod_setter_introspect[T, S, introspected_classproperty[T, R, S]]|None=None,
241
+ deleter: _classmethod_deleter_introspect[T, introspected_classproperty[T, R, S]]|None=None,
242
+ *,
243
+ cache: Literal[False]=False,
244
+ cache_default: _NoValueT=_NoValue,
245
+ introspect: Literal[True],
246
+ check_metaclass: bool=True,
247
+ ) -> introspected_classproperty[T, R, S]:
248
+ """Create an introspected classproperty"""
249
+
250
+ @overload
251
+ def classproperty(
252
+ getter: _classmethod_getter_cache_introspect[T, R, introspected_cached_classproperty[T, R, S]],
253
+ setter: _classmethod_setter_cache_introspect[T, R, S, introspected_cached_classproperty[T, R, S]]|None=None,
254
+ deleter: _classmethod_deleter_cache_introspect[T, introspected_cached_classproperty[T, R, S]]|None=None,
255
+ *,
256
+ cache: Literal[True],
257
+ cache_default: R|_NoValueT=_NoValue,
258
+ introspect: Literal[True],
259
+ check_metaclass: bool=True,
260
+ ) -> introspected_cached_classproperty[T, R, S]:
261
+ """Create an introspected cached classproperty"""
262
+
263
+ # Instance method definitions
264
+ @overload
265
+ def classproperty(
266
+ getter: _instancemethod_getter_default[T, R],
267
+ setter: _instancemethod_setter_default[T, S]|None = None,
268
+ deleter: _instancemethod_deleter_default[T]|None = None,
269
+ *,
270
+ cache: Literal[False] = False,
271
+ cache_default: _NoValueT = _NoValue,
272
+ introspect: Literal[False] = False,
273
+ check_metaclass: bool = True,
274
+ ) -> _classproperty[T, R, S]:
275
+ """Create a classproperty"""
276
+
277
+ @overload
278
+ def classproperty(
279
+ getter: _instancemethod_getter_cache[T, R],
280
+ setter: _instancemethod_setter_cache[T, R, S]|None = None,
281
+ deleter: _instancemethod_deleter_cache[T]|None = None,
282
+ *,
283
+ cache: Literal[True],
284
+ cache_default: R|_NoValueT = _NoValue,
285
+ introspect: Literal[False] = False,
286
+ check_metaclass: bool = True,
287
+ ) -> cached_classproperty[T, R, S]:
288
+ """Create a cached classproperty"""
289
+
290
+ @overload
291
+ def classproperty(
292
+ getter: _instancemethod_getter_introspect[T, R, introspected_classproperty[T, R, S]],
293
+ setter: _instancemethod_setter_introspect[T, S, introspected_classproperty[T, R, S]]|None=None,
294
+ deleter: _instancemethod_deleter_introspect[T, introspected_classproperty[T, R, S]]|None=None,
295
+ *,
296
+ cache: Literal[False]=False,
297
+ cache_default: _NoValueT=_NoValue,
298
+ introspect: Literal[True],
299
+ check_metaclass: bool=True,
300
+ ) -> introspected_classproperty[T, R, S]:
301
+ """Create an introspected classproperty"""
302
+
303
+ @overload
304
+ def classproperty(
305
+ getter: _instancemethod_getter_cache_introspect[T, R, introspected_cached_classproperty[T, R, S]],
306
+ setter: _instancemethod_setter_cache_introspect[T, R, S, introspected_cached_classproperty[T, R, S]]|None=None,
307
+ deleter: _instancemethod_deleter_cache_introspect[T, introspected_cached_classproperty[T, R, S]]|None=None,
308
+ *,
309
+ cache: Literal[True],
310
+ cache_default: R|_NoValueT=_NoValue,
311
+ introspect: Literal[True],
312
+ check_metaclass: bool=True,
313
+ ) -> introspected_cached_classproperty[T, R, S]:
314
+ """Create an introspected cached classproperty"""
315
+
316
+ # Deferred definitions
317
+ @overload
318
+ def classproperty(
319
+ getter: None=None,
320
+ setter: None=None,
321
+ deleter: None=None,
322
+ *,
323
+ cache: Literal[False]=False,
324
+ cache_default: _NoValueT=_NoValue,
325
+ introspect: Literal[False]=False,
326
+ check_metaclass: bool=True,
327
+ ) -> Callable[[_classmethod_getter_default[T, R]|_instancemethod_getter_default[T, R]], _classproperty[T, R, S]]:
328
+ """Return a decorator to create a classproperty"""
329
+
330
+ @overload
331
+ def classproperty(
332
+ getter: None=None,
333
+ setter: None=None,
334
+ deleter: None=None,
335
+ *,
336
+ cache: Literal[True],
337
+ cache_default: R|_NoValueT=_NoValue,
338
+ introspect: Literal[False]=False,
339
+ check_metaclass: bool=True,
340
+ ) -> Callable[[_classmethod_getter_cache[T, R]|_instancemethod_getter_cache[T, R]], cached_classproperty[T, R, S]]:
341
+ """Return a decorator to create a cached classproperty"""
342
+
343
+ @overload
344
+ def classproperty(
345
+ getter: None=None,
346
+ setter: None=None,
347
+ deleter: None=None,
348
+ *,
349
+ cache: Literal[False]=False,
350
+ cache_default: _NoValueT=_NoValue,
351
+ introspect: Literal[True],
352
+ check_metaclass: bool=True,
353
+ ) -> Callable[[_classmethod_getter_introspect[T, R, introspected_classproperty[T, R, S]]|_instancemethod_getter_introspect[T, R, introspected_classproperty[T, R, S]]], introspected_classproperty[T, R, S]]:
354
+ """Return a decorator to create an introspected classproperty"""
355
+
356
+ @overload
357
+ def classproperty(
358
+ getter: None=None,
359
+ setter: None=None,
360
+ deleter: None=None,
361
+ *,
362
+ cache: Literal[True],
363
+ cache_default: R|_NoValueT=_NoValue,
364
+ introspect: Literal[True],
365
+ check_metaclass: bool=True,
366
+ ) -> Callable[[_classmethod_getter_cache_introspect[T, R, introspected_cached_classproperty[T, R, S]]|_instancemethod_getter_cache_introspect[T, R, introspected_cached_classproperty[T, R, S]]], introspected_cached_classproperty[T, R, S]]:
367
+ """Return a decorator to create an introspected cached classproperty"""
368
+
369
+
370
+ # Actual implementation
371
+ def classproperty(
372
+ getter: Any=None,
373
+ setter: Any=None,
374
+ deleter: Any=None,
375
+ *,
376
+ cache: bool=False,
377
+ cache_default: Any=_NoValue,
378
+ introspect: bool=False,
379
+ check_metaclass: bool=True,
380
+ ) -> Any:
381
+ _cls: type|None = None
382
+
383
+ if cache and introspect:
384
+ _cls = introspected_cached_classproperty
385
+ elif cache:
386
+ _cls = cached_classproperty
387
+ elif introspect:
388
+ _cls = introspected_classproperty
389
+ else:
390
+ _cls = _classproperty
391
+
392
+ if getter:
393
+ return _cls(getter, setter, deleter, cache_default=cache_default, check_metaclass=check_metaclass)
394
+
395
+ def _decorator(getter_dec: Any) -> Any:
396
+ return _cls(getter_dec, setter, deleter, cache_default=cache_default, check_metaclass=check_metaclass)
397
+ return _decorator
propert/common.py ADDED
@@ -0,0 +1,9 @@
1
+ from typing_extensions import Type, TypeVar
2
+
3
+ T = TypeVar("T")
4
+
5
+ _propert_setters_name = "__propert_setters_enabled__"
6
+ _propert_deleters_name = "__propert_deleters_enabled__"
7
+
8
+ def _check_feature_enabled(cls: Type[T], name: str) -> bool:
9
+ return getattr(cls, name, False)
propert/meta.py ADDED
@@ -0,0 +1,105 @@
1
+ from typing_extensions import Type, Any, TYPE_CHECKING
2
+
3
+ from .common import _propert_setters_name, _propert_deleters_name
4
+ from .base import _base_property
5
+ from .types import T
6
+
7
+ _propert_meta_cache = dict[tuple[Type[type], bool, bool], Type[type]]()
8
+ def _create_propert_meta(base_meta: Type[type]=type, *, enable_setters: bool = False, enable_deleters: bool = False) -> Type[type]:
9
+ _key = (base_meta, enable_setters, enable_deleters)
10
+
11
+ _cached = _propert_meta_cache.get(_key, None)
12
+ if _cached is not None:
13
+ return _cached
14
+
15
+ class _PropertMeta(base_meta): # type: ignore
16
+ def __init__(self, name: str, bases: tuple[type, ...], namespace: dict[str, Any], /, **kwargs: Any) -> None:
17
+ super().__init__(name, bases, namespace, **kwargs)
18
+
19
+ self.__propert_setters_enabled__ = getattr(self, _propert_setters_name, enable_setters)
20
+ self.__propert_deleters_enabled__ = getattr(self, _propert_deleters_name, enable_deleters)
21
+
22
+ def __setattr__(self, name: str, value: Any, /) -> None:
23
+ if getattr(self, _propert_setters_name, False):
24
+ old_value = self.__dict__.get(name, None)
25
+ if isinstance(old_value, _base_property):
26
+ old_value._set(None, self, value)
27
+ return
28
+ super().__setattr__(name, value)
29
+
30
+ def __delattr__(self, name: str, /) -> None:
31
+ if self.__propert_deleters_enabled__:
32
+ old_value = self.__dict__.get(name, None)
33
+ if isinstance(old_value, _base_property):
34
+ old_value._del(None, self)
35
+ return
36
+ super().__delattr__(name)
37
+
38
+ _propert_meta_cache[_key] = _PropertMeta
39
+ return _PropertMeta
40
+
41
+ def _try_enable_feature(cls: Type[T], name: str, enable: bool, *, force_disable: bool = False) -> bool:
42
+ """
43
+ Try enabling a setter/deleter/... feature for a class. If force_disable is set to true, it also allows disabling the feature, otherwise it will only try to enable it.
44
+ Only works, when the class already inherits from a class that allows enabling/disabling the feature (see `_create_propert_meta(...)` for more info)
45
+ Returns whether it was successful or if the class must be patched with the metaclass first
46
+ """
47
+ # If we are not forcing the feature to be disabled and we are not trying to disable the feature, return True to indicate success
48
+ if not force_disable and not enable:
49
+ return True
50
+
51
+ # Check if the feature is already enabled
52
+ is_enabled = getattr(cls, name, None)
53
+
54
+ # If the feature is not even present, return False to indicate that the class must be patched with the metaclass first
55
+ if is_enabled is None:
56
+ return False
57
+
58
+ # Enable the feature if it is not already enabled (or disable it if it is not already disabled, only ever called when `force_disable=True`)
59
+ if is_enabled != enable:
60
+ setattr(cls, name, enable)
61
+
62
+ # Return True to indicate success
63
+ return True
64
+
65
+ _propert_cls_cache = dict[tuple[type, bool, bool], type]()
66
+ def _patch_propert_meta_into_class(cls: Type[T], *, enable_setters: bool = False, enable_deleters: bool = False, force_disable: bool = False) -> Type[T]:
67
+ """Enable the given features for the given class. Patches and returns a new class if necessary."""
68
+ must_create_meta = (
69
+ not _try_enable_feature(cls, _propert_setters_name, enable_setters, force_disable=force_disable) or
70
+ not _try_enable_feature(cls, _propert_deleters_name, enable_deleters, force_disable=force_disable)
71
+ )
72
+
73
+ if not must_create_meta:
74
+ return cls
75
+
76
+ _key = (cls, enable_setters, enable_deleters)
77
+
78
+ new_cls = _propert_cls_cache.get(_key, None)
79
+
80
+ if new_cls is not None:
81
+ return new_cls
82
+
83
+ new_meta = _create_propert_meta(type(cls), enable_setters=enable_setters, enable_deleters=enable_deleters)
84
+ new_cls = new_meta(cls.__name__, (cls,), {})
85
+
86
+ _propert_cls_cache[_key] = new_cls
87
+ return new_cls
88
+
89
+ def enable_propert_setters(cls: Type[T], /) -> Type[T]:
90
+ """Return a new class with the propert setters (@your_propert.setter) enabled."""
91
+ return _patch_propert_meta_into_class(cls, enable_setters=True)
92
+
93
+ def enable_propert_deleters(cls: Type[T], /) -> Type[T]:
94
+ """Return a new class with the propert deleters (@your_propert.deleter) enabled."""
95
+ return _patch_propert_meta_into_class(cls, enable_deleters=True)
96
+
97
+ def enable_propert_modifications(cls: Type[T], /) -> Type[T]:
98
+ """Return a new class with all propert modifications (e.g. @your_propert.setter and @your_propert.deleter) enabled."""
99
+ return _patch_propert_meta_into_class(cls, enable_setters=True, enable_deleters=True)
100
+
101
+ if TYPE_CHECKING:
102
+ PropertMeta = type
103
+ else:
104
+ PropertMeta = _create_propert_meta(enable_setters=True, enable_deleters=True)
105
+ class PropertBase(metaclass=PropertMeta): """Base class for classes that use propert to enable setter and deleter features for class- and staticproperties provided by propert."""