Skip to content

Commit 5ec19d6

Browse files
committed
Add support for pybind11 enum docstrings
1 parent cf58aa6 commit 5ec19d6

File tree

10 files changed

+108
-6
lines changed
  • pybind11_stubgen
  • tests
    • py-demo/bindings/src/modules
    • stubs
      • python-3.12
        • pybind11-master
          • numpy-array-use-type-var/demo/_bindings
          • numpy-array-wrap-with-annotated/demo/_bindings
        • pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings
        • pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings
      • python-3.7/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings
      • python-3.8/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings

10 files changed

+108
-6
lines changed

pybind11_stubgen/parser/mixins/parse.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,17 @@ def handle_alias(self, path: QualifiedName, origin: Any) -> Alias | None:
171171
)
172172

173173
def handle_attribute(self, path: QualifiedName, value: Any) -> Attribute | None:
174+
doc = None
175+
entries = getattr(value, "__entries", None)
176+
177+
if entries is not None and path[-1] in entries:
178+
doc = self.handle_docstring(path, entries[path[-1]][1])
179+
174180
return Attribute(
175181
name=path[-1],
176182
value=self.handle_value(value),
177183
annotation=ResolvedType(name=self.handle_type(type(value))),
184+
doc=doc,
178185
)
179186

180187
def handle_bases(
@@ -195,6 +202,7 @@ def handle_field(self, path: QualifiedName, value: Any) -> Field | None:
195202
attribute=Attribute(
196203
name=attr.name,
197204
value=attr.value,
205+
doc=attr.doc
198206
),
199207
modifier="static",
200208
)

pybind11_stubgen/printer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ def print_attribute(self, attr: Attribute) -> list[str]:
5151
if attr.value is not None:
5252
parts.append(f" # value = {self.print_value(attr.value)}")
5353

54-
return ["".join(parts)]
54+
result = ["".join(parts)]
55+
if attr.doc is not None:
56+
result.extend(self.print_docstring(attr.doc))
57+
return result
5558

5659
def print_argument(self, arg: Argument) -> str:
5760
parts = []

pybind11_stubgen/structs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class Attribute:
108108
name: Identifier
109109
value: Value | None
110110
annotation: Annotation | None = field_(default=None)
111+
doc: Docstring | None = field_(default=None)
111112

112113

113114
@dataclass

