Skip to content

classmethod copy attrs #5831

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

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1558,8 +1558,6 @@ class B(A1, A2):
else:
self.fail("finding the most derived metaclass should have failed")

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_classmethods(self):
# Testing class methods...
class C(object):
Expand Down
33 changes: 24 additions & 9 deletions vm/src/builtins/classmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,34 @@ impl Constructor for PyClassMethod {
type Args = PyObjectRef;

fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult {
let doc = callable.get_attr("__doc__", vm);
// Create a dictionary to hold copied attributes
let dict = vm.ctx.new_dict();

let result = PyClassMethod {
callable: PyMutex::new(callable),
// Copy attributes from the callable to the dict
// This is similar to functools.wraps in CPython
if let Ok(doc) = callable.get_attr("__doc__", vm) {
dict.set_item(identifier!(vm.ctx, __doc__), doc, vm)?;
}
.into_ref_with_type(vm, cls)?;
let obj = PyObjectRef::from(result);

if let Ok(doc) = doc {
obj.set_attr("__doc__", doc, vm)?;
if let Ok(name) = callable.get_attr("__name__", vm) {
dict.set_item(identifier!(vm.ctx, __name__), name, vm)?;
}
if let Ok(qualname) = callable.get_attr("__qualname__", vm) {
dict.set_item(identifier!(vm.ctx, __qualname__), qualname, vm)?;
}
if let Ok(module) = callable.get_attr("__module__", vm) {
dict.set_item(identifier!(vm.ctx, __module__), module, vm)?;
}
if let Ok(annotations) = callable.get_attr("__annotations__", vm) {
dict.set_item(identifier!(vm.ctx, __annotations__), annotations, vm)?;
}

// Create PyClassMethod instance with the pre-populated dict
let classmethod = PyClassMethod {
callable: PyMutex::new(callable),
};

Ok(obj)
let result = PyRef::new_ref(classmethod, cls, Some(dict));
Ok(PyObjectRef::from(result))
}
}

Expand Down
Loading