Skip to content

Commit

Permalink
Update classmethod with reviewer comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Skylion007 committed Oct 11, 2022
1 parent e5f8666 commit 2e3d29d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 5 additions & 5 deletions tests/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
5 changes: 3 additions & 2 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 2e3d29d

Please sign in to comment.