propert 0.1.0__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.0/LICENSE +7 -0
- propert-0.1.0/PKG-INFO +347 -0
- propert-0.1.0/README.md +334 -0
- propert-0.1.0/pyproject.toml +44 -0
- propert-0.1.0/setup.cfg +4 -0
- propert-0.1.0/src/propert/__init__.py +5 -0
- propert-0.1.0/src/propert/autoproperty.py +397 -0
- propert-0.1.0/src/propert/base.py +164 -0
- propert-0.1.0/src/propert/classproperty.py +397 -0
- propert-0.1.0/src/propert/common.py +9 -0
- propert-0.1.0/src/propert/meta.py +105 -0
- propert-0.1.0/src/propert/shorthands.py +1113 -0
- propert-0.1.0/src/propert/staticproperty.py +204 -0
- propert-0.1.0/src/propert/types.py +98 -0
- propert-0.1.0/src/propert.egg-info/PKG-INFO +347 -0
- propert-0.1.0/src/propert.egg-info/SOURCES.txt +22 -0
- propert-0.1.0/src/propert.egg-info/dependency_links.txt +1 -0
- propert-0.1.0/src/propert.egg-info/requires.txt +1 -0
- propert-0.1.0/src/propert.egg-info/top_level.txt +1 -0
- propert-0.1.0/tests/test_classproperties.py +61 -0
- propert-0.1.0/tests/test_metas.py +22 -0
- propert-0.1.0/tests/test_mypy.py +46 -0
- propert-0.1.0/tests/test_shorthands.py +45 -0
- propert-0.1.0/tests/test_staticproperties.py +9 -0
propert-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2025 Martin Kunze
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the βSoftwareβ), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED βAS ISβ, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
propert-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: propert
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: @propert - Static and class-level properties in python
|
|
5
|
+
Project-URL: Homepage, https://github.com/martinkunze/propert
|
|
6
|
+
Project-URL: Documentation, https://github.com/martinkunze/propert/blob/main/README.md
|
|
7
|
+
Project-URL: Source, https://github.com/martinkunze/propert
|
|
8
|
+
Requires-Python: >=3.8
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: typing-extensions
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# π΅ `@propert` - Static and class-level properties in python
|
|
15
|
+
|
|
16
|
+
## βοΈ Features
|
|
17
|
+
|
|
18
|
+
You may know the `@property` decorator in python, which sadly does not work with static and class methods.
|
|
19
|
+
|
|
20
|
+
Introducing `@propert` - a small library to add static and class-level properties to your code:
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from propert import classproperty, staticproperty
|
|
24
|
+
|
|
25
|
+
class Test:
|
|
26
|
+
@classproperty
|
|
27
|
+
def class_prop(cls):
|
|
28
|
+
return ...
|
|
29
|
+
|
|
30
|
+
@staticproperty
|
|
31
|
+
def static_prop():
|
|
32
|
+
return ...
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### π Overview
|
|
36
|
+
|
|
37
|
+
- π οΈ properties for [class and static methods](#-static--class--and-autoproperties)
|
|
38
|
+
- π properties getting called on both instance and class level (see [`@autoproperty`](#-static--class--and-autoproperties))
|
|
39
|
+
- π custom [setters and deleters](#%EF%B8%8F-setters-and-deleters)
|
|
40
|
+
- π¦ [cached](#-cached-properties) and [introspected](#-introspection) properties
|
|
41
|
+
- π€ versatile and powerful [shorthands](#-shorthands)
|
|
42
|
+
- π€ΈββοΈ highly flexible [syntax](#-your-code-your-style)
|
|
43
|
+
- βοΈ decent type-checking support (see [below](#-type-checking-and-mypy))
|
|
44
|
+
|
|
45
|
+
### π·οΈ Setters and Deleters
|
|
46
|
+
|
|
47
|
+
All properties provided by this library support setters and deleters:
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
class Test(PropertBase):
|
|
51
|
+
@classproperty
|
|
52
|
+
def class_prop(cls):
|
|
53
|
+
return ...
|
|
54
|
+
|
|
55
|
+
@class_prop.setter
|
|
56
|
+
def class_prop(cls, value: ...):
|
|
57
|
+
...
|
|
58
|
+
|
|
59
|
+
@class_prop.deleter
|
|
60
|
+
def class_prop(cls):
|
|
61
|
+
...
|
|
62
|
+
|
|
63
|
+
x = Test.class_prop # calls getter
|
|
64
|
+
Test.class_prop = x # calls setter
|
|
65
|
+
del Test.class_prop # calls deleter
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### Metaclass Patching
|
|
69
|
+
|
|
70
|
+
Note, that you will have to apply the `PropertMeta` metaclass to your class to enable custom setters and deleters. There are multiple ways to achieve this:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
class Test(PropertBase):
|
|
74
|
+
...
|
|
75
|
+
|
|
76
|
+
class Test(metaclass=PropertMeta):
|
|
77
|
+
...
|
|
78
|
+
|
|
79
|
+
class Test(propert(OtherBase)):
|
|
80
|
+
...
|
|
81
|
+
|
|
82
|
+
class Test(enable_propert_modifications(OtherBase)):
|
|
83
|
+
...
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
You can also only enable setters (or just deleters) for a class:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
class Test(propert.enable_setters(OtherBase)):
|
|
90
|
+
...
|
|
91
|
+
|
|
92
|
+
class Test(enable_propert_setters(OtherBase)):
|
|
93
|
+
...
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### π¦ Cached Properties
|
|
97
|
+
|
|
98
|
+
There are cached properties equivalent to the `@cached_property` in the `functools` library:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from propert import cached_classproperty, cached_staticproperty
|
|
102
|
+
|
|
103
|
+
class Test:
|
|
104
|
+
@cached_classproperty
|
|
105
|
+
def cached_class_prop(cls):
|
|
106
|
+
return ...
|
|
107
|
+
|
|
108
|
+
@cached_staticproperty
|
|
109
|
+
def cached_static_prop():
|
|
110
|
+
return ...
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
A custom setter on a cached property can return a value that will be saved in the cache. Alternatively, it can return `propert.CACHE_RESET` to reset the cache or `propert.NO_VALUE` to not change the cache.
|
|
114
|
+
|
|
115
|
+
Calling `del Test.cached_class_prop` will reset the cached value by default causing the property to be re-evaluated on the next access. A custom deleter for a cached property should return `True` to reset the cache or `False` to leave the cache unchanged. This allows conditional cache invalidation.
|
|
116
|
+
|
|
117
|
+
> [!WARNING]
|
|
118
|
+
> In order for cached properties to reset when calling `del Test.cached_prop`, the `Test` class must be patched with the `PropertMeta` metaclass. See [Metaclass Patching](#metaclass-patching) for more info.
|
|
119
|
+
|
|
120
|
+
### π Introspection
|
|
121
|
+
|
|
122
|
+
There is a special type of properties that can be used to introspect the property when a getter, setter or deleter is called:
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from propert import introspected_classproperty, introspected_staticproperty
|
|
126
|
+
|
|
127
|
+
class Test:
|
|
128
|
+
@introspected_classproperty
|
|
129
|
+
def class_prop(cls, prop):
|
|
130
|
+
prop._some_internal_var = 42
|
|
131
|
+
return ...
|
|
132
|
+
|
|
133
|
+
@introspected_staticproperty
|
|
134
|
+
def static_prop(prop):
|
|
135
|
+
prop._some_internal_var = 42
|
|
136
|
+
return ...
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
This behaviour can be useful if you want to declare custom property subclasses meant for decorating methods requiring access to the property object itself.
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
### π€ Shorthands
|
|
143
|
+
|
|
144
|
+
You can also use shorthands to avoid importing and typing the full name of each decorator:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from propert import propert
|
|
148
|
+
|
|
149
|
+
class Test:
|
|
150
|
+
@propert
|
|
151
|
+
def class_prop(cls):
|
|
152
|
+
return ...
|
|
153
|
+
|
|
154
|
+
@propert
|
|
155
|
+
def static_prop():
|
|
156
|
+
return ...
|
|
157
|
+
|
|
158
|
+
@propert.cached
|
|
159
|
+
def cached_class_prop(cls):
|
|
160
|
+
return ...
|
|
161
|
+
|
|
162
|
+
@propert.cached
|
|
163
|
+
def cached_static_prop():
|
|
164
|
+
return ...
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
This will automatically determine if the property is a class or static property based on the number of arguments.
|
|
168
|
+
|
|
169
|
+
### π Static-, Class- and Autoproperties
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
| | `@staticproperty` | `@classproperty` | `@autoproperty` |
|
|
173
|
+
|------------------------|-----------------------|----------------------|---------------------|
|
|
174
|
+
| Syntax | `def prop(): ...` | `def prop(cls): ...` | `def prop(cls_or_self): ...` |
|
|
175
|
+
| `x = Test.prop` | | `cls = Test` | `cls_or_self = Test` |
|
|
176
|
+
| `x = Test().prop` | | `cls = Test` | `cls_or_self = Test()` |
|
|
177
|
+
| | | | |
|
|
178
|
+
| Supports Cache | β
| β
| β
(instance and class share cache) |
|
|
179
|
+
| Supports Introspection | β
| β
| β
|
|
|
180
|
+
| Supported by `@propert` shorthand | β
| β
| β |
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
### π¨ Your code, your style
|
|
184
|
+
|
|
185
|
+
Every syntax you can think of is supported with `propert` to suit your needs.
|
|
186
|
+
For example, all of these are valid ways to create a cached class property:
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
class Test:
|
|
190
|
+
@propert(cached=True)
|
|
191
|
+
def prop(cls):
|
|
192
|
+
return ...
|
|
193
|
+
|
|
194
|
+
@propert.cached
|
|
195
|
+
def prop(cls):
|
|
196
|
+
return ...
|
|
197
|
+
|
|
198
|
+
@classproperty(cached=True)
|
|
199
|
+
def prop(cls):
|
|
200
|
+
return ...
|
|
201
|
+
|
|
202
|
+
@cached_classproperty
|
|
203
|
+
def prop():
|
|
204
|
+
return ...
|
|
205
|
+
|
|
206
|
+
prop = propert(lambda cls: ..., cached=True)
|
|
207
|
+
prop = propert.cached(lambda cls: ...)
|
|
208
|
+
prop = classproperty(lambda cls: ..., cached=True)
|
|
209
|
+
prop = cached_classproperty(lambda cls: ...)
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Or let's say your entire project uses the `@propert.introspected` shorthand and now you need an introspected class property that is also cached:
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
class Test:
|
|
216
|
+
@propert.introspected
|
|
217
|
+
def prop_1(cls, prop):
|
|
218
|
+
return ...
|
|
219
|
+
|
|
220
|
+
@propert.introspected
|
|
221
|
+
def prop_2(cls, prop):
|
|
222
|
+
return ...
|
|
223
|
+
|
|
224
|
+
@propert.introspected(cached=True)
|
|
225
|
+
def prop_cached(cls, prop):
|
|
226
|
+
return ...
|
|
227
|
+
|
|
228
|
+
@propert.introspected
|
|
229
|
+
def prop_3(cls, prop):
|
|
230
|
+
return ...
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Blends right in, doesn't it?
|
|
234
|
+
|
|
235
|
+
### π Type Checking and MyPy
|
|
236
|
+
|
|
237
|
+
#### Classmethod/Staticmethod
|
|
238
|
+
|
|
239
|
+
If you are planning to use this library with mypy, you may need to add `@classmethod` or `@staticmethod` between the decorator and the property method, like this:
|
|
240
|
+
|
|
241
|
+
```python
|
|
242
|
+
class Test:
|
|
243
|
+
@classproperty
|
|
244
|
+
@classmethod
|
|
245
|
+
def class_property(cls):
|
|
246
|
+
return ...
|
|
247
|
+
|
|
248
|
+
@staticproperty
|
|
249
|
+
@staticmethod
|
|
250
|
+
def static_property():
|
|
251
|
+
return ...
|
|
252
|
+
|
|
253
|
+
@classproperty
|
|
254
|
+
def class_property_instance(cls):
|
|
255
|
+
# will work, but mypy will assume that cls: Self@Test instead of cls: Type[Self@Test]
|
|
256
|
+
return ...
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Using a classmethod will also solve the issue, that type checkers show the wrong type annotation in the method of a classproperty: `cls: Self@Test` instead of `cls: Type[Self@Test]`, even though at runtime, the type will be `Type[Test]`.
|
|
260
|
+
|
|
261
|
+
#### Redefinition
|
|
262
|
+
|
|
263
|
+
Additionally, when using custom setters and deleters, you may run into a redefinition error. You can solve that error in three ways:
|
|
264
|
+
|
|
265
|
+
```python
|
|
266
|
+
class Test:
|
|
267
|
+
@classproperty
|
|
268
|
+
def class_prop(cls):
|
|
269
|
+
return ...
|
|
270
|
+
|
|
271
|
+
# 1. Use and underscore as the function name for setters and deleters
|
|
272
|
+
@class_prop.setter
|
|
273
|
+
def _(cls, value: ...):
|
|
274
|
+
...
|
|
275
|
+
|
|
276
|
+
@class_prop.deleter
|
|
277
|
+
def _(cls):
|
|
278
|
+
...
|
|
279
|
+
|
|
280
|
+
# 2. Suppress the redefinition error
|
|
281
|
+
@class_prop.setter # type: ignore[no-redef]
|
|
282
|
+
def class_prop(cls, value: ...):
|
|
283
|
+
...
|
|
284
|
+
|
|
285
|
+
# 3. Use a different function name for setters and deleters (not recommended)
|
|
286
|
+
@class_prop.setter
|
|
287
|
+
def class_prop_set(cls, value: int) -> None:
|
|
288
|
+
pass
|
|
289
|
+
|
|
290
|
+
@class_prop.deleter
|
|
291
|
+
def class_prop_del(cls) -> None:
|
|
292
|
+
pass
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
#### Invalid Assignment
|
|
296
|
+
|
|
297
|
+
Finally, when using setters, mypy will not allow you to assign to the property, because it thinks that it is a method (and/or a property object of some sort). You can suppress this warning by using `# type: ignore[assignment, method-assign]`.
|
|
298
|
+
|
|
299
|
+
#### Runtime
|
|
300
|
+
|
|
301
|
+
The library itself will still work without the `@classmethod` or `@staticmethod` decorators, but mypy and type-checking may show warnings. The same applies for the "redefinitions" and "method assignments", which are properly resolved at runtime but may cause issues with type-checking.
|
|
302
|
+
|
|
303
|
+
#### Plugin
|
|
304
|
+
|
|
305
|
+
In the future, a mypy plugin might be added, but this will require at least the following PR to be merged: https://github.com/python/mypy/pull/9925
|
|
306
|
+
|
|
307
|
+
## π» Development
|
|
308
|
+
|
|
309
|
+
### π Code structure
|
|
310
|
+
|
|
311
|
+
The code is structured as follows:
|
|
312
|
+
|
|
313
|
+
- `src/propert/` contains the source code
|
|
314
|
+
- `tests/` contains the tests
|
|
315
|
+
- `tests/test_mypy.py` contains the mypy tests
|
|
316
|
+
|
|
317
|
+
Most of the actual logic is in the `src/propert/base.py` and `src/propert/meta.py` files.
|
|
318
|
+
The `src/propert/common.py` file contains some values that are used by both files.
|
|
319
|
+
All remaining files primarily contain type hints and some decision logic for the shorthands they represent.
|
|
320
|
+
|
|
321
|
+
### π¦ Distribution
|
|
322
|
+
|
|
323
|
+
To build the package, you can do the following:
|
|
324
|
+
|
|
325
|
+
```bash
|
|
326
|
+
uv run build
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
<details>
|
|
330
|
+
<summary>Publishing</summary>
|
|
331
|
+
|
|
332
|
+
> π‘ This section is primarily relevant for the maintainers of this package (me), as it requires permission to push a package to the `propert` repository on PyPI.
|
|
333
|
+
|
|
334
|
+
```bash
|
|
335
|
+
uv run publish --token <token>
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
</details>
|
|
339
|
+
|
|
340
|
+
### π― Tests
|
|
341
|
+
|
|
342
|
+
To run all tests, you can do the following:
|
|
343
|
+
|
|
344
|
+
```bash
|
|
345
|
+
uv run pytest
|
|
346
|
+
uv run mypy src tests/test_mypy.py
|
|
347
|
+
```
|
propert-0.1.0/README.md
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
# π΅ `@propert` - Static and class-level properties in python
|
|
2
|
+
|
|
3
|
+
## βοΈ Features
|
|
4
|
+
|
|
5
|
+
You may know the `@property` decorator in python, which sadly does not work with static and class methods.
|
|
6
|
+
|
|
7
|
+
Introducing `@propert` - a small library to add static and class-level properties to your code:
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
from propert import classproperty, staticproperty
|
|
11
|
+
|
|
12
|
+
class Test:
|
|
13
|
+
@classproperty
|
|
14
|
+
def class_prop(cls):
|
|
15
|
+
return ...
|
|
16
|
+
|
|
17
|
+
@staticproperty
|
|
18
|
+
def static_prop():
|
|
19
|
+
return ...
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### π Overview
|
|
23
|
+
|
|
24
|
+
- π οΈ properties for [class and static methods](#-static--class--and-autoproperties)
|
|
25
|
+
- π properties getting called on both instance and class level (see [`@autoproperty`](#-static--class--and-autoproperties))
|
|
26
|
+
- π custom [setters and deleters](#%EF%B8%8F-setters-and-deleters)
|
|
27
|
+
- π¦ [cached](#-cached-properties) and [introspected](#-introspection) properties
|
|
28
|
+
- π€ versatile and powerful [shorthands](#-shorthands)
|
|
29
|
+
- π€ΈββοΈ highly flexible [syntax](#-your-code-your-style)
|
|
30
|
+
- βοΈ decent type-checking support (see [below](#-type-checking-and-mypy))
|
|
31
|
+
|
|
32
|
+
### π·οΈ Setters and Deleters
|
|
33
|
+
|
|
34
|
+
All properties provided by this library support setters and deleters:
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
class Test(PropertBase):
|
|
38
|
+
@classproperty
|
|
39
|
+
def class_prop(cls):
|
|
40
|
+
return ...
|
|
41
|
+
|
|
42
|
+
@class_prop.setter
|
|
43
|
+
def class_prop(cls, value: ...):
|
|
44
|
+
...
|
|
45
|
+
|
|
46
|
+
@class_prop.deleter
|
|
47
|
+
def class_prop(cls):
|
|
48
|
+
...
|
|
49
|
+
|
|
50
|
+
x = Test.class_prop # calls getter
|
|
51
|
+
Test.class_prop = x # calls setter
|
|
52
|
+
del Test.class_prop # calls deleter
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### Metaclass Patching
|
|
56
|
+
|
|
57
|
+
Note, that you will have to apply the `PropertMeta` metaclass to your class to enable custom setters and deleters. There are multiple ways to achieve this:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
class Test(PropertBase):
|
|
61
|
+
...
|
|
62
|
+
|
|
63
|
+
class Test(metaclass=PropertMeta):
|
|
64
|
+
...
|
|
65
|
+
|
|
66
|
+
class Test(propert(OtherBase)):
|
|
67
|
+
...
|
|
68
|
+
|
|
69
|
+
class Test(enable_propert_modifications(OtherBase)):
|
|
70
|
+
...
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
You can also only enable setters (or just deleters) for a class:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
class Test(propert.enable_setters(OtherBase)):
|
|
77
|
+
...
|
|
78
|
+
|
|
79
|
+
class Test(enable_propert_setters(OtherBase)):
|
|
80
|
+
...
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### π¦ Cached Properties
|
|
84
|
+
|
|
85
|
+
There are cached properties equivalent to the `@cached_property` in the `functools` library:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from propert import cached_classproperty, cached_staticproperty
|
|
89
|
+
|
|
90
|
+
class Test:
|
|
91
|
+
@cached_classproperty
|
|
92
|
+
def cached_class_prop(cls):
|
|
93
|
+
return ...
|
|
94
|
+
|
|
95
|
+
@cached_staticproperty
|
|
96
|
+
def cached_static_prop():
|
|
97
|
+
return ...
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
A custom setter on a cached property can return a value that will be saved in the cache. Alternatively, it can return `propert.CACHE_RESET` to reset the cache or `propert.NO_VALUE` to not change the cache.
|
|
101
|
+
|
|
102
|
+
Calling `del Test.cached_class_prop` will reset the cached value by default causing the property to be re-evaluated on the next access. A custom deleter for a cached property should return `True` to reset the cache or `False` to leave the cache unchanged. This allows conditional cache invalidation.
|
|
103
|
+
|
|
104
|
+
> [!WARNING]
|
|
105
|
+
> In order for cached properties to reset when calling `del Test.cached_prop`, the `Test` class must be patched with the `PropertMeta` metaclass. See [Metaclass Patching](#metaclass-patching) for more info.
|
|
106
|
+
|
|
107
|
+
### π Introspection
|
|
108
|
+
|
|
109
|
+
There is a special type of properties that can be used to introspect the property when a getter, setter or deleter is called:
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from propert import introspected_classproperty, introspected_staticproperty
|
|
113
|
+
|
|
114
|
+
class Test:
|
|
115
|
+
@introspected_classproperty
|
|
116
|
+
def class_prop(cls, prop):
|
|
117
|
+
prop._some_internal_var = 42
|
|
118
|
+
return ...
|
|
119
|
+
|
|
120
|
+
@introspected_staticproperty
|
|
121
|
+
def static_prop(prop):
|
|
122
|
+
prop._some_internal_var = 42
|
|
123
|
+
return ...
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
This behaviour can be useful if you want to declare custom property subclasses meant for decorating methods requiring access to the property object itself.
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
### π€ Shorthands
|
|
130
|
+
|
|
131
|
+
You can also use shorthands to avoid importing and typing the full name of each decorator:
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
from propert import propert
|
|
135
|
+
|
|
136
|
+
class Test:
|
|
137
|
+
@propert
|
|
138
|
+
def class_prop(cls):
|
|
139
|
+
return ...
|
|
140
|
+
|
|
141
|
+
@propert
|
|
142
|
+
def static_prop():
|
|
143
|
+
return ...
|
|
144
|
+
|
|
145
|
+
@propert.cached
|
|
146
|
+
def cached_class_prop(cls):
|
|
147
|
+
return ...
|
|
148
|
+
|
|
149
|
+
@propert.cached
|
|
150
|
+
def cached_static_prop():
|
|
151
|
+
return ...
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
This will automatically determine if the property is a class or static property based on the number of arguments.
|
|
155
|
+
|
|
156
|
+
### π Static-, Class- and Autoproperties
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
| | `@staticproperty` | `@classproperty` | `@autoproperty` |
|
|
160
|
+
|------------------------|-----------------------|----------------------|---------------------|
|
|
161
|
+
| Syntax | `def prop(): ...` | `def prop(cls): ...` | `def prop(cls_or_self): ...` |
|
|
162
|
+
| `x = Test.prop` | | `cls = Test` | `cls_or_self = Test` |
|
|
163
|
+
| `x = Test().prop` | | `cls = Test` | `cls_or_self = Test()` |
|
|
164
|
+
| | | | |
|
|
165
|
+
| Supports Cache | β
| β
| β
(instance and class share cache) |
|
|
166
|
+
| Supports Introspection | β
| β
| β
|
|
|
167
|
+
| Supported by `@propert` shorthand | β
| β
| β |
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
### π¨ Your code, your style
|
|
171
|
+
|
|
172
|
+
Every syntax you can think of is supported with `propert` to suit your needs.
|
|
173
|
+
For example, all of these are valid ways to create a cached class property:
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
class Test:
|
|
177
|
+
@propert(cached=True)
|
|
178
|
+
def prop(cls):
|
|
179
|
+
return ...
|
|
180
|
+
|
|
181
|
+
@propert.cached
|
|
182
|
+
def prop(cls):
|
|
183
|
+
return ...
|
|
184
|
+
|
|
185
|
+
@classproperty(cached=True)
|
|
186
|
+
def prop(cls):
|
|
187
|
+
return ...
|
|
188
|
+
|
|
189
|
+
@cached_classproperty
|
|
190
|
+
def prop():
|
|
191
|
+
return ...
|
|
192
|
+
|
|
193
|
+
prop = propert(lambda cls: ..., cached=True)
|
|
194
|
+
prop = propert.cached(lambda cls: ...)
|
|
195
|
+
prop = classproperty(lambda cls: ..., cached=True)
|
|
196
|
+
prop = cached_classproperty(lambda cls: ...)
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Or let's say your entire project uses the `@propert.introspected` shorthand and now you need an introspected class property that is also cached:
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
class Test:
|
|
203
|
+
@propert.introspected
|
|
204
|
+
def prop_1(cls, prop):
|
|
205
|
+
return ...
|
|
206
|
+
|
|
207
|
+
@propert.introspected
|
|
208
|
+
def prop_2(cls, prop):
|
|
209
|
+
return ...
|
|
210
|
+
|
|
211
|
+
@propert.introspected(cached=True)
|
|
212
|
+
def prop_cached(cls, prop):
|
|
213
|
+
return ...
|
|
214
|
+
|
|
215
|
+
@propert.introspected
|
|
216
|
+
def prop_3(cls, prop):
|
|
217
|
+
return ...
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Blends right in, doesn't it?
|
|
221
|
+
|
|
222
|
+
### π Type Checking and MyPy
|
|
223
|
+
|
|
224
|
+
#### Classmethod/Staticmethod
|
|
225
|
+
|
|
226
|
+
If you are planning to use this library with mypy, you may need to add `@classmethod` or `@staticmethod` between the decorator and the property method, like this:
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
class Test:
|
|
230
|
+
@classproperty
|
|
231
|
+
@classmethod
|
|
232
|
+
def class_property(cls):
|
|
233
|
+
return ...
|
|
234
|
+
|
|
235
|
+
@staticproperty
|
|
236
|
+
@staticmethod
|
|
237
|
+
def static_property():
|
|
238
|
+
return ...
|
|
239
|
+
|
|
240
|
+
@classproperty
|
|
241
|
+
def class_property_instance(cls):
|
|
242
|
+
# will work, but mypy will assume that cls: Self@Test instead of cls: Type[Self@Test]
|
|
243
|
+
return ...
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Using a classmethod will also solve the issue, that type checkers show the wrong type annotation in the method of a classproperty: `cls: Self@Test` instead of `cls: Type[Self@Test]`, even though at runtime, the type will be `Type[Test]`.
|
|
247
|
+
|
|
248
|
+
#### Redefinition
|
|
249
|
+
|
|
250
|
+
Additionally, when using custom setters and deleters, you may run into a redefinition error. You can solve that error in three ways:
|
|
251
|
+
|
|
252
|
+
```python
|
|
253
|
+
class Test:
|
|
254
|
+
@classproperty
|
|
255
|
+
def class_prop(cls):
|
|
256
|
+
return ...
|
|
257
|
+
|
|
258
|
+
# 1. Use and underscore as the function name for setters and deleters
|
|
259
|
+
@class_prop.setter
|
|
260
|
+
def _(cls, value: ...):
|
|
261
|
+
...
|
|
262
|
+
|
|
263
|
+
@class_prop.deleter
|
|
264
|
+
def _(cls):
|
|
265
|
+
...
|
|
266
|
+
|
|
267
|
+
# 2. Suppress the redefinition error
|
|
268
|
+
@class_prop.setter # type: ignore[no-redef]
|
|
269
|
+
def class_prop(cls, value: ...):
|
|
270
|
+
...
|
|
271
|
+
|
|
272
|
+
# 3. Use a different function name for setters and deleters (not recommended)
|
|
273
|
+
@class_prop.setter
|
|
274
|
+
def class_prop_set(cls, value: int) -> None:
|
|
275
|
+
pass
|
|
276
|
+
|
|
277
|
+
@class_prop.deleter
|
|
278
|
+
def class_prop_del(cls) -> None:
|
|
279
|
+
pass
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
#### Invalid Assignment
|
|
283
|
+
|
|
284
|
+
Finally, when using setters, mypy will not allow you to assign to the property, because it thinks that it is a method (and/or a property object of some sort). You can suppress this warning by using `# type: ignore[assignment, method-assign]`.
|
|
285
|
+
|
|
286
|
+
#### Runtime
|
|
287
|
+
|
|
288
|
+
The library itself will still work without the `@classmethod` or `@staticmethod` decorators, but mypy and type-checking may show warnings. The same applies for the "redefinitions" and "method assignments", which are properly resolved at runtime but may cause issues with type-checking.
|
|
289
|
+
|
|
290
|
+
#### Plugin
|
|
291
|
+
|
|
292
|
+
In the future, a mypy plugin might be added, but this will require at least the following PR to be merged: https://github.com/python/mypy/pull/9925
|
|
293
|
+
|
|
294
|
+
## π» Development
|
|
295
|
+
|
|
296
|
+
### π Code structure
|
|
297
|
+
|
|
298
|
+
The code is structured as follows:
|
|
299
|
+
|
|
300
|
+
- `src/propert/` contains the source code
|
|
301
|
+
- `tests/` contains the tests
|
|
302
|
+
- `tests/test_mypy.py` contains the mypy tests
|
|
303
|
+
|
|
304
|
+
Most of the actual logic is in the `src/propert/base.py` and `src/propert/meta.py` files.
|
|
305
|
+
The `src/propert/common.py` file contains some values that are used by both files.
|
|
306
|
+
All remaining files primarily contain type hints and some decision logic for the shorthands they represent.
|
|
307
|
+
|
|
308
|
+
### π¦ Distribution
|
|
309
|
+
|
|
310
|
+
To build the package, you can do the following:
|
|
311
|
+
|
|
312
|
+
```bash
|
|
313
|
+
uv run build
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
<details>
|
|
317
|
+
<summary>Publishing</summary>
|
|
318
|
+
|
|
319
|
+
> π‘ This section is primarily relevant for the maintainers of this package (me), as it requires permission to push a package to the `propert` repository on PyPI.
|
|
320
|
+
|
|
321
|
+
```bash
|
|
322
|
+
uv run publish --token <token>
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
</details>
|
|
326
|
+
|
|
327
|
+
### π― Tests
|
|
328
|
+
|
|
329
|
+
To run all tests, you can do the following:
|
|
330
|
+
|
|
331
|
+
```bash
|
|
332
|
+
uv run pytest
|
|
333
|
+
uv run mypy src tests/test_mypy.py
|
|
334
|
+
```
|