Match CPython's str subscript TypeError message#1
Closed
kim-jaedeok wants to merge 1 commit into
Closed
Conversation
`str.__getitem__` with a non-index key raised the shared sequence
wording ("str indices must be integers or slices or classes that
override __index__ operator, not 'x'") instead of CPython's
str-specific "string indices must be integers, not 'x'" (bpo-44110 /
gh-88276, Python 3.11).
Add `SequenceIndex::try_from_borrowed_object_with_err`, which takes a
caller-supplied error closure, and have the existing
`try_from_borrowed_object` delegate to it with the unchanged shared
message. `str._getitem` now supplies the CPython-accurate message,
which flows through both `__getitem__` and the mapping subscript slot.
Other sequence types keep the shared wording.
Removes the expectedFailure marker on
`string_tests.StringLikeTest.test_subscript`, fixing it for both
test_str and test_userstring.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Aligns RustPython’s str subscripting TypeError wording with CPython by allowing str to supply a custom non-index error message while keeping the shared sequence index parsing logic unchanged for all other sequence types.
Changes:
- Added
SequenceIndex::try_from_borrowed_object_with_errto allow callers to customize theTypeErroremitted for non-index objects. - Updated
strsubscripting (PyStr::_getitem) to emit CPython’s"string indices must be integers, not '...'"message. - Removed the
@unittest.expectedFailuremarker fromLib/test/string_tests.py::test_subscriptnow that the test passes.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| Lib/test/string_tests.py | Removes expectedFailure for test_subscript now that the error message matches CPython. |
| crates/vm/src/sliceable.rs | Introduces an alternate SequenceIndex constructor that accepts a caller-provided TypeError for non-index objects. |
| crates/vm/src/builtins/str.rs | Uses the new constructor to produce CPython-accurate str subscript TypeError wording. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Owner
Author
|
Superseded by the upstream PR: RustPython#8319 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Make
strsubscripting with a non-integer index raise the sameTypeErrormessage as CPython, and drop the
expectedFailuremarker onstring_tests.test_subscript.Before / after (
'abc'['def']):string indices must be integers, not 'str'str indices must be integers or slices or classes that override __index__ operator, not 'str'string indices must be integers, not 'str'Only the error message differed — indexing itself (int / negative / slice)
already behaved correctly. CPython gives
strits own wording inunicode_subscript(see python/cpython#88276, fixed in 3.11), distinct from theshared sequence message used by
list/tuple/bytes.Approach
SequenceIndex::try_from_borrowed_object_with_err, which keeps the sharedindex-parsing logic but lets the caller supply the
TypeErrorraised for anon-index object.
try_from_borrowed_objectnow delegates to it with the existing wording, soevery other sequence caller (
list/tuple/bytes/bytearray/array/mmap/struct-sequence) is byte-for-byte unchanged.
str::_getitem— the single path shared by__getitem__and the mappingsubscript slot — supplies the CPython-accurate message.
Scoped intentionally to
stronly: CPython declined to unify the sequencemessages (python/cpython#75731, rejected), so the other types' wording is left
as-is.
Testing
test_str/test_userstringtest_subscriptnow pass (marker removed).str/object/None/floatindices, and for
UserStringas receiver.list/tuple/bytesmessages verified unchanged;rustpython-vmandrustpython-codegenunit tests pass.🤖 Generated with Claude Code