Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pybind11 enum docstrings #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pybind11_stubgen/parser/mixins/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,17 @@ def handle_alias(self, path: QualifiedName, origin: Any) -> Alias | None:
)

def handle_attribute(self, path: QualifiedName, value: Any) -> Attribute | None:
doc = None
entries = getattr(value, "__entries", None)

if entries is not None and path[-1] in entries:
doc = self.handle_docstring(path, entries[path[-1]][1])

return Attribute(
name=path[-1],
value=self.handle_value(value),
annotation=ResolvedType(name=self.handle_type(type(value))),
doc=doc,
)

def handle_bases(
Expand All @@ -195,6 +202,7 @@ def handle_field(self, path: QualifiedName, value: Any) -> Field | None:
attribute=Attribute(
name=attr.name,
value=attr.value,
doc=attr.doc
),
modifier="static",
)
Expand Down
5 changes: 4 additions & 1 deletion pybind11_stubgen/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def print_attribute(self, attr: Attribute) -> list[str]:
if attr.value is not None:
parts.append(f" # value = {self.print_value(attr.value)}")

return ["".join(parts)]
result = ["".join(parts)]
if attr.doc is not None:
result.extend(self.print_docstring(attr.doc))
return result

def print_argument(self, arg: Argument) -> str:
parts = []
Expand Down
1 change: 1 addition & 0 deletions pybind11_stubgen/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Attribute:
name: Identifier
value: Value | None
annotation: Annotation | None = field_(default=None)
doc: Docstring | None = field_(default=None)


@dataclass
Expand Down
10 changes: 5 additions & 5 deletions tests/py-demo/bindings/src/modules/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
void bind_enum_module(py::module&&m) {

py::enum_<demo::sublibA::ConsoleForegroundColor>(m, "ConsoleForegroundColor")
.value("Green", demo::sublibA::ConsoleForegroundColor::Green)
.value("Yellow", demo::sublibA::ConsoleForegroundColor::Yellow)
.value("Blue", demo::sublibA::ConsoleForegroundColor::Blue)
.value("Magenta", demo::sublibA::ConsoleForegroundColor::Magenta)
.value("None_", demo::sublibA::ConsoleForegroundColor::None_)
.value("Green", demo::sublibA::ConsoleForegroundColor::Green, "Green color")
.value("Yellow", demo::sublibA::ConsoleForegroundColor::Yellow, "Yellow color")
.value("Blue", demo::sublibA::ConsoleForegroundColor::Blue, "Blue color")
.value("Magenta", demo::sublibA::ConsoleForegroundColor::Magenta, "Magenta color")
.value("None_", demo::sublibA::ConsoleForegroundColor::None_, "No color")
.export_values();

m.def(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,33 @@ class ConsoleForegroundColor:
Blue: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Blue: 34>
"""
Blue color
"""
Green: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Green: 32>
"""
Green color
"""
Magenta: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Magenta: 35>
"""
Magenta color
"""
None_: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.None_: -1>
"""
No color
"""
Yellow: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Yellow: 33>
"""
Yellow color
"""
__members__: typing.ClassVar[
dict[str, ConsoleForegroundColor]
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,33 @@ class ConsoleForegroundColor:
Blue: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Blue: 34>
"""
Blue color
"""
Green: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Green: 32>
"""
Green color
"""
Magenta: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Magenta: 35>
"""
Magenta color
"""
None_: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.None_: -1>
"""
No color
"""
Yellow: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Yellow: 33>
"""
Yellow color
"""
__members__: typing.ClassVar[
dict[str, ConsoleForegroundColor]
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,33 @@ class ConsoleForegroundColor:
Blue: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Blue: 34>
"""
Blue color
"""
Green: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Green: 32>
"""
Green color
"""
Magenta: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Magenta: 35>
"""
Magenta color
"""
None_: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.None_: -1>
"""
No color
"""
Yellow: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Yellow: 33>
"""
Yellow color
"""
__members__: typing.ClassVar[
dict[str, ConsoleForegroundColor]
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,33 @@ class ConsoleForegroundColor:
Blue: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Blue: 34>
"""
Blue color
"""
Green: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Green: 32>
"""
Green color
"""
Magenta: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Magenta: 35>
"""
Magenta color
"""
None_: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.None_: -1>
"""
No color
"""
Yellow: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Yellow: 33>
"""
Yellow color
"""
__members__: typing.ClassVar[
dict[str, ConsoleForegroundColor]
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,33 @@ class ConsoleForegroundColor:
Blue: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Blue: 34>
"""
Blue color
"""
Green: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Green: 32>
"""
Green color
"""
Magenta: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Magenta: 35>
"""
Magenta color
"""
None_: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.None_: -1>
"""
No color
"""
Yellow: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Yellow: 33>
"""
Yellow color
"""
__members__: typing.ClassVar[
dict[str, ConsoleForegroundColor]
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,33 @@ class ConsoleForegroundColor:
Blue: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Blue: 34>
"""
Blue color
"""
Green: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Green: 32>
"""
Green color
"""
Magenta: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Magenta: 35>
"""
Magenta color
"""
None_: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.None_: -1>
"""
No color
"""
Yellow: typing.ClassVar[
ConsoleForegroundColor
] # value = <ConsoleForegroundColor.Yellow: 33>
"""
Yellow color
"""
__members__: typing.ClassVar[
dict[str, ConsoleForegroundColor]
] # value = {'Green': <ConsoleForegroundColor.Green: 32>, 'Yellow': <ConsoleForegroundColor.Yellow: 33>, 'Blue': <ConsoleForegroundColor.Blue: 34>, 'Magenta': <ConsoleForegroundColor.Magenta: 35>, 'None_': <ConsoleForegroundColor.None_: -1>}
Expand Down