mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 08:03:55 +00:00
Update classmethod with reviewer comments
This commit is contained in:
parent
e5f866626e
commit
2e3d29de45
@ -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__
|
||||
|
@ -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():
|
||||
|
Loading…
Reference in New Issue
Block a user