From 77f5fc107e3d7de8c1e84b260bf54aeab668ea8d Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 19 Nov 2022 07:46:35 +0100 Subject: [PATCH] guide: better document negative index behavior w.r.t. PySequence Fixes #2601 --- guide/src/class/protocols.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/guide/src/class/protocols.md b/guide/src/class/protocols.md index ea3b980d..7fbd0aac 100644 --- a/guide/src/class/protocols.md +++ b/guide/src/class/protocols.md @@ -243,18 +243,25 @@ Use the `#[pyclass(sequence)]` annotation to instruct PyO3 to fill the `sq_lengt Implements retrieval of the `self[a]` element. - *Note:* Negative integer indexes are not handled specially. + *Note:* Negative integer indexes are not handled specially by PyO3. + However, for classes with `#[pyclass(sequence)]`, when a negative index is + accessed via `PySequence::get_item`, the underlying C API already adjusts + the index to be positive. - `__setitem__(, object, object) -> ()` Implements assignment to the `self[a]` element. Should only be implemented if elements can be replaced. + Same behavior regarding negative indices as for `__getitem__`. + - `__delitem__(, object) -> ()` Implements deletion of the `self[a]` element. Should only be implemented if elements can be deleted. + Same behavior regarding negative indices as for `__getitem__`. + * `fn __concat__(&self, other: impl FromPyObject) -> PyResult` Concatenates two sequences.