Skip to content

Commit 051afd1

Browse files
committed
Add option for enable/disable enum members in docstring.
1 parent 98f1bbb commit 051afd1

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

include/pybind11/options.h

+7
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ class options {
3838

3939
options& enable_function_signatures() & { global_state().show_function_signatures = true; return *this; }
4040

41+
options& disable_enum_members_docstring() & { global_state().show_enum_members_docstring = false; return *this; }
42+
43+
options& enable_enum_members_docstring() & { global_state().show_enum_members_docstring = true; return *this; }
44+
4145
// Getter methods (return the global state):
4246

4347
static bool show_user_defined_docstrings() { return global_state().show_user_defined_docstrings; }
4448

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

51+
static bool show_enum_members_docstring() { return global_state().show_enum_members_docstring; }
52+
4753
// This type is not meant to be allocated on the heap.
4854
void* operator new(size_t) = delete;
4955

@@ -52,6 +58,7 @@ class options {
5258
struct state {
5359
bool show_user_defined_docstrings = true; //< Include user-supplied texts in docstrings.
5460
bool show_function_signatures = true; //< Include auto-generated function signatures in docstrings.
61+
bool show_enum_members_docstring = true; //< Include auto-generated member list in enum docstring.
5562
};
5663

5764
static state &global_state() {

include/pybind11/pybind11.h

+19-17
Original file line numberDiff line numberDiff line change
@@ -1594,23 +1594,25 @@ struct enum_base {
15941594
}, name("name"), is_method(m_base)
15951595
);
15961596

1597-
m_base.attr("__doc__") = static_property(cpp_function(
1598-
[](handle arg) -> std::string {
1599-
std::string docstring;
1600-
dict entries = arg.attr("__entries");
1601-
if (((PyTypeObject *) arg.ptr())->tp_doc)
1602-
docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n";
1603-
docstring += "Members:";
1604-
for (auto kv : entries) {
1605-
auto key = std::string(pybind11::str(kv.first));
1606-
auto comment = kv.second[int_(1)];
1607-
docstring += "\n\n " + key;
1608-
if (!comment.is_none())
1609-
docstring += " : " + (std::string) pybind11::str(comment);
1610-
}
1611-
return docstring;
1612-
}, name("__doc__")
1613-
), none(), none(), "");
1597+
if (options::show_enum_members_docstring()) {
1598+
m_base.attr("__doc__") = static_property(cpp_function(
1599+
[](handle arg) -> std::string {
1600+
std::string docstring;
1601+
dict entries = arg.attr("__entries");
1602+
if (((PyTypeObject *) arg.ptr())->tp_doc)
1603+
docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n";
1604+
docstring += "Members:";
1605+
for (auto kv : entries) {
1606+
auto key = std::string(pybind11::str(kv.first));
1607+
auto comment = kv.second[int_(1)];
1608+
docstring += "\n\n " + key;
1609+
if (!comment.is_none())
1610+
docstring += " : " + (std::string) pybind11::str(comment);
1611+
}
1612+
return docstring;
1613+
}, name("__doc__")
1614+
), none(), none(), "");
1615+
}
16141616

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

0 commit comments

Comments
 (0)