Skip to content

Commit 771fecc

Browse files
committed
Add option for enable/disable enum members in docstring
1 parent e133c33 commit 771fecc

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

include/pybind11/options.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ class options {
4747
return *this;
4848
}
4949

50+
options& disable_enum_members_docstring() & {
51+
global_state().show_enum_members_docstring = false;
52+
return *this;
53+
}
54+
55+
options& enable_enum_members_docstring() & {
56+
global_state().show_enum_members_docstring = true;
57+
return *this;
58+
}
59+
5060
// Getter methods (return the global state):
5161

5262
static bool show_user_defined_docstrings() {
@@ -55,6 +65,10 @@ class options {
5565

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

68+
static bool show_enum_members_docstring() {
69+
return global_state().show_enum_members_docstring;
70+
}
71+
5872
// This type is not meant to be allocated on the heap.
5973
void *operator new(size_t) = delete;
6074

@@ -63,6 +77,8 @@ class options {
6377
bool show_user_defined_docstrings = true; //< Include user-supplied texts in docstrings.
6478
bool show_function_signatures = true; //< Include auto-generated function signatures
6579
// in docstrings.
80+
bool show_enum_members_docstring = true; //< Include auto-generated member list in enum
81+
// docstrings.
6682
};
6783

6884
static state &global_state() {

include/pybind11/pybind11.h

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,29 +1972,32 @@ struct enum_base {
19721972
name("name"),
19731973
is_method(m_base));
19741974

1975-
m_base.attr("__doc__") = static_property(
1976-
cpp_function(
1977-
[](handle arg) -> std::string {
1978-
std::string docstring;
1979-
dict entries = arg.attr("__entries");
1980-
if (((PyTypeObject *) arg.ptr())->tp_doc) {
1981-
docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n";
1982-
}
1983-
docstring += "Members:";
1984-
for (auto kv : entries) {
1985-
auto key = std::string(pybind11::str(kv.first));
1986-
auto comment = kv.second[int_(1)];
1987-
docstring += "\n\n " + key;
1988-
if (!comment.is_none()) {
1989-
docstring += " : " + (std::string) pybind11::str(comment);
1975+
if (options::show_enum_members_docstring()) {
1976+
m_base.attr("__doc__") = static_property(
1977+
cpp_function(
1978+
[](handle arg) -> std::string {
1979+
std::string docstring;
1980+
dict entries = arg.attr("__entries");
1981+
if (((PyTypeObject *) arg.ptr())->tp_doc) {
1982+
docstring
1983+
+= std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n";
19901984
}
1991-
}
1992-
return docstring;
1993-
},
1994-
name("__doc__")),
1995-
none(),
1996-
none(),
1997-
"");
1985+
docstring += "Members:";
1986+
for (auto kv : entries) {
1987+
auto key = std::string(pybind11::str(kv.first));
1988+
auto comment = kv.second[int_(1)];
1989+
docstring += "\n\n " + key;
1990+
if (!comment.is_none()) {
1991+
docstring += " : " + (std::string) pybind11::str(comment);
1992+
}
1993+
}
1994+
return docstring;
1995+
},
1996+
name("__doc__")),
1997+
none(),
1998+
none(),
1999+
"");
2000+
}
19982001

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

0 commit comments

Comments
 (0)