Skip to content

Commit 18be530

Browse files
committed
Add option for enable/disable enum members in docstring.
1 parent 9c18a74 commit 18be530

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

Diff for: include/pybind11/options.h

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class options {
4747
return *this;
4848
}
4949

50+
options& disable_enum_members_docstring() & { global_state().show_enum_members_docstring = false; return *this; }
51+
52+
options& enable_enum_members_docstring() & { global_state().show_enum_members_docstring = true; return *this; }
53+
5054
// Getter methods (return the global state):
5155

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

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

62+
static bool show_enum_members_docstring() { return global_state().show_enum_members_docstring; }
63+
5864
// This type is not meant to be allocated on the heap.
5965
void *operator new(size_t) = delete;
6066

@@ -63,6 +69,8 @@ class options {
6369
bool show_user_defined_docstrings = true; //< Include user-supplied texts in docstrings.
6470
bool show_function_signatures = true; //< Include auto-generated function signatures
6571
// in docstrings.
72+
bool show_enum_members_docstring = true; //< Include auto-generated member list in enum
73+
// docstrings.
6674
};
6775

6876
static state &global_state() {

Diff for: include/pybind11/pybind11.h

+24-22
Original file line numberDiff line numberDiff line change
@@ -1974,29 +1974,31 @@ struct enum_base {
19741974
name("name"),
19751975
is_method(m_base));
19761976

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

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

0 commit comments

Comments
 (0)