Ensure __new__ entries in class __dict__ are staticmethods (#6659)

* Initial plan

* Wrap __new__ methods as staticmethods in type dicts

Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>

* Unmark passing enum test

Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
This commit is contained in:
Copilot
2026-01-06 12:16:51 +09:00
committed by GitHub
parent 906aebf5a1
commit 03380dc4ec
3 changed files with 10 additions and 3 deletions

View File

@@ -5208,7 +5208,6 @@ class TestStdLib(unittest.TestCase):
]),
)
@unittest.expectedFailure # TODO: RUSTPYTHON; len is often/always > 256
def test_test_simple_enum(self):
@_simple_enum(Enum)
class SimpleColor:

View File

@@ -1,6 +1,6 @@
use super::{
PyClassMethod, PyDictRef, PyList, PyStr, PyStrInterned, PyStrRef, PyTupleRef, PyWeak,
mappingproxy::PyMappingProxy, object, union_,
PyClassMethod, PyDictRef, PyList, PyStaticMethod, PyStr, PyStrInterned, PyStrRef, PyTupleRef,
PyWeak, mappingproxy::PyMappingProxy, object, union_,
};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
@@ -1166,6 +1166,12 @@ impl Constructor for PyType {
*f = PyClassMethod::from(f.clone()).into_pyobject(vm);
}
if let Some(f) = attributes.get_mut(identifier!(vm, __new__))
&& f.class().is(vm.ctx.types.function_type)
{
*f = PyStaticMethod::from(f.clone()).into_pyobject(vm);
}
if let Some(current_frame) = vm.current_frame() {
let entry = attributes.entry(identifier!(vm, __module__));
if matches!(entry, Entry::Vacant(_)) {

View File

@@ -584,6 +584,7 @@ assert ClassWithNew.__new__.__qualname__ == "ClassWithNew.__new__"
assert ClassWithNew().__new__.__qualname__ == "ClassWithNew.__new__"
assert ClassWithNew.__new__.__name__ == "__new__"
assert ClassWithNew().__new__.__name__ == "__new__"
assert isinstance(ClassWithNew.__dict__.get("__new__"), staticmethod)
assert ClassWithNew.N.__new__.__qualname__ == "ClassWithNew.N.__new__"
assert ClassWithNew().N.__new__.__qualname__ == "ClassWithNew.N.__new__"
@@ -593,6 +594,7 @@ assert ClassWithNew.N().__new__.__qualname__ == "ClassWithNew.N.__new__"
assert ClassWithNew().N().__new__.__qualname__ == "ClassWithNew.N.__new__"
assert ClassWithNew.N().__new__.__name__ == "__new__"
assert ClassWithNew().N().__new__.__name__ == "__new__"
assert isinstance(ClassWithNew.N.__dict__.get("__new__"), staticmethod)
# Regression to: