pymethods: seq methods from mapping methods #2065
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Cut out from #2060
This is the part of that PR which changes
__getitem__
,__setitem__
, and__delitem__
to implement the sequence slots as well as the mapping slots.I think it's reasonably clear that this design makes sense, because it matches pure-Python behavior. It defers how to solve "pure-sequence" and "pure-mapping" classes to #2060 - which might either be the
__seqlen__
(etc.) methods or attributes like#[pyo3(sequence)]
and#[pyo3(mapping)]
.Note that I ended up making the decision to not implement
sq_length
from__len__
, because this way Python won't automatically try to convert negative indices to positive ones by adding the sequence length. This behaviour is inconsistent, because it only applies to calls to__getitem__
etc from the sequence slot (not the mapping slot). We can't reverse it on the PyO3 side either, because if we get a positive index to the sequence slot we don't know whether the original index was actually a negative one but had the length added. From searching the CPython source I don't see any downsides to not implementingsq_length
except thatPySequence::len()
will fail for pyclasses.I'm tempted to submit a PR to upstream CPython to see if there can be a different way to opt out of this length-correcting behaviour so that we can implement
sq_length
too.