Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Infer struct sequence module name from __module__ attribute in repr
slot_repr now checks the __module__ attribute set by #[pymodule]
at runtime, so explicit module = "..." in #[pystruct_sequence] is
no longer needed for types defined inside a #[pymodule].
  • Loading branch information
youknowone committed Feb 21, 2026
commit 5cc589980cfb1767c2200bcabc2e0f4febaac8a6
18 changes: 16 additions & 2 deletions crates/vm/src/types/structseq.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::common::lock::LazyLock;
use crate::{
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, atomic_func,
builtins::{PyBaseExceptionRef, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef},
builtins::{PyBaseExceptionRef, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef},
class::{PyClassImpl, StaticType},
function::{Either, FuncArgs, PyComparisonValue, PyMethodDef, PyMethodFlags},
iter::PyExactSizeIterator,
Expand Down Expand Up @@ -225,7 +225,21 @@ pub trait PyStructSequence: StaticType + PyClassImpl + Sized + 'static {
} else {
(String::new(), "...")
};
let repr_str = format!("{}({}{})", Self::TP_NAME, body, suffix);
// Build qualified name: if MODULE_NAME is already in TP_NAME, use it directly.
// Otherwise, check __module__ attribute (set by #[pymodule] at runtime).
let type_name = if Self::MODULE_NAME.is_some() {
alloc::borrow::Cow::Borrowed(Self::TP_NAME)
} else {
let typ = zelf.class();
match typ.get_attr(identifier!(vm.ctx, __module__)) {
Some(module) if module.downcastable::<PyStr>() => {
let module_str = module.downcast_ref::<PyStr>().unwrap();
alloc::borrow::Cow::Owned(format!("{}.{}", module_str.as_str(), Self::NAME))
}
_ => alloc::borrow::Cow::Borrowed(Self::TP_NAME),
}
};
let repr_str = format!("{}({}{})", type_name, body, suffix);
Ok(vm.ctx.new_str(repr_str))
}

Expand Down
Loading