Skip to content

Commit 9b9a734

Browse files
committed
Properties doc: about deletion
Took me a short while to understand the "is being deleted" to be nothing more than a `del` call, so I guess it's worth attempting to be more explicit.
1 parent 1c80d4a commit 9b9a734

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/py_class/py_class.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,13 @@ get its value and, optionally, to set or delete it.
176176
177177
### Setter details
178178
179-
* The setter is optional. If omitted, the attribute will be read-only.
179+
* The setter is optional. If omitted, the attribute will be read-only
180+
and any setting or deleting attempt will raise `AttributeError`.
180181
* Unlike Python, the setter method name must be different from the property name.
181182
The setter method name is used to call the setter from Rust.
182-
* If value is `None` then the property is being deleted.
183+
* A `None` value represents that the property is being deleted, for instance
184+
with Python `del`.
185+
As with Python properties, what should happen is entirely up to the implementation.
183186
* The value type can be any type that implements `FromPyObject`, or a reference or
184187
optional reference to any type that implements `RefFromPyObject`. In the latter
185188
case, the type of the value is `Option<Option<&impl RefFromPyObject>>`, where

tests/test_class.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,10 @@ fn properties() {
12611261
py_run!(py, c, "assert c.match");
12621262
assert!(c.r#match(py).unwrap());
12631263

1264+
// Instead of really deleting, our setter sets back to 0
1265+
py_run!(py, c, "delattr(c, 'prop')");
1266+
py_run!(py, c, "assert c.prop == 0");
1267+
12641268
py_run!(py, c, "c.prop_by_ref = 'testing'");
12651269
py_run!(py, c, "assert c.prop_by_ref == 'testing'");
12661270

0 commit comments

Comments
 (0)