Skip to content
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

Implement classmethod pytype #4158

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,18 @@ class class_ : public detail::generic_type {
return *this;
}

template <typename Func, typename... Extra>
class_ &def_classmethod(const char *name_, Func &&f, const Extra &...extra) {
cpp_function cf(std::forward<Func>(f),
name(name_),
is_method(*this),
sibling(getattr(*this, name_, none())),
extra...);
auto cf_name = cf.name();
attr(std::move(cf_name)) = classmethod(std::move(cf));
return *this;
}

template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
class_ &def(const detail::op_<id, ot, L, R> &op, const Extra &...extra) {
op.execute(*this, extra...);
Expand Down
6 changes: 6 additions & 0 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,7 @@ inline bool PyUnicode_Check_Permissive(PyObject *o) {
#endif

inline bool PyStaticMethod_Check(PyObject *o) { return o->ob_type == &PyStaticMethod_Type; }
inline bool PyClassMethod_Check(PyObject *o) { return o->ob_type == &PyClassMethod_Type; }

class kwargs_proxy : public handle {
public:
Expand Down Expand Up @@ -2089,6 +2090,11 @@ class staticmethod : public object {
PYBIND11_OBJECT_CVT(staticmethod, object, detail::PyStaticMethod_Check, PyStaticMethod_New)
};

class classmethod : public object {
rwgk marked this conversation as resolved.
Show resolved Hide resolved
public:
PYBIND11_OBJECT_CVT(classmethod, object, detail::PyClassMethod_Check, PyClassMethod_New)
};

class buffer : public object {
public:
PYBIND11_OBJECT_DEFAULT(buffer, object, PyObject_CheckBuffer)
Expand Down
10 changes: 9 additions & 1 deletion tests/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ TEST_SUBMODULE(class_, m) {
};

py::class_<NoConstructor>(m, "NoConstructor")
.def_static("new_instance", &NoConstructor::new_instance, "Return an instance");
.def_static("new_instance", &NoConstructor::new_instance, "Return an instance")
.def_classmethod(
"new_instance_uuid",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe serial or seq or similar? (It doesn't look like a universally unique identifier.)

[](py::object &cls) {
py::int_ uuid = getattr(cls, "uuid", py::int_(0));
cls.attr("uuid") = uuid + py::int_(1);
return NoConstructorNew::new_instance();
},
"Returns a new instance and then increment the uuid");

py::class_<NoConstructorNew>(m, "NoConstructorNew")
.def(py::init([](const NoConstructorNew &self) { return self; })) // Need a NOOP __init__
Expand Down
6 changes: 6 additions & 0 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def test_instance_new(msg):
assert cstats.alive() == 0


def test_classmethod(num_instances=10):
for i in range(num_instances):
assert getattr(m.NoConstructor, "uuid", 0) == i
m.NoConstructor.new_instance_uuid()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe (untested):

    assert no hasattr(m.NoConstructor, "uuid")
    for i in range(num_instances):
        m.NoConstructor.new_instance_uuid()
        assert m.NoConstructor.uuid == i + 1

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ...
Commit 2e3d29d from two months ago escaped my attention.
If you reply with Done or similar here it's more likely that that doesn't happen.


def test_type():
assert m.check_type(1) == m.DerivedClass1
with pytest.raises(RuntimeError) as execinfo:
Expand Down