Skip to content

Commit

Permalink
Add option for enable/disable enum members in docstring.
Browse files Browse the repository at this point in the history
  • Loading branch information
knarfS committed Nov 26, 2022
1 parent 9c18a74 commit 18be530
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
8 changes: 8 additions & 0 deletions include/pybind11/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class options {
return *this;
}

options& disable_enum_members_docstring() & { global_state().show_enum_members_docstring = false; return *this; }

options& enable_enum_members_docstring() & { global_state().show_enum_members_docstring = true; return *this; }

// Getter methods (return the global state):

static bool show_user_defined_docstrings() {
Expand All @@ -55,6 +59,8 @@ class options {

static bool show_function_signatures() { return global_state().show_function_signatures; }

static bool show_enum_members_docstring() { return global_state().show_enum_members_docstring; }

// This type is not meant to be allocated on the heap.
void *operator new(size_t) = delete;

Expand All @@ -63,6 +69,8 @@ class options {
bool show_user_defined_docstrings = true; //< Include user-supplied texts in docstrings.
bool show_function_signatures = true; //< Include auto-generated function signatures
// in docstrings.
bool show_enum_members_docstring = true; //< Include auto-generated member list in enum
// docstrings.
};

static state &global_state() {
Expand Down
46 changes: 24 additions & 22 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1974,29 +1974,31 @@ struct enum_base {
name("name"),
is_method(m_base));

m_base.attr("__doc__") = static_property(
cpp_function(
[](handle arg) -> std::string {
std::string docstring;
dict entries = arg.attr("__entries");
if (((PyTypeObject *) arg.ptr())->tp_doc) {
docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n";
}
docstring += "Members:";
for (auto kv : entries) {
auto key = std::string(pybind11::str(kv.first));
auto comment = kv.second[int_(1)];
docstring += "\n\n " + key;
if (!comment.is_none()) {
docstring += " : " + (std::string) pybind11::str(comment);
if (options::show_enum_members_docstring()) {
m_base.attr("__doc__") = static_property(
cpp_function(
[](handle arg) -> std::string {
std::string docstring;
dict entries = arg.attr("__entries");
if (((PyTypeObject *) arg.ptr())->tp_doc) {
docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n";
}
}
return docstring;
},
name("__doc__")),
none(),
none(),
"");
docstring += "Members:";
for (auto kv : entries) {
auto key = std::string(pybind11::str(kv.first));
auto comment = kv.second[int_(1)];
docstring += "\n\n " + key;
if (!comment.is_none()) {
docstring += " : " + (std::string) pybind11::str(comment);
}
}
return docstring;
},
name("__doc__")),
none(),
none(),
"");
}

m_base.attr("__members__") = static_property(cpp_function(
[](handle arg) -> dict {
Expand Down

0 comments on commit 18be530

Please sign in to comment.