-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Match CPython's str subscript TypeError message #8319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||||
| // export through sliceable module, not slice. | ||||||
| use crate::{ | ||||||
| PyObject, PyResult, VirtualMachine, | ||||||
| builtins::{int::PyInt, slice::PySlice}, | ||||||
| builtins::{PyBaseExceptionRef, int::PyInt, slice::PySlice}, | ||||||
| }; | ||||||
| use core::ops::Range; | ||||||
| use malachite_bigint::BigInt; | ||||||
|
|
@@ -262,6 +262,24 @@ impl SequenceIndex { | |||||
| vm: &VirtualMachine, | ||||||
| obj: &PyObject, | ||||||
| type_name: &str, | ||||||
| ) -> PyResult<Self> { | ||||||
| Self::try_from_borrowed_object_with_err(vm, obj, || { | ||||||
| vm.new_type_error(format!( | ||||||
| "{} indices must be integers or slices or classes that override __index__ operator, not '{}'", | ||||||
| type_name, | ||||||
| obj.class() | ||||||
| )) | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| /// Like [`Self::try_from_borrowed_object`], but lets the caller supply the | ||||||
| /// `TypeError` raised for a non-index object. Used by types whose CPython | ||||||
| /// message differs from the shared sequence wording (e.g. `str`, whose | ||||||
| /// `unicode_subscript` says "string indices must be integers, not ..."). | ||||||
| pub fn try_from_borrowed_object_with_err( | ||||||
| vm: &VirtualMachine, | ||||||
| obj: &PyObject, | ||||||
| err: impl FnOnce() -> PyBaseExceptionRef, | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because we always raise type error,
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to use |
||||||
| ) -> PyResult<Self> { | ||||||
| if let Some(i) = obj.downcast_ref::<PyInt>() { | ||||||
| // TODO: number protocol | ||||||
|
|
@@ -276,11 +294,7 @@ impl SequenceIndex { | |||||
| .map_err(|_| vm.new_index_error("cannot fit 'int' into an index-sized integer")) | ||||||
| .map(Self::Int) | ||||||
| } else { | ||||||
| Err(vm.new_type_error(format!( | ||||||
| "{} indices must be integers or slices or classes that override __index__ operator, not '{}'", | ||||||
| type_name, | ||||||
| obj.class() | ||||||
| ))) | ||||||
| Err(err()) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Use
.name()to format the class name correctly.Passing the type object directly to
format!may fail to compile or produce an incorrect string representation (like<class 'str'>instead ofstr, defeating the PR's objective of strictly matching CPython's exact error wording). Consistent with other error formatting in the codebase, call.name()on the class to extract just the type name string.crates/vm/src/sliceable.rs#L266-L272: append.name()toobj.class().crates/vm/src/builtins/str.rs#L663-L668: append.name()toneedle.class().📍 Affects 2 files
crates/vm/src/sliceable.rs#L266-L272(this comment)crates/vm/src/builtins/str.rs#L663-L668🤖 Prompt for AI Agents