tests/py-demo/bindings/src/modules/enum.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
void bind_enum_module(py::module&&m) {
66

77
py::enum_<demo::sublibA::ConsoleForegroundColor>(m, "ConsoleForegroundColor")
8-
.value("Green", demo::sublibA::ConsoleForegroundColor::Green)
9-
.value("Yellow", demo::sublibA::ConsoleForegroundColor::Yellow)
10-
.value("Blue", demo::sublibA::ConsoleForegroundColor::Blue)
11-
.value("Magenta", demo::sublibA::ConsoleForegroundColor::Magenta)
12-
.value("None_", demo::sublibA::ConsoleForegroundColor::None_)
8+
.value("Green", demo::sublibA::ConsoleForegroundColor::Green, "Green color")
9+
.value("Yellow", demo::sublibA::ConsoleForegroundColor::Yellow, "Yellow color")
10+
.value("Blue", demo::sublibA::ConsoleForegroundColor::Blue, "Blue color")
11+
.value("Magenta", demo::sublibA::ConsoleForegroundColor::Magenta, "Magenta color")
12+
.value("None_", demo::sublibA::ConsoleForegroundColor::None_, "No color")
1313
.export_values();
1414

1515
m.def(

tests/stubs/python-3.12/pybind11-master/numpy-array-use-type-var/demo/_bindings/enum.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,33 @@ class ConsoleForegroundColor:
3030
Blue: typing.ClassVar[
3131
ConsoleForegroundColor
3232
] # value = <ConsoleForegroundColor.Blue: 34>
33+
"""
34+
Blue color
35+
"""
3336
Green: typing.ClassVar[
3437
ConsoleForegroundColor
3538
] # value = <ConsoleForegroundColor.Green: 32>
39+
"""
40+
Green color
41+
"""
3642
Magenta: typing.ClassVar[
3743
ConsoleForegroundColor
3844
] # value = <ConsoleForegroundColor.Magenta: 35>
45+
"""
46+
Magenta color
47+
"""
3948
None_: typing.ClassVar[
4049
ConsoleForegroundColor
4150
] # value = <ConsoleForegroundColor.None_: -1>
51+
"""
52+
No color
53+
"""
4254
Yellow: typing.ClassVar[
4355
ConsoleForegroundColor
4456
] # value = <ConsoleForegroundColor.Yellow: 33>
57+
"""
58+
Yellow color
59+
"""
4560
__members__: typing.ClassVar[
4661
dict[str, ConsoleForegroundColor]
4762
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}

tests/stubs/python-3.12/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/enum.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,33 @@ class ConsoleForegroundColor:
3030
Blue: typing.ClassVar[
3131
ConsoleForegroundColor
3232
] # value = <ConsoleForegroundColor.Blue: 34>
33+
"""
34+
Blue color
35+
"""
3336
Green: typing.ClassVar[
3437
ConsoleForegroundColor
3538
] # value = <ConsoleForegroundColor.Green: 32>
39+
"""
40+
Green color
41+
"""
3642
Magenta: typing.ClassVar[
3743
ConsoleForegroundColor
3844
] # value = <ConsoleForegroundColor.Magenta: 35>
45+
"""
46+
Magenta color
47+
"""
3948
None_: typing.ClassVar[
4049
ConsoleForegroundColor
4150
] # value = <ConsoleForegroundColor.None_: -1>
51+
"""
52+
No color
53+
"""
4254
Yellow: typing.ClassVar[
4355
ConsoleForegroundColor
4456
] # value = <ConsoleForegroundColor.Yellow: 33>
57+
"""
58+
Yellow color
59+
"""
4560
__members__: typing.ClassVar[
4661
dict[str, ConsoleForegroundColor]
4762
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}

tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/enum.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,33 @@ class ConsoleForegroundColor:
3030
Blue: typing.ClassVar[
3131
ConsoleForegroundColor
3232
] # value = <ConsoleForegroundColor.Blue: 34>
33+
"""
34+
Blue color
35+
"""
3336
Green: typing.ClassVar[
3437
ConsoleForegroundColor
3538
] # value = <ConsoleForegroundColor.Green: 32>
39+
"""
40+
Green color
41+
"""
3642
Magenta: typing.ClassVar[
3743
ConsoleForegroundColor
3844
] # value = <ConsoleForegroundColor.Magenta: 35>
45+
"""
46+
Magenta color
47+
"""
3948
None_: typing.ClassVar[
4049
ConsoleForegroundColor
4150
] # value = <ConsoleForegroundColor.None_: -1>
51+
"""
52+
No color
53+
"""
4254
Yellow: typing.ClassVar[
4355
ConsoleForegroundColor
4456
] # value = <ConsoleForegroundColor.Yellow: 33>
57+
"""
58+
Yellow color
59+
"""
4560
__members__: typing.ClassVar[
4661
dict[str, ConsoleForegroundColor]
4762
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}

tests/stubs/python-3.12/pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings/enum.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,33 @@ class ConsoleForegroundColor:
3030
Blue: typing.ClassVar[
3131
ConsoleForegroundColor
3232
] # value = <ConsoleForegroundColor.Blue: 34>
33+
"""
34+
Blue color
35+
"""
3336
Green: typing.ClassVar[
3437
ConsoleForegroundColor
3538
] # value = <ConsoleForegroundColor.Green: 32>
39+
"""
40+
Green color
41+
"""
3642
Magenta: typing.ClassVar[
3743
ConsoleForegroundColor
3844
] # value = <ConsoleForegroundColor.Magenta: 35>
45+
"""
46+
Magenta color
47+
"""
3948
None_: typing.ClassVar[
4049
ConsoleForegroundColor
4150
] # value = <ConsoleForegroundColor.None_: -1>
51+
"""
52+
No color
53+
"""
4254
Yellow: typing.ClassVar[
4355
ConsoleForegroundColor
4456
] # value = <ConsoleForegroundColor.Yellow: 33>
57+
"""
58+
Yellow color
59+
"""
4560
__members__: typing.ClassVar[
4661
dict[str, ConsoleForegroundColor]
4762
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}

tests/stubs/python-3.7/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/enum.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,33 @@ class ConsoleForegroundColor:
3030
Blue: typing.ClassVar[
3131
ConsoleForegroundColor
3232
] # value = <ConsoleForegroundColor.Blue: 34>
33+
"""
34+
Blue color
35+
"""
3336
Green: typing.ClassVar[
3437
ConsoleForegroundColor
3538
] # value = <ConsoleForegroundColor.Green: 32>
39+
"""
40+
Green color
41+
"""
3642
Magenta: typing.ClassVar[
3743
ConsoleForegroundColor
3844
] # value = <ConsoleForegroundColor.Magenta: 35>
45+
"""
46+
Magenta color
47+
"""
3948
None_: typing.ClassVar[
4049
ConsoleForegroundColor
4150
] # value = <ConsoleForegroundColor.None_: -1>
51+
"""
52+
No color
53+
"""
4254
Yellow: typing.ClassVar[
4355
ConsoleForegroundColor
4456
] # value = <ConsoleForegroundColor.Yellow: 33>
57+
"""
58+
Yellow color
59+
"""
4560
__members__: typing.ClassVar[
4661
dict[str, ConsoleForegroundColor]
4762
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}

tests/stubs/python-3.8/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/enum.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,33 @@ class ConsoleForegroundColor:
3030
Blue: typing.ClassVar[
3131
ConsoleForegroundColor
3232
] # value = <ConsoleForegroundColor.Blue: 34>
33+
"""
34+
Blue color
35+
"""
3336
Green: typing.ClassVar[
3437
ConsoleForegroundColor
3538
] # value = <ConsoleForegroundColor.Green: 32>
39+
"""
40+
Green color
41+
"""
3642
Magenta: typing.ClassVar[
3743
ConsoleForegroundColor
3844
] # value = <ConsoleForegroundColor.Magenta: 35>
45+
"""
46+
Magenta color
47+
"""
3948
None_: typing.ClassVar[
4049
ConsoleForegroundColor
4150
] # value = <ConsoleForegroundColor.None_: -1>
51+
"""
52+
No color
53+
"""
4254
Yellow: typing.ClassVar[
4355
ConsoleForegroundColor
4456
] # value = <ConsoleForegroundColor.Yellow: 33>
57+
"""
58+
Yellow color
59+
"""
4560
__members__: typing.ClassVar[
4661
dict[str, ConsoleForegroundColor]
4762
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}

0 commit comments

Comments
 (0)