Skip to content

Commit a992757

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

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
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

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

1597+
bool show_enum_members = options::show_enum_members_docstring();
15971598
m_base.attr("__doc__") = static_property(cpp_function(
1598-
[](handle arg) -> std::string {
1599+
[show_enum_members](handle arg) -> std::string {
15991600
std::string docstring;
1600-
dict entries = arg.attr("__entries");
16011601
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);
1602+
docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc);
1603+
if (show_enum_members) {
1604+
docstring += "\n\nMembers:";
1605+
dict entries = arg.attr("__entries");
1606+
for (auto kv : entries) {
1607+
auto key = std::string(pybind11::str(kv.first));
1608+
auto comment = kv.second[int_(1)];
1609+
docstring += "\n\n " + key;
1610+
if (!comment.is_none())
1611+
docstring += " : " + (std::string) pybind11::str(comment);
1612+
}
16101613
}
16111614
return docstring;
16121615
}, name("__doc__")

0 commit comments

Comments
 (0)