Update classmethod with reviewer comments

This commit is contained in:
Aaron Gokaslan 2022-10-11 11:30:09 -07:00
parent e5f866626e
commit 2e3d29de45
2 changed files with 8 additions and 7 deletions

View File

@ -64,13 +64,13 @@ TEST_SUBMODULE(class_, m) {
py::class_<NoConstructor>(m, "NoConstructor")
.def_static("new_instance", &NoConstructor::new_instance, "Return an instance")
.def_classmethod(
"new_instance_uuid",
[](py::object &cls) {
py::int_ uuid = getattr(cls, "uuid", py::int_(0));
cls.attr("uuid") = uuid + py::int_(1);
"new_instance_seq_id",
[](py::type &cls) {
py::int_ seq_id = getattr(cls, "seq_id", py::int_(0));
cls.attr("seq_id") = seq_id + py::int_(1);
return NoConstructorNew::new_instance();
},
"Returns a new instance and then increment the uuid");
"Returns a new instance and then increment the seq_id");
py::class_<NoConstructorNew>(m, "NoConstructorNew")
.def(py::init([](const NoConstructorNew &self) { return self; })) // Need a NOOP __init__

View File

@ -32,9 +32,10 @@ def test_instance_new(msg):
def test_classmethod(num_instances=10):
assert not hasattr(m.NoConstructor, "seq_id")
for i in range(num_instances):
assert getattr(m.NoConstructor, "uuid", 0) == i
m.NoConstructor.new_instance_uuid()
m.NoConstructor.new_instance_seq_id()
assert m.NoConstructor.seq_id == i + 1
def test_type():