Skip to content

Commit 7e7d14a

Browse files
committed
Add support for docstrings on enums
Cf. pybind/pybind11#1160 and pybind/pybind11#2275 (comment)
1 parent 3a4f1a2 commit 7e7d14a

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

src/expose.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,10 @@ void EnumExposer::emitIntroducer(llvm::raw_ostream &os,
999999
emitType(os);
10001000
os << "(" << parent_identifier << ", ";
10011001
emitSpelling(os, enum_decl, annotations.lookup<NamedDeclAttrs>(enum_decl));
1002+
if (llvm::StringRef doc = getBriefText(enum_decl); !doc.empty()) {
1003+
os << ", ";
1004+
emitStringLiteral(os, doc);
1005+
}
10021006
if (const auto attrs = annotations.get<EnumDeclAttrs>(enum_decl);
10031007
attrs.has_value() && attrs->arithmetic) {
10041008
os << ", ::pybind11::arithmetic()";
@@ -1024,7 +1028,12 @@ void EnumExposer::handleDeclImpl(llvm::raw_ostream &os,
10241028
const std::string scope = getFullyQualifiedName(enum_decl);
10251029
os << "context.value(";
10261030
emitSpelling(os, decl, annotations.lookup<NamedDeclAttrs>(decl));
1027-
os << ", " << scope << "::" << enumerator->getName() << ");\n";
1031+
os << ", " << scope << "::" << enumerator->getName();
1032+
if (llvm::StringRef doc = getBriefText(decl); !doc.empty()) {
1033+
os << ", ";
1034+
emitStringLiteral(os, doc);
1035+
}
1036+
os << ");\n";
10281037
}
10291038
}
10301039

@@ -1050,6 +1059,10 @@ void RecordExposer::emitIntroducer(llvm::raw_ostream &os,
10501059
os << "(" << parent_identifier << ", ";
10511060
emitSpelling(os, record_decl,
10521061
annotations.lookup<NamedDeclAttrs>(record_decl));
1062+
if (llvm::StringRef doc = getBriefText(record_decl); !doc.empty()) {
1063+
os << ", ";
1064+
emitStringLiteral(os, doc);
1065+
}
10531066
if (const auto attrs = annotations.get<RecordDeclAttrs>(record_decl);
10541067
attrs.has_value() && attrs->dynamic_attr) {
10551068
os << ", ::pybind11::dynamic_attr()";
@@ -1059,9 +1072,6 @@ void RecordExposer::emitIntroducer(llvm::raw_ostream &os,
10591072

10601073
void RecordExposer::finalizeDefinition(llvm::raw_ostream &os) {
10611074
emitProperties(os);
1062-
os << "context.doc() = ";
1063-
emitStringLiteral(os, getBriefText(record_decl));
1064-
os << ";\n";
10651075
}
10661076

10671077
void RecordExposer::emitProperties(llvm::raw_ostream &os) {

tests/integration/docstrings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "docstrings.h"

tests/integration/docstrings.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include "genpybind.h"
4+
5+
/// Describes how the output will taste.
6+
enum class GENPYBIND(visible) Flavor {
7+
/// Like you would expect.
8+
bland,
9+
/// It tastes different.
10+
fruity,
11+
};
12+
13+
/// A contrived example.
14+
///
15+
/// Only the “brief” docstring is used in the Python bindings.
16+
class GENPYBIND(visible) Example {};
17+
18+
/// Also here.
19+
class GENPYBIND(dynamic_attr) Dynamic {};

tests/integration/docstrings_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import docstrings as m
2+
3+
4+
def test_enums_support_docstrings():
5+
expected = """\
6+
Describes how the output will taste.
7+
8+
Members:
9+
10+
bland : Like you would expect.
11+
12+
fruity : It tastes different.\
13+
"""
14+
assert m.Flavor.__doc__ == expected
15+
16+
17+
def test_classes_support_docstrings():
18+
assert m.Example.__doc__ == "A contrived example."
19+
# Docstrings also work with extra arguments to `class_`.
20+
assert m.Dynamic.__doc__ == "Also here."

tests/integration/enums-can-be-arithmetic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum GENPYBIND(arithmetic(false)) ExplicitFalse {
1818
Three = 3,
1919
};
2020

21+
/// Docstrings are also supported.
2122
enum GENPYBIND(arithmetic(true)) ExplicitTrue {
2223
Four = 4,
2324
Five = 5,

tests/integration/enums-can-be-arithmetic_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ def test_arithmetic_true_can_use_bitwise_operators():
2424
def test_enums_arent_arithmetic_by_default():
2525
with pytest.raises(TypeError, match="unsupported operand type"):
2626
m.Default.Seven | m.Default.Eight
27+
28+
29+
def test_arithmetic_enums_support_docstrings():
30+
assert m.ExplicitTrue.__doc__.startswith("Docstrings are also supported.")

0 commit comments

Comments
 (0)