Skip to content

add option to disable printing '# value =' comments #251

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

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
12 changes: 11 additions & 1 deletion pybind11_stubgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class CLIArgs(Namespace):
numpy_array_remove_parameters: bool
print_invalid_expressions_as_is: bool
print_safe_value_reprs: re.Pattern | None
print_disable_value_comments: bool
exit_code: bool
dry_run: bool
stub_extension: str
Expand Down Expand Up @@ -190,6 +191,12 @@ def regex_colon_path(regex_path: str) -> tuple[re.Pattern, str]:
type=regex,
help="Override the print-safe check for values matching REGEX",
)
parser.add_argument(
"--print-disable-value-comments",
default=False,
action="store_true",
help="Disable printing attribute value comments, i.e., '# value = <value>'",
)

parser.add_argument(
"--exit-code",
Expand Down Expand Up @@ -308,7 +315,10 @@ def main():
args = arg_parser().parse_args(namespace=CLIArgs())

parser = stub_parser_from_args(args)
printer = Printer(invalid_expr_as_ellipses=not args.print_invalid_expressions_as_is)
printer = Printer(
invalid_expr_as_ellipses=not args.print_invalid_expressions_as_is,
print_disable_value_comments=args.print_disable_value_comments,
)

out_dir, sub_dir = to_output_and_subdir(
output_dir=args.output_dir,
Expand Down
7 changes: 5 additions & 2 deletions pybind11_stubgen/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ def indent_lines(lines: list[str], by=4) -> list[str]:


class Printer:
def __init__(self, invalid_expr_as_ellipses: bool):
def __init__(
self, invalid_expr_as_ellipses: bool, print_disable_value_comments: bool
):
self.invalid_expr_as_ellipses = invalid_expr_as_ellipses
self.print_disable_value_comments = print_disable_value_comments

def print_alias(self, alias: Alias) -> list[str]:
return [f"{alias.name} = {alias.origin}"]
Expand All @@ -48,7 +51,7 @@ def print_attribute(self, attr: Attribute) -> list[str]:
else:
if attr.annotation is None:
parts.append(" = ...")
if attr.value is not None:
if attr.value is not None and not self.print_disable_value_comments:
parts.append(f" # value = {self.print_value(attr.value)}")

return ["".join(parts)]
Expand Down