Skip to content

Commit 3a2dac9

Browse files
dyollbax3l
andauthored
Add option to print # value = comments, disable by default (#251)
Fixes #235 --------- Co-authored-by: Bryn Lloyd <12702862+dyollb@users.noreply.github.com> Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>
1 parent e0aa238 commit 3a2dac9

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pybind11-stubgen [-h]
3333
--numpy-array-use-type-var|
3434
--numpy-array-remove-parameters]
3535
[--print-invalid-expressions-as-is]
36+
[--print-value-comments]
3637
[--print-safe-value-reprs REGEX]
3738
[--exit-code]
3839
[--stub-extension EXT]

pybind11_stubgen/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class CLIArgs(Namespace):
7474
numpy_array_remove_parameters: bool
7575
print_invalid_expressions_as_is: bool
7676
print_safe_value_reprs: re.Pattern | None
77+
print_value_comments: bool
7778
exit_code: bool
7879
dry_run: bool
7980
stub_extension: str
@@ -191,6 +192,13 @@ def regex_colon_path(regex_path: str) -> tuple[re.Pattern, str]:
191192
type=regex,
192193
help="Override the print-safe check for values matching REGEX",
193194
)
195+
parser.add_argument(
196+
"--print-value-comments",
197+
default=False,
198+
action="store_true",
199+
help="Print attribute values as comments for debugging, "
200+
"i.e., '... # value = <value>'",
201+
)
194202

195203
parser.add_argument(
196204
"--exit-code",
@@ -310,7 +318,10 @@ def main(argv: Sequence[str] | None = None) -> None:
310318
args = arg_parser().parse_args(argv, namespace=CLIArgs())
311319

312320
parser = stub_parser_from_args(args)
313-
printer = Printer(invalid_expr_as_ellipses=not args.print_invalid_expressions_as_is)
321+
printer = Printer(
322+
invalid_expr_as_ellipses=not args.print_invalid_expressions_as_is,
323+
print_value_comments=args.print_value_comments,
324+
)
314325

315326
run(
316327
parser,

pybind11_stubgen/printer.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,13 @@ def _add_edge(dependency: str, dependent: str) -> None:
137137

138138

139139
class Printer:
140-
def __init__(self, invalid_expr_as_ellipses: bool):
140+
def __init__(
141+
self,
142+
invalid_expr_as_ellipses: bool,
143+
print_value_comments: bool = False,
144+
):
141145
self.invalid_expr_as_ellipses = invalid_expr_as_ellipses
146+
self.print_value_comments = print_value_comments
142147

143148
def _order_classes(self, classes: list[Class]) -> list[Class]:
144149
return _topological_sort_classes(classes)
@@ -158,7 +163,7 @@ def print_attribute(self, attr: Attribute) -> list[str]:
158163
else:
159164
if attr.annotation is None:
160165
parts.append(" = ...")
161-
if attr.value is not None:
166+
if attr.value is not None and self.print_value_comments:
162167
parts.append(f" # value = {self.print_value(attr.value)}")
163168

164169
return ["".join(parts)]

tests/check-demo-stubs-generation.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ run_stubgen() {
6161
${NUMPY_FORMAT} \
6262
--ignore-invalid-expressions="\(anonymous namespace\)::(Enum|Unbound)|<demo\._bindings\.flawed_bindings\..*" \
6363
--enum-class-locations="ConsoleForegroundColor:demo._bindings.enum" \
64+
--print-value-comments \
6465
--print-safe-value-reprs="Foo\(\d+\)" \
6566
--exit-code
6667
}

0 commit comments

Comments
 (0)