Class attribute tables
Builtin attribute calls currently use per-type dispatchers, while class attributes and classmethods have separate hooks.
This PR gives each native namespace one attribute table for lookup and internal enumeration.
For example, str.upper("foo") and "foo".upper() call the same implementation, with receiver validation for the
unbound call.
Attribute definitions
#[pyattrs] generates PyAttrs::TABLE and retains the handlers as inherent Rust methods:
pub(crate) struct PyListImpl;
#[pyattrs]
impl<'heap> PyAttrs<'heap> for PyListImpl {
type Receiver = HeapReadMut<'heap, List>;
#[pymethod(Append)]
fn append<'h>(list: &HeapReadMut<'h, List>, args: ArgValues, vm: &mut VM<'h>) -> RunResult<Value> {
let item = args.get_one_arg("list.append", vm.heap)?;
list.append(vm, item)?;
Ok(Value::None)
}
}
Annotations name an existing StaticStrings key explicitly:
#[pymethod(Append)]: instance method with a native receiver.#[pyclassmethod(Fromkeys)]: classmethod receiving the accessing class asTypeRef.#[pyvalue(Min)]: attribute value factory.
The generated table contains enumerable entries and a lookup function.
Rust checks handler signatures; argument parsing stays in the implementation.
Adapters project the existing PyValue into the declared native view and preserve CallResult, including suspension.
Inheritance and dispatch
Each TypeDef registers its table through attrs.
Type::lookup_attr walks the native MRO and returns the first definition together with its defining type.
This replaces the separate table-selection match and the old class_attr and class_method hooks.
Unbound native descriptors validate against their defining type.
For example, defaultdict.get({}, "key") accepts a plain dict because dict defines get.
Classmethods bind to the accessing class, including calls through instances:
defaultdict.fromkeys(...) produces a defaultdict with no default factory.
The duplicate PyClass representation is removed in favor of TypeRef.
Bases with differently represented native subtypes register a subtype_call adapter.
Tuple descriptors use namedtuple storage; date descriptors adapt datetime values, preserving the special behavior of
strftime and replace.
Dict subtypes share a payload and inherit common methods without forwarding wrappers.
Inherited datetime.today() uses the session clock and supports host suspension.
Table misses retain dynamic handling for modules, user-defined namespaces, stored callables, and ForwardRef stringifiers.
Direct calls do not allocate bound-method objects.
Scope
The migration covers collections, strings and bytes, complex and decimal types, datetime, regex, paths and files, random
generators, hashes, CSV writers, cache wrappers, and ForwardRef.
Object attributes, BLAKE2 constants, and chain.from_iterable also use tables.
Standalone builtin method objects, method lookup through getattr, and a Python-visible builtin __dict__ remain outside
this PR.
Assigning str.upper to a variable still raises AttributeError.
The change preserves opcode encodings and existing dump compatibility.
Review entry points:
attribute definitions and dispatch,
MRO lookup,
native registrations, and
macro documentation.
Validation
For the integration in 3cb34774:
- All 786 Monty fixtures pass.
- Macro tests and the class-inheritance, dump-compatibility, and OS-policy suites pass.
- The direct-method-call fixture passes on Monty and CPython, including Monty memory-model checks.
- OS-policy tests pass with memory-model checks, covering inherited clock calls and host suspension.
make format-rs,make lint-rs, andmake lint-pypass.
The full Monty and macro test suites also passed during the adaptation; affected suites were rerun after the final edits.
At 9b3a9639, the only subsequent change reorders DunderNew alphabetically.
Formatting and all 16 dump-compatibility tests pass at that commit.
The focused attribute-dispatch benchmark measured about 2% slower than the pre-integration branch.
Criterion classified the change as within its noise threshold